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/ssl.h" |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 47 | #include "mbedtls/ssl_internal.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 48 | #include "mbedtls/debug.h" |
| 49 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 50 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 51 | #include "mbedtls/version.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 52 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 53 | #include <string.h> |
| 54 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 55 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 56 | #include "mbedtls/psa_util.h" |
| 57 | #include "psa/crypto.h" |
| 58 | #endif |
| 59 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 60 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 61 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 62 | #endif |
| 63 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 64 | 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] | 65 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 66 | /* |
| 67 | * Start a timer. |
| 68 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 69 | */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 70 | void mbedtls_ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 71 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 72 | if( ssl->f_set_timer == NULL ) |
| 73 | return; |
| 74 | |
| 75 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); |
| 76 | ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Return -1 is timer is expired, 0 if it isn't. |
| 81 | */ |
Hanno Becker | 7876d12 | 2020-02-05 10:39:31 +0000 | [diff] [blame] | 82 | int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 83 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 84 | if( ssl->f_get_timer == NULL ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 85 | return( 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 86 | |
| 87 | if( ssl->f_get_timer( ssl->p_timer ) == 2 ) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 88 | { |
| 89 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 90 | return( -1 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 91 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 92 | |
| 93 | return( 0 ); |
| 94 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 95 | |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 96 | #if defined(MBEDTLS_SSL_RECORD_CHECKING) |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 97 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, |
| 98 | unsigned char *buf, |
| 99 | size_t len, |
| 100 | mbedtls_record *rec ); |
| 101 | |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 102 | int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl, |
| 103 | unsigned char *buf, |
| 104 | size_t buflen ) |
| 105 | { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 106 | int ret = 0; |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 107 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) ); |
| 108 | MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen ); |
| 109 | |
| 110 | /* We don't support record checking in TLS because |
| 111 | * (a) there doesn't seem to be a usecase for it, and |
| 112 | * (b) In SSLv3 and TLS 1.0, CBC record decryption has state |
| 113 | * and we'd need to backup the transform here. |
| 114 | */ |
| 115 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM ) |
| 116 | { |
| 117 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 118 | goto exit; |
| 119 | } |
| 120 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 121 | else |
| 122 | { |
irwir | 734f0cf | 2019-09-26 21:03:24 +0300 | [diff] [blame] | 123 | mbedtls_record rec; |
| 124 | |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 125 | ret = ssl_parse_record_header( ssl, buf, buflen, &rec ); |
| 126 | if( ret != 0 ) |
| 127 | { |
| 128 | MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret ); |
| 129 | goto exit; |
| 130 | } |
| 131 | |
| 132 | if( ssl->transform_in != NULL ) |
| 133 | { |
| 134 | ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec ); |
| 135 | if( ret != 0 ) |
| 136 | { |
| 137 | MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret ); |
| 138 | goto exit; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 143 | |
| 144 | exit: |
| 145 | /* On success, we have decrypted the buffer in-place, so make |
| 146 | * sure we don't leak any plaintext data. */ |
| 147 | mbedtls_platform_zeroize( buf, buflen ); |
| 148 | |
| 149 | /* For the purpose of this API, treat messages with unexpected CID |
| 150 | * as well as such from future epochs as unexpected. */ |
| 151 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID || |
| 152 | ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 153 | { |
| 154 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 155 | } |
| 156 | |
| 157 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) ); |
| 158 | return( ret ); |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 159 | } |
| 160 | #endif /* MBEDTLS_SSL_RECORD_CHECKING */ |
| 161 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 162 | #define SSL_DONT_FORCE_FLUSH 0 |
| 163 | #define SSL_FORCE_FLUSH 1 |
| 164 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 165 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 166 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 167 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 168 | /* Top-level Connection ID API */ |
| 169 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 170 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 171 | size_t len, |
| 172 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 173 | { |
| 174 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 175 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 176 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 177 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 178 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 179 | { |
| 180 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 181 | } |
| 182 | |
| 183 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 184 | conf->cid_len = len; |
| 185 | return( 0 ); |
| 186 | } |
| 187 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 188 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 189 | int enable, |
| 190 | unsigned char const *own_cid, |
| 191 | size_t own_cid_len ) |
| 192 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 193 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 194 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 195 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 196 | ssl->negotiate_cid = enable; |
| 197 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 198 | { |
| 199 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 200 | return( 0 ); |
| 201 | } |
| 202 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 203 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 204 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 205 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 206 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 207 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 208 | (unsigned) own_cid_len, |
| 209 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 210 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 211 | } |
| 212 | |
| 213 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 214 | /* Truncation is not an issue here because |
| 215 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 216 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 217 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 218 | return( 0 ); |
| 219 | } |
| 220 | |
| 221 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 222 | int *enabled, |
| 223 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 224 | size_t *peer_cid_len ) |
| 225 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 226 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 227 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 228 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 229 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 230 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 231 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 232 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 233 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 234 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 235 | * were used, but client and server requested the empty CID. |
| 236 | * This is indistinguishable from not using the CID extension |
| 237 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 238 | if( ssl->transform_in->in_cid_len == 0 && |
| 239 | ssl->transform_in->out_cid_len == 0 ) |
| 240 | { |
| 241 | return( 0 ); |
| 242 | } |
| 243 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 244 | if( peer_cid_len != NULL ) |
| 245 | { |
| 246 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 247 | if( peer_cid != NULL ) |
| 248 | { |
| 249 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 250 | ssl->transform_in->out_cid_len ); |
| 251 | } |
| 252 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 253 | |
| 254 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 255 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 256 | return( 0 ); |
| 257 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 258 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 259 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 260 | /* Forward declarations for functions related to message buffering. */ |
| 261 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ); |
| 262 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 263 | uint8_t slot ); |
| 264 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ); |
| 265 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ); |
| 266 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ); |
| 267 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 268 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, |
| 269 | mbedtls_record const *rec ); |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 270 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 271 | |
Hanno Becker | a67dee2 | 2018-08-22 10:05:20 +0100 | [diff] [blame] | 272 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ); |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 273 | 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] | 274 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 275 | size_t mtu = ssl_get_current_mtu( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 276 | |
| 277 | if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN ) |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 278 | return( mtu ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 279 | |
| 280 | return( MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 281 | } |
| 282 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 283 | static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl ) |
| 284 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 285 | size_t const bytes_written = ssl->out_left; |
| 286 | size_t const mtu = ssl_get_maximum_datagram_size( ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 287 | |
| 288 | /* Double-check that the write-index hasn't gone |
| 289 | * past what we can transmit in a single datagram. */ |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 290 | if( bytes_written > mtu ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 291 | { |
| 292 | /* Should never happen... */ |
| 293 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 294 | } |
| 295 | |
| 296 | return( (int) ( mtu - bytes_written ) ); |
| 297 | } |
| 298 | |
| 299 | static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl ) |
| 300 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 301 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 302 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 303 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 304 | |
| 305 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 306 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 307 | |
| 308 | if( max_len > mfl ) |
| 309 | max_len = mfl; |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 310 | |
| 311 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 312 | * only limits the maximum record payload size, so in theory |
| 313 | * we would be allowed to pack multiple records of payload size |
| 314 | * MFL into a single datagram. However, this would mean that there's |
| 315 | * no way to explicitly communicate MTU restrictions to the peer. |
| 316 | * |
| 317 | * The following reduction of max_len makes sure that we never |
| 318 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 319 | */ |
| 320 | if( max_len <= ssl->out_left ) |
| 321 | return( 0 ); |
| 322 | |
| 323 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 324 | #endif |
| 325 | |
| 326 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
| 327 | if( ret < 0 ) |
| 328 | return( ret ); |
| 329 | remaining = (size_t) ret; |
| 330 | |
| 331 | ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 332 | if( ret < 0 ) |
| 333 | return( ret ); |
| 334 | expansion = (size_t) ret; |
| 335 | |
| 336 | if( remaining <= expansion ) |
| 337 | return( 0 ); |
| 338 | |
| 339 | remaining -= expansion; |
| 340 | if( remaining >= max_len ) |
| 341 | remaining = max_len; |
| 342 | |
| 343 | return( (int) remaining ); |
| 344 | } |
| 345 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 346 | /* |
| 347 | * Double the retransmit timeout value, within the allowed range, |
| 348 | * returning -1 if the maximum value has already been reached. |
| 349 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 351 | { |
| 352 | uint32_t new_timeout; |
| 353 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 354 | if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 355 | return( -1 ); |
| 356 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 357 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 358 | * in the following way: after the initial transmission and a first |
| 359 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 360 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 361 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 362 | * on most non-IP stacks too. */ |
| 363 | if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min ) |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 364 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 365 | ssl->handshake->mtu = 508; |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 366 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) ); |
| 367 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 368 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 369 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 370 | |
| 371 | /* Avoid arithmetic overflow and range overflow */ |
| 372 | if( new_timeout < ssl->handshake->retransmit_timeout || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 373 | new_timeout > ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 374 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 375 | new_timeout = ssl->conf->hs_timeout_max; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | ssl->handshake->retransmit_timeout = new_timeout; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 379 | 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] | 380 | ssl->handshake->retransmit_timeout ) ); |
| 381 | |
| 382 | return( 0 ); |
| 383 | } |
| 384 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 385 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 386 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 387 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 388 | 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] | 389 | ssl->handshake->retransmit_timeout ) ); |
| 390 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 392 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 393 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 394 | /* |
| 395 | * Convert max_fragment_length codes to length. |
| 396 | * RFC 6066 says: |
| 397 | * enum{ |
| 398 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 399 | * } MaxFragmentLength; |
| 400 | * and we add 0 -> extension unused |
| 401 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 402 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 403 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 404 | switch( mfl ) |
| 405 | { |
| 406 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 407 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 408 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 409 | return 512; |
| 410 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 411 | return 1024; |
| 412 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 413 | return 2048; |
| 414 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 415 | return 4096; |
| 416 | default: |
| 417 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 418 | } |
| 419 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 420 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 421 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 422 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 423 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 424 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 425 | mbedtls_ssl_session_free( dst ); |
| 426 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 427 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 428 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 429 | |
| 430 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 431 | if( src->peer_cert != NULL ) |
| 432 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 433 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 434 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 435 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 436 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 437 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 438 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 439 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 441 | 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] | 442 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 443 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 444 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 445 | dst->peer_cert = NULL; |
| 446 | return( ret ); |
| 447 | } |
| 448 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 449 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 450 | if( src->peer_cert_digest != NULL ) |
| 451 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 452 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 453 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 454 | if( dst->peer_cert_digest == NULL ) |
| 455 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 456 | |
| 457 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 458 | src->peer_cert_digest_len ); |
| 459 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 460 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 461 | } |
| 462 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 463 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 464 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 465 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 466 | #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] | 467 | if( src->ticket != NULL ) |
| 468 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 469 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 470 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 471 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 472 | |
| 473 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 474 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 475 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 476 | |
| 477 | return( 0 ); |
| 478 | } |
| 479 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 480 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 481 | int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl, |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 482 | const unsigned char *key_enc, const unsigned char *key_dec, |
| 483 | size_t keylen, |
| 484 | const unsigned char *iv_enc, const unsigned char *iv_dec, |
| 485 | size_t ivlen, |
| 486 | const unsigned char *mac_enc, const unsigned char *mac_dec, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 487 | size_t maclen ) = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 488 | int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL; |
| 489 | int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL; |
| 490 | int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL; |
| 491 | int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL; |
| 492 | int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL; |
| 493 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 494 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 495 | /* |
| 496 | * Key material generation |
| 497 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 498 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 499 | static int ssl3_prf( const unsigned char *secret, size_t slen, |
| 500 | const char *label, |
| 501 | const unsigned char *random, size_t rlen, |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 502 | unsigned char *dstbuf, size_t dlen ) |
| 503 | { |
Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 504 | int ret = 0; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 505 | size_t i; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 506 | mbedtls_md5_context md5; |
| 507 | mbedtls_sha1_context sha1; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 508 | unsigned char padding[16]; |
| 509 | unsigned char sha1sum[20]; |
| 510 | ((void)label); |
| 511 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 512 | mbedtls_md5_init( &md5 ); |
| 513 | mbedtls_sha1_init( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 514 | |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 515 | /* |
| 516 | * SSLv3: |
| 517 | * block = |
| 518 | * MD5( secret + SHA1( 'A' + secret + random ) ) + |
| 519 | * MD5( secret + SHA1( 'BB' + secret + random ) ) + |
| 520 | * MD5( secret + SHA1( 'CCC' + secret + random ) ) + |
| 521 | * ... |
| 522 | */ |
| 523 | for( i = 0; i < dlen / 16; i++ ) |
| 524 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 525 | memset( padding, (unsigned char) ('A' + i), 1 + i ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 526 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 527 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 528 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 529 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 530 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 531 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 532 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 533 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 534 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 535 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 536 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 537 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 538 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 539 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 540 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 541 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 542 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 543 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 544 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 545 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 548 | exit: |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 549 | mbedtls_md5_free( &md5 ); |
| 550 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 551 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 552 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); |
| 553 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 554 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 555 | return( ret ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 556 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 557 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 558 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 559 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 560 | static int tls1_prf( const unsigned char *secret, size_t slen, |
| 561 | const char *label, |
| 562 | const unsigned char *random, size_t rlen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 563 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 564 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 565 | size_t nb, hs; |
| 566 | size_t i, j, k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 567 | const unsigned char *S1, *S2; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 568 | unsigned char *tmp; |
| 569 | size_t tmp_len = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 570 | unsigned char h_i[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 571 | const mbedtls_md_info_t *md_info; |
| 572 | mbedtls_md_context_t md_ctx; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 573 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 574 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 575 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 576 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 577 | tmp_len = 20 + strlen( label ) + rlen; |
| 578 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 579 | if( tmp == NULL ) |
| 580 | { |
| 581 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 582 | goto exit; |
| 583 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 584 | |
| 585 | hs = ( slen + 1 ) / 2; |
| 586 | S1 = secret; |
| 587 | S2 = secret + slen - hs; |
| 588 | |
| 589 | nb = strlen( label ); |
| 590 | memcpy( tmp + 20, label, nb ); |
| 591 | memcpy( tmp + 20 + nb, random, rlen ); |
| 592 | nb += rlen; |
| 593 | |
| 594 | /* |
| 595 | * First compute P_md5(secret,label+random)[0..dlen] |
| 596 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 597 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 598 | { |
| 599 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 600 | goto exit; |
| 601 | } |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 602 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 603 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 604 | { |
| 605 | goto exit; |
| 606 | } |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 608 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); |
| 609 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 610 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 611 | |
| 612 | for( i = 0; i < dlen; i += 16 ) |
| 613 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 614 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 615 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); |
| 616 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 618 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 619 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); |
| 620 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 621 | |
| 622 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; |
| 623 | |
| 624 | for( j = 0; j < k; j++ ) |
| 625 | dstbuf[i + j] = h_i[j]; |
| 626 | } |
| 627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 629 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 630 | /* |
| 631 | * XOR out with P_sha1(secret,label+random)[0..dlen] |
| 632 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 633 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 634 | { |
| 635 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 636 | goto exit; |
| 637 | } |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 638 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 639 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 640 | { |
| 641 | goto exit; |
| 642 | } |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 643 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 644 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); |
| 645 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 646 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 647 | |
| 648 | for( i = 0; i < dlen; i += 20 ) |
| 649 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 650 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 651 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); |
| 652 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 655 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); |
| 656 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 657 | |
| 658 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; |
| 659 | |
| 660 | for( j = 0; j < k; j++ ) |
| 661 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); |
| 662 | } |
| 663 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 664 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 666 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 667 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 668 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 669 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 670 | mbedtls_free( tmp ); |
| 671 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 672 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 673 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 675 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 676 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 677 | |
| 678 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 679 | psa_key_handle_t slot, |
| 680 | psa_algorithm_t alg, |
| 681 | const unsigned char* seed, size_t seed_length, |
| 682 | const unsigned char* label, size_t label_length, |
| 683 | size_t capacity ) |
| 684 | { |
| 685 | psa_status_t status; |
| 686 | |
| 687 | status = psa_key_derivation_setup( derivation, alg ); |
| 688 | if( status != PSA_SUCCESS ) |
| 689 | return( status ); |
| 690 | |
| 691 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 692 | { |
| 693 | status = psa_key_derivation_input_bytes( derivation, |
| 694 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 695 | seed, seed_length ); |
| 696 | if( status != PSA_SUCCESS ) |
| 697 | return( status ); |
| 698 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 699 | if( slot == 0 ) |
| 700 | { |
| 701 | status = psa_key_derivation_input_bytes( |
| 702 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 703 | NULL, 0 ); |
| 704 | } |
| 705 | else |
| 706 | { |
| 707 | status = psa_key_derivation_input_key( |
| 708 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 709 | slot ); |
| 710 | } |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 711 | if( status != PSA_SUCCESS ) |
| 712 | return( status ); |
| 713 | |
| 714 | status = psa_key_derivation_input_bytes( derivation, |
| 715 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 716 | label, label_length ); |
| 717 | if( status != PSA_SUCCESS ) |
| 718 | return( status ); |
| 719 | } |
| 720 | else |
| 721 | { |
| 722 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 723 | } |
| 724 | |
| 725 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 726 | if( status != PSA_SUCCESS ) |
| 727 | return( status ); |
| 728 | |
| 729 | return( PSA_SUCCESS ); |
| 730 | } |
| 731 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 732 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 733 | const unsigned char *secret, size_t slen, |
| 734 | const char *label, |
| 735 | const unsigned char *random, size_t rlen, |
| 736 | unsigned char *dstbuf, size_t dlen ) |
| 737 | { |
| 738 | psa_status_t status; |
| 739 | psa_algorithm_t alg; |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 740 | psa_key_handle_t master_slot = 0; |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 741 | psa_key_derivation_operation_t derivation = |
Janos Follath | 8dee877 | 2019-07-30 12:53:32 +0100 | [diff] [blame] | 742 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 743 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 744 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 745 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 746 | else |
| 747 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 748 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 749 | /* Normally a "secret" should be long enough to be impossible to |
| 750 | * find by brute force, and in particular should not be empty. But |
| 751 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 752 | * and for this use case it makes sense to have a 0-length "secret". |
| 753 | * Since the key API doesn't allow importing a key of length 0, |
| 754 | * keep master_slot=0, which setup_psa_key_derivation() understands |
| 755 | * to mean a 0-length "secret" input. */ |
| 756 | if( slen != 0 ) |
| 757 | { |
| 758 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 759 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 760 | psa_set_key_algorithm( &key_attributes, alg ); |
| 761 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 762 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 763 | status = psa_import_key( &key_attributes, secret, slen, &master_slot ); |
| 764 | if( status != PSA_SUCCESS ) |
| 765 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 766 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 767 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 768 | status = setup_psa_key_derivation( &derivation, |
| 769 | master_slot, alg, |
| 770 | random, rlen, |
| 771 | (unsigned char const *) label, |
| 772 | (size_t) strlen( label ), |
| 773 | dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 774 | if( status != PSA_SUCCESS ) |
| 775 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 776 | psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 777 | psa_destroy_key( master_slot ); |
| 778 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 779 | } |
| 780 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 781 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 782 | if( status != PSA_SUCCESS ) |
| 783 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 784 | psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 785 | psa_destroy_key( master_slot ); |
| 786 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 787 | } |
| 788 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 789 | status = psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 790 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 791 | { |
| 792 | psa_destroy_key( master_slot ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 793 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 794 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 795 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 796 | if( master_slot != 0 ) |
| 797 | status = psa_destroy_key( master_slot ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 798 | if( status != PSA_SUCCESS ) |
| 799 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 800 | |
Andrzej Kurek | 3317126 | 2019-01-15 03:25:18 -0500 | [diff] [blame] | 801 | return( 0 ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 805 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 806 | 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] | 807 | const unsigned char *secret, size_t slen, |
| 808 | const char *label, |
| 809 | const unsigned char *random, size_t rlen, |
| 810 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 811 | { |
| 812 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 813 | size_t i, j, k, md_len; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 814 | unsigned char *tmp; |
| 815 | size_t tmp_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 817 | const mbedtls_md_info_t *md_info; |
| 818 | mbedtls_md_context_t md_ctx; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 819 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 820 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 821 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 822 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 823 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 824 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 825 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 826 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 827 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 828 | tmp_len = md_len + strlen( label ) + rlen; |
| 829 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 830 | if( tmp == NULL ) |
| 831 | { |
| 832 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 833 | goto exit; |
| 834 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 835 | |
| 836 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 837 | memcpy( tmp + md_len, label, nb ); |
| 838 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 839 | nb += rlen; |
| 840 | |
| 841 | /* |
| 842 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 843 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 844 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 845 | goto exit; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 846 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 847 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 848 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 849 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 850 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 851 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 852 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 853 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 854 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 855 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 856 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 857 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 858 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 859 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 860 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 861 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 862 | |
| 863 | for( j = 0; j < k; j++ ) |
| 864 | dstbuf[i + j] = h_i[j]; |
| 865 | } |
| 866 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 867 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 868 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 869 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 870 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 871 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 872 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 873 | mbedtls_free( tmp ); |
| 874 | |
| 875 | return( ret ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 876 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 877 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 878 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 879 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 880 | const char *label, |
| 881 | const unsigned char *random, size_t rlen, |
| 882 | unsigned char *dstbuf, size_t dlen ) |
| 883 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 884 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 885 | label, random, rlen, dstbuf, dlen ) ); |
| 886 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 887 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 888 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 889 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 890 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 891 | const char *label, |
| 892 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 893 | unsigned char *dstbuf, size_t dlen ) |
| 894 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 895 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 896 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 897 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 898 | #endif /* MBEDTLS_SHA512_C */ |
| 899 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 900 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 901 | 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] | 902 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 903 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 904 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 905 | 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] | 906 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 907 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 908 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 909 | 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] | 910 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 911 | #endif |
| 912 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 913 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 914 | 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] | 915 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 916 | #endif |
| 917 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 918 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 919 | #if defined(MBEDTLS_SHA256_C) |
| 920 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 921 | 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] | 922 | 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] | 923 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 924 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 925 | #if defined(MBEDTLS_SHA512_C) |
| 926 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 927 | 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] | 928 | 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] | 929 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 930 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 931 | |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 932 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 933 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 934 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) |
| 935 | { |
| 936 | if( ssl->conf->f_psk != NULL ) |
| 937 | { |
| 938 | /* If we've used a callback to select the PSK, |
| 939 | * the static configuration is irrelevant. */ |
| 940 | if( ssl->handshake->psk_opaque != 0 ) |
| 941 | return( 1 ); |
| 942 | |
| 943 | return( 0 ); |
| 944 | } |
| 945 | |
| 946 | if( ssl->conf->psk_opaque != 0 ) |
| 947 | return( 1 ); |
| 948 | |
| 949 | return( 0 ); |
| 950 | } |
| 951 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 952 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 953 | |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 954 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 955 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 956 | { |
| 957 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 958 | if( tls_prf == ssl3_prf ) |
| 959 | { |
Ron Eldor | 0810f0b | 2019-05-15 12:32:32 +0300 | [diff] [blame] | 960 | return( MBEDTLS_SSL_TLS_PRF_SSL3 ); |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 961 | } |
| 962 | else |
| 963 | #endif |
| 964 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 965 | if( tls_prf == tls1_prf ) |
| 966 | { |
| 967 | return( MBEDTLS_SSL_TLS_PRF_TLS1 ); |
| 968 | } |
| 969 | else |
| 970 | #endif |
| 971 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 972 | #if defined(MBEDTLS_SHA512_C) |
| 973 | if( tls_prf == tls_prf_sha384 ) |
| 974 | { |
| 975 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 976 | } |
| 977 | else |
| 978 | #endif |
| 979 | #if defined(MBEDTLS_SHA256_C) |
| 980 | if( tls_prf == tls_prf_sha256 ) |
| 981 | { |
| 982 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 983 | } |
| 984 | else |
| 985 | #endif |
| 986 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 987 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 988 | } |
| 989 | #endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
| 990 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 991 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 992 | const unsigned char *secret, size_t slen, |
| 993 | const char *label, |
| 994 | const unsigned char *random, size_t rlen, |
| 995 | unsigned char *dstbuf, size_t dlen ) |
| 996 | { |
| 997 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 998 | |
| 999 | switch( prf ) |
| 1000 | { |
| 1001 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1002 | case MBEDTLS_SSL_TLS_PRF_SSL3: |
| 1003 | tls_prf = ssl3_prf; |
| 1004 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 1005 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1006 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 1007 | case MBEDTLS_SSL_TLS_PRF_TLS1: |
| 1008 | tls_prf = tls1_prf; |
| 1009 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 1010 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 1011 | |
| 1012 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1013 | #if defined(MBEDTLS_SHA512_C) |
| 1014 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 1015 | tls_prf = tls_prf_sha384; |
| 1016 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 1017 | #endif /* MBEDTLS_SHA512_C */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1018 | #if defined(MBEDTLS_SHA256_C) |
| 1019 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 1020 | tls_prf = tls_prf_sha256; |
| 1021 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 1022 | #endif /* MBEDTLS_SHA256_C */ |
| 1023 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1024 | default: |
| 1025 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1026 | } |
| 1027 | |
| 1028 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 1029 | } |
| 1030 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1031 | /* Type for the TLS PRF */ |
| 1032 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 1033 | const unsigned char *, size_t, |
| 1034 | unsigned char *, size_t); |
| 1035 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1036 | /* |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1037 | * Populate a transform structure with session keys and all the other |
| 1038 | * necessary information. |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1039 | * |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1040 | * Parameters: |
| 1041 | * - [in/out]: transform: structure to populate |
| 1042 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1043 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1044 | * - [in] ciphersuite |
| 1045 | * - [in] master |
| 1046 | * - [in] encrypt_then_mac |
| 1047 | * - [in] trunc_hmac |
| 1048 | * - [in] compression |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1049 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 1050 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1051 | * - [in] minor_ver: SSL/TLS minor version |
| 1052 | * - [in] endpoint: client or server |
| 1053 | * - [in] ssl: optionally used for: |
| 1054 | * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context |
| 1055 | * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys |
| 1056 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1057 | */ |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1058 | static int ssl_populate_transform( mbedtls_ssl_transform *transform, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1059 | int ciphersuite, |
| 1060 | const unsigned char master[48], |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1061 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1062 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1063 | int encrypt_then_mac, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1064 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1065 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1066 | int trunc_hmac, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1067 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 1068 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1069 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1070 | int compression, |
| 1071 | #endif |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1072 | ssl_tls_prf_t tls_prf, |
| 1073 | const unsigned char randbytes[64], |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1074 | int minor_ver, |
| 1075 | unsigned endpoint, |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1076 | const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1077 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1078 | int ret = 0; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1079 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1080 | int psa_fallthrough; |
| 1081 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1082 | unsigned char keyblk[256]; |
| 1083 | unsigned char *key1; |
| 1084 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1085 | unsigned char *mac_enc; |
| 1086 | unsigned char *mac_dec; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1087 | size_t mac_key_len; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1088 | size_t iv_copy_len; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1089 | unsigned keylen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1090 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | const mbedtls_cipher_info_t *cipher_info; |
| 1092 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1093 | |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1094 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \ |
| 1095 | !defined(MBEDTLS_SSL_EXPORT_KEYS) && \ |
| 1096 | !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1097 | ssl = NULL; /* make sure we don't use it except for those cases */ |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1098 | (void) ssl; |
| 1099 | #endif |
| 1100 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 1101 | /* |
| 1102 | * Some data just needs copying into the structure |
| 1103 | */ |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1104 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 1105 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1106 | transform->encrypt_then_mac = encrypt_then_mac; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1107 | #endif |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1108 | transform->minor_ver = minor_ver; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1109 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 1110 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 1111 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 1112 | #endif |
| 1113 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1114 | /* |
| 1115 | * Get various info structures |
| 1116 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1117 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1118 | if( ciphersuite_info == NULL ) |
| 1119 | { |
| 1120 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1121 | ciphersuite ) ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1122 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1123 | } |
| 1124 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1125 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1126 | if( cipher_info == NULL ) |
| 1127 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1128 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1129 | ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1130 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1131 | } |
| 1132 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1133 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1134 | if( md_info == NULL ) |
| 1135 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1136 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1137 | ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1138 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1139 | } |
| 1140 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1141 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1142 | /* Copy own and peer's CID if the use of the CID |
| 1143 | * extension has been negotiated. */ |
| 1144 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 1145 | { |
| 1146 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | 8a7f972 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 1147 | |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 1148 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 1149 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 1c1f046 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 1150 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1151 | transform->in_cid_len ); |
Hanno Becker | d1f2035 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 1152 | |
| 1153 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 1154 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 1155 | ssl->handshake->peer_cid_len ); |
| 1156 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 1157 | transform->out_cid_len ); |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1158 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1159 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1160 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1161 | /* |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1162 | * Compute key block using the PRF |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1163 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1164 | 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] | 1165 | if( ret != 0 ) |
| 1166 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1167 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1168 | return( ret ); |
| 1169 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1170 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1171 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1172 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1173 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1174 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1175 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1176 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1177 | /* |
| 1178 | * Determine the appropriate key, IV and MAC length. |
| 1179 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1180 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1181 | keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1182 | |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1183 | #if defined(MBEDTLS_GCM_C) || \ |
| 1184 | defined(MBEDTLS_CCM_C) || \ |
| 1185 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1186 | if( cipher_info->mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1187 | cipher_info->mode == MBEDTLS_MODE_CCM || |
| 1188 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1189 | { |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1190 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1191 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1192 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1193 | mac_key_len = 0; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1194 | transform->taglen = |
| 1195 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1196 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1197 | /* All modes haves 96-bit IVs; |
| 1198 | * GCM and CCM has 4 implicit and 8 explicit bytes |
| 1199 | * ChachaPoly has all 12 bytes implicit |
| 1200 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1201 | transform->ivlen = 12; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1202 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1203 | transform->fixed_ivlen = 12; |
| 1204 | else |
| 1205 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1206 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1207 | /* Minimum length of encrypted record */ |
| 1208 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1209 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1210 | } |
| 1211 | else |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1212 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 1213 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 1214 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 1215 | cipher_info->mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1216 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1217 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1218 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 1219 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1220 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1221 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1222 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1223 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1224 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1225 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1226 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 1227 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1228 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1229 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1230 | /* |
| 1231 | * If HMAC is to be truncated, we shall keep the leftmost bytes, |
| 1232 | * (rfc 6066 page 13 or rfc 2104 section 4), |
| 1233 | * so we only need to adjust the length here. |
| 1234 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1235 | if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1236 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1237 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1238 | |
| 1239 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) |
| 1240 | /* Fall back to old, non-compliant version of the truncated |
Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 1241 | * HMAC implementation which also truncates the key |
| 1242 | * (Mbed TLS versions from 1.3 to 2.6.0) */ |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1243 | mac_key_len = transform->maclen; |
| 1244 | #endif |
| 1245 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1247 | |
| 1248 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1249 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1250 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1251 | /* Minimum length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1252 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1253 | transform->minlen = transform->maclen; |
| 1254 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1255 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1256 | /* |
| 1257 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1258 | * 1. if EtM is in use: one block plus MAC |
| 1259 | * otherwise: * first multiple of blocklen greater than maclen |
| 1260 | * 2. IV except for SSL3 and TLS 1.0 |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1261 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1262 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1263 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1264 | { |
| 1265 | transform->minlen = transform->maclen |
| 1266 | + cipher_info->block_size; |
| 1267 | } |
| 1268 | else |
| 1269 | #endif |
| 1270 | { |
| 1271 | transform->minlen = transform->maclen |
| 1272 | + cipher_info->block_size |
| 1273 | - transform->maclen % cipher_info->block_size; |
| 1274 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1275 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1276 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1277 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 1278 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1279 | ; /* No need to adjust minlen */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1280 | else |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1281 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1282 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1283 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || |
| 1284 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1285 | { |
| 1286 | transform->minlen += transform->ivlen; |
| 1287 | } |
| 1288 | else |
| 1289 | #endif |
| 1290 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1291 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1292 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1293 | goto end; |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1294 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1295 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1296 | } |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1297 | else |
| 1298 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1299 | { |
| 1300 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1301 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1302 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1303 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1304 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 1305 | (unsigned) keylen, |
| 1306 | (unsigned) transform->minlen, |
| 1307 | (unsigned) transform->ivlen, |
| 1308 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1309 | |
| 1310 | /* |
| 1311 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 1312 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1313 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1314 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1315 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1316 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1317 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1318 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1319 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1320 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1321 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1322 | /* |
| 1323 | * This is not used in TLS v1.1. |
| 1324 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1325 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1326 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1327 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 1328 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1329 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1330 | } |
| 1331 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1332 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1333 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1334 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1335 | { |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1336 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1337 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1338 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1339 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1340 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1341 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1342 | /* |
| 1343 | * This is not used in TLS v1.1. |
| 1344 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1345 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1346 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1347 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 1348 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1349 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1350 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1351 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1352 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1353 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1354 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1355 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1356 | goto end; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1357 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1358 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1359 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1360 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1361 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1362 | { |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1363 | if( mac_key_len > sizeof( transform->mac_enc ) ) |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1364 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1365 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1366 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1367 | goto end; |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1368 | } |
| 1369 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1370 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); |
| 1371 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1372 | } |
| 1373 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1374 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1375 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1376 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1377 | if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1378 | { |
Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1379 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 1380 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 1381 | if( mac_key_len != 0 ) |
| 1382 | { |
| 1383 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 1384 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 1385 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1386 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1387 | else |
| 1388 | #endif |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1389 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1390 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1391 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1392 | goto end; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1393 | } |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1394 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1395 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1396 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 1397 | if( mbedtls_ssl_hw_record_init != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1398 | { |
| 1399 | int ret = 0; |
| 1400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1401 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1402 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1403 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1404 | transform->iv_enc, transform->iv_dec, |
| 1405 | iv_copy_len, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1406 | mac_enc, mac_dec, |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1407 | mac_key_len ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1408 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1409 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1410 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1411 | goto end; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1412 | } |
| 1413 | } |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1414 | #else |
| 1415 | ((void) mac_dec); |
| 1416 | ((void) mac_enc); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1417 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1418 | |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1419 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 1420 | if( ssl->conf->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1421 | { |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1422 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1423 | master, keyblk, |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1424 | mac_key_len, keylen, |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1425 | iv_copy_len ); |
| 1426 | } |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1427 | |
| 1428 | if( ssl->conf->f_export_keys_ext != NULL ) |
| 1429 | { |
| 1430 | ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1431 | master, keyblk, |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1432 | mac_key_len, keylen, |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1433 | iv_copy_len, |
Jaeden Amero | fa63645 | 2019-09-12 10:47:37 +0100 | [diff] [blame] | 1434 | randbytes + 32, |
| 1435 | randbytes, |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1436 | tls_prf_get_type( tls_prf ) ); |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1437 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1438 | #endif |
| 1439 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1440 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1441 | |
| 1442 | /* Only use PSA-based ciphers for TLS-1.2. |
| 1443 | * That's relevant at least for TLS-1.0, where |
| 1444 | * we assume that mbedtls_cipher_crypt() updates |
| 1445 | * the structure field for the IV, which the PSA-based |
| 1446 | * implementation currently doesn't. */ |
| 1447 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1448 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1449 | { |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1450 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc, |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1451 | cipher_info, transform->taglen ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1452 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 1453 | { |
| 1454 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1455 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | if( ret == 0 ) |
| 1459 | { |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1460 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1461 | psa_fallthrough = 0; |
| 1462 | } |
| 1463 | else |
| 1464 | { |
| 1465 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) ); |
| 1466 | psa_fallthrough = 1; |
| 1467 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1468 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1469 | else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1470 | psa_fallthrough = 1; |
| 1471 | #else |
| 1472 | psa_fallthrough = 1; |
| 1473 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1474 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1475 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1476 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1477 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1478 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1479 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1480 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1481 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1482 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1483 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1484 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1485 | /* Only use PSA-based ciphers for TLS-1.2. |
| 1486 | * That's relevant at least for TLS-1.0, where |
| 1487 | * we assume that mbedtls_cipher_crypt() updates |
| 1488 | * the structure field for the IV, which the PSA-based |
| 1489 | * implementation currently doesn't. */ |
| 1490 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1491 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1492 | { |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1493 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec, |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1494 | cipher_info, transform->taglen ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1495 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 1496 | { |
| 1497 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1498 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | if( ret == 0 ) |
| 1502 | { |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1503 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1504 | psa_fallthrough = 0; |
| 1505 | } |
| 1506 | else |
| 1507 | { |
| 1508 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) ); |
| 1509 | psa_fallthrough = 1; |
| 1510 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1511 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1512 | else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1513 | psa_fallthrough = 1; |
| 1514 | #else |
| 1515 | psa_fallthrough = 1; |
| 1516 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1517 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1518 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1519 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1520 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1521 | cipher_info ) ) != 0 ) |
| 1522 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1523 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1524 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1525 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1526 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1527 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1528 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1529 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1530 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1531 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1532 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1533 | } |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1534 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1535 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1536 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1537 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1538 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1539 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1540 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1541 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1543 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1544 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1545 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1546 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1547 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1548 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1549 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1550 | goto end; |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1551 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1553 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1554 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1555 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1556 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1557 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1558 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1559 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1560 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1561 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1562 | |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1563 | /* Initialize Zlib contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1564 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1565 | if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1566 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1567 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1568 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1569 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); |
| 1570 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1571 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1572 | if( deflateInit( &transform->ctx_deflate, |
| 1573 | Z_DEFAULT_COMPRESSION ) != Z_OK || |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1574 | inflateInit( &transform->ctx_inflate ) != Z_OK ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1575 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1576 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1577 | ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED; |
| 1578 | goto end; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1579 | } |
| 1580 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1581 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1582 | |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1583 | end: |
Ron Eldor | a9f9a73 | 2019-05-07 18:29:02 +0300 | [diff] [blame] | 1584 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1585 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1588 | /* |
Manuel Pégourié-Gonnard | 47e33e1 | 2019-05-20 10:10:17 +0200 | [diff] [blame] | 1589 | * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1590 | * |
| 1591 | * Inputs: |
| 1592 | * - SSL/TLS minor version |
| 1593 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1594 | * |
Manuel Pégourié-Gonnard | 31d3ef1 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1595 | * Outputs: |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1596 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1597 | */ |
| 1598 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1599 | int minor_ver, |
| 1600 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1601 | { |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1602 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) |
| 1603 | (void) hash; |
| 1604 | #endif |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1605 | |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1606 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1607 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1608 | { |
| 1609 | handshake->tls_prf = ssl3_prf; |
| 1610 | handshake->calc_verify = ssl_calc_verify_ssl; |
| 1611 | handshake->calc_finished = ssl_calc_finished_ssl; |
| 1612 | } |
| 1613 | else |
| 1614 | #endif |
| 1615 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1616 | if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1617 | { |
| 1618 | handshake->tls_prf = tls1_prf; |
| 1619 | handshake->calc_verify = ssl_calc_verify_tls; |
| 1620 | handshake->calc_finished = ssl_calc_finished_tls; |
| 1621 | } |
| 1622 | else |
| 1623 | #endif |
| 1624 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1625 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1626 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1627 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1628 | { |
| 1629 | handshake->tls_prf = tls_prf_sha384; |
| 1630 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1631 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1632 | } |
| 1633 | else |
| 1634 | #endif |
| 1635 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1636 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1637 | { |
| 1638 | handshake->tls_prf = tls_prf_sha256; |
| 1639 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1640 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1641 | } |
| 1642 | else |
| 1643 | #endif |
| 1644 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1645 | { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1646 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1647 | } |
| 1648 | |
| 1649 | return( 0 ); |
| 1650 | } |
| 1651 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1652 | /* |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1653 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1654 | * |
| 1655 | * Parameters: |
| 1656 | * [in/out] handshake |
| 1657 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1658 | * (PSA-PSK) ciphersuite_info, psk_opaque |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1659 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1660 | * [out] master |
| 1661 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 1662 | * debug: conf->f_dbg, conf->p_dbg |
| 1663 | * EMS: passed to calc_verify (debug + (SSL3) session_negotiate) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1664 | * PSA-PSA: minor_ver, conf |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1665 | */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1666 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1667 | unsigned char *master, |
Manuel Pégourié-Gonnard | 0d56aaa | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1668 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1669 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1670 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1671 | |
| 1672 | /* cf. RFC 5246, Section 8.1: |
| 1673 | * "The master secret is always exactly 48 bytes in length." */ |
| 1674 | size_t const master_secret_len = 48; |
| 1675 | |
| 1676 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1677 | unsigned char session_hash[48]; |
| 1678 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 1679 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1680 | /* The label for the KDF used for key expansion. |
| 1681 | * This is either "master secret" or "extended master secret" |
| 1682 | * depending on whether the Extended Master Secret extension |
| 1683 | * is used. */ |
| 1684 | char const *lbl = "master secret"; |
| 1685 | |
| 1686 | /* The salt for the KDF used for key expansion. |
| 1687 | * - If the Extended Master Secret extension is not used, |
| 1688 | * this is ClientHello.Random + ServerHello.Random |
| 1689 | * (see Sect. 8.1 in RFC 5246). |
| 1690 | * - If the Extended Master Secret extension is used, |
| 1691 | * this is the transcript of the handshake so far. |
| 1692 | * (see Sect. 4 in RFC 7627). */ |
| 1693 | unsigned char const *salt = handshake->randbytes; |
| 1694 | size_t salt_len = 64; |
| 1695 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1696 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 1697 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 1698 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1699 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1700 | ssl = NULL; /* make sure we don't use it except for those cases */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1701 | (void) ssl; |
| 1702 | #endif |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1703 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1704 | if( handshake->resume != 0 ) |
| 1705 | { |
| 1706 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1707 | return( 0 ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1708 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1709 | |
| 1710 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1711 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1712 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1713 | lbl = "extended master secret"; |
| 1714 | salt = session_hash; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1715 | handshake->calc_verify( ssl, session_hash, &salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1716 | |
Manuel Pégourié-Gonnard | 8faa70e | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1717 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 1718 | session_hash, salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1719 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1720 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
| 1721 | |
| 1722 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1723 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1724 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1725 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1726 | ssl_use_opaque_psk( ssl ) == 1 ) |
| 1727 | { |
| 1728 | /* Perform PSK-to-MS expansion in a single step. */ |
| 1729 | psa_status_t status; |
| 1730 | psa_algorithm_t alg; |
| 1731 | psa_key_handle_t psk; |
| 1732 | psa_key_derivation_operation_t derivation = |
| 1733 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1734 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1735 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1736 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1737 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1738 | psk = ssl->conf->psk_opaque; |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1739 | if( handshake->psk_opaque != 0 ) |
| 1740 | psk = handshake->psk_opaque; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1741 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1742 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1743 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1744 | else |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1745 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1746 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 1747 | status = setup_psa_key_derivation( &derivation, psk, alg, |
| 1748 | salt, salt_len, |
| 1749 | (unsigned char const *) lbl, |
| 1750 | (size_t) strlen( lbl ), |
| 1751 | master_secret_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1752 | if( status != PSA_SUCCESS ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1753 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1754 | psa_key_derivation_abort( &derivation ); |
| 1755 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1756 | } |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1757 | |
| 1758 | status = psa_key_derivation_output_bytes( &derivation, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1759 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1760 | master_secret_len ); |
| 1761 | if( status != PSA_SUCCESS ) |
| 1762 | { |
| 1763 | psa_key_derivation_abort( &derivation ); |
| 1764 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1765 | } |
| 1766 | |
| 1767 | status = psa_key_derivation_abort( &derivation ); |
| 1768 | if( status != PSA_SUCCESS ) |
| 1769 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1770 | } |
| 1771 | else |
| 1772 | #endif |
| 1773 | { |
| 1774 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1775 | lbl, salt, salt_len, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1776 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1777 | master_secret_len ); |
| 1778 | if( ret != 0 ) |
| 1779 | { |
| 1780 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1781 | return( ret ); |
| 1782 | } |
| 1783 | |
| 1784 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 1785 | handshake->premaster, |
| 1786 | handshake->pmslen ); |
| 1787 | |
| 1788 | mbedtls_platform_zeroize( handshake->premaster, |
| 1789 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | return( 0 ); |
| 1793 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1794 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1795 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1796 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1797 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1798 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 1799 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1800 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1801 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1802 | |
| 1803 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1804 | ret = ssl_set_handshake_prfs( ssl->handshake, |
| 1805 | ssl->minor_ver, |
| 1806 | ciphersuite_info->mac ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1807 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1808 | { |
| 1809 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1810 | return( ret ); |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1811 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1812 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1813 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1814 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1815 | ssl->session_negotiate->master, |
| 1816 | ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1817 | if( ret != 0 ) |
| 1818 | { |
| 1819 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1820 | return( ret ); |
| 1821 | } |
| 1822 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1823 | /* Swap the client and server random values: |
| 1824 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1825 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1826 | { |
| 1827 | unsigned char tmp[64]; |
| 1828 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1829 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1830 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1831 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1832 | } |
| 1833 | |
| 1834 | /* Populate transform structure */ |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1835 | ret = ssl_populate_transform( ssl->transform_negotiate, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1836 | ssl->session_negotiate->ciphersuite, |
| 1837 | ssl->session_negotiate->master, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1838 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1839 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1840 | ssl->session_negotiate->encrypt_then_mac, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1841 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1842 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1843 | ssl->session_negotiate->trunc_hmac, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1844 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 1845 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1846 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1847 | ssl->session_negotiate->compression, |
| 1848 | #endif |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1849 | ssl->handshake->tls_prf, |
| 1850 | ssl->handshake->randbytes, |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1851 | ssl->minor_ver, |
| 1852 | ssl->conf->endpoint, |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1853 | ssl ); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1854 | if( ret != 0 ) |
| 1855 | { |
| 1856 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret ); |
| 1857 | return( ret ); |
| 1858 | } |
| 1859 | |
| 1860 | /* We no longer need Server/ClientHello.random values */ |
| 1861 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1862 | sizeof( ssl->handshake->randbytes ) ); |
| 1863 | |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1864 | /* Allocate compression buffer */ |
| 1865 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
jiblime | 9f25b8d | 2019-12-18 21:40:01 -0800 | [diff] [blame] | 1866 | if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE && |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1867 | ssl->compress_buf == NULL ) |
| 1868 | { |
| 1869 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); |
| 1870 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
| 1871 | if( ssl->compress_buf == NULL ) |
| 1872 | { |
| 1873 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1874 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1875 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1876 | } |
| 1877 | } |
| 1878 | #endif |
| 1879 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1880 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1881 | |
| 1882 | return( 0 ); |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1883 | } |
| 1884 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1885 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1886 | void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl, |
| 1887 | unsigned char hash[36], |
| 1888 | size_t *hlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1889 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1890 | mbedtls_md5_context md5; |
| 1891 | mbedtls_sha1_context sha1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1892 | unsigned char pad_1[48]; |
| 1893 | unsigned char pad_2[48]; |
| 1894 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1895 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1896 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1897 | mbedtls_md5_init( &md5 ); |
| 1898 | mbedtls_sha1_init( &sha1 ); |
| 1899 | |
| 1900 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1901 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1902 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1903 | memset( pad_1, 0x36, 48 ); |
| 1904 | memset( pad_2, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1905 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1906 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1907 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); |
| 1908 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1909 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1910 | mbedtls_md5_starts_ret( &md5 ); |
| 1911 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1912 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); |
| 1913 | mbedtls_md5_update_ret( &md5, hash, 16 ); |
| 1914 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1915 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1916 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1917 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); |
| 1918 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1919 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1920 | mbedtls_sha1_starts_ret( &sha1 ); |
| 1921 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1922 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); |
| 1923 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); |
| 1924 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1925 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1926 | *hlen = 36; |
| 1927 | |
| 1928 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1929 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1930 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1931 | mbedtls_md5_free( &md5 ); |
| 1932 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1933 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1934 | return; |
| 1935 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1936 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1937 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1938 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1939 | void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl, |
| 1940 | unsigned char hash[36], |
| 1941 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1942 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1943 | mbedtls_md5_context md5; |
| 1944 | mbedtls_sha1_context sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1945 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1946 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1947 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1948 | mbedtls_md5_init( &md5 ); |
| 1949 | mbedtls_sha1_init( &sha1 ); |
| 1950 | |
| 1951 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1952 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1953 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1954 | mbedtls_md5_finish_ret( &md5, hash ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1955 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1956 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1957 | *hlen = 36; |
| 1958 | |
| 1959 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1960 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1961 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1962 | mbedtls_md5_free( &md5 ); |
| 1963 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1964 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1965 | return; |
| 1966 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1967 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1968 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1969 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1970 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1971 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 1972 | unsigned char hash[32], |
| 1973 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1974 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1975 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1976 | size_t hash_size; |
| 1977 | psa_status_t status; |
| 1978 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 1979 | |
| 1980 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 1981 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 1982 | if( status != PSA_SUCCESS ) |
| 1983 | { |
| 1984 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1985 | return; |
| 1986 | } |
| 1987 | |
| 1988 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 1989 | if( status != PSA_SUCCESS ) |
| 1990 | { |
| 1991 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1992 | return; |
| 1993 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1994 | |
| 1995 | *hlen = 32; |
| 1996 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1997 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1998 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1999 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2000 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2001 | mbedtls_sha256_init( &sha256 ); |
| 2002 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2003 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2004 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2005 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2006 | mbedtls_sha256_finish_ret( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2007 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 2008 | *hlen = 32; |
| 2009 | |
| 2010 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2011 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2012 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2013 | mbedtls_sha256_free( &sha256 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2014 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2015 | return; |
| 2016 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2017 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2018 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2019 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 2020 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 2021 | unsigned char hash[48], |
| 2022 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2023 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2024 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2025 | size_t hash_size; |
| 2026 | psa_status_t status; |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2027 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2028 | |
| 2029 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2030 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2031 | if( status != PSA_SUCCESS ) |
| 2032 | { |
| 2033 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 2034 | return; |
| 2035 | } |
| 2036 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2037 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2038 | if( status != PSA_SUCCESS ) |
| 2039 | { |
| 2040 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 2041 | return; |
| 2042 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 2043 | |
| 2044 | *hlen = 48; |
| 2045 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2046 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 2047 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2048 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2049 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2050 | mbedtls_sha512_init( &sha512 ); |
| 2051 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2052 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2053 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2054 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2055 | mbedtls_sha512_finish_ret( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2056 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 2057 | *hlen = 48; |
| 2058 | |
| 2059 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2060 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2061 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2062 | mbedtls_sha512_free( &sha512 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2063 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2064 | return; |
| 2065 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2066 | #endif /* MBEDTLS_SHA512_C */ |
| 2067 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2068 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2069 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 2070 | 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] | 2071 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2072 | unsigned char *p = ssl->handshake->premaster; |
| 2073 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2074 | const unsigned char *psk = ssl->conf->psk; |
| 2075 | size_t psk_len = ssl->conf->psk_len; |
| 2076 | |
| 2077 | /* If the psk callback was called, use its result */ |
| 2078 | if( ssl->handshake->psk != NULL ) |
| 2079 | { |
| 2080 | psk = ssl->handshake->psk; |
| 2081 | psk_len = ssl->handshake->psk_len; |
| 2082 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2083 | |
| 2084 | /* |
| 2085 | * PMS = struct { |
| 2086 | * opaque other_secret<0..2^16-1>; |
| 2087 | * opaque psk<0..2^16-1>; |
| 2088 | * }; |
| 2089 | * with "other_secret" depending on the particular key exchange |
| 2090 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2091 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 2092 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2093 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2094 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2095 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2096 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2097 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 2098 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2099 | |
| 2100 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 2101 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2102 | |
| 2103 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2104 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2105 | } |
| 2106 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2107 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 2108 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 2109 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2110 | { |
| 2111 | /* |
| 2112 | * other_secret already set by the ClientKeyExchange message, |
| 2113 | * and is 48 bytes long |
| 2114 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 2115 | if( end - p < 2 ) |
| 2116 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2117 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2118 | *p++ = 0; |
| 2119 | *p++ = 48; |
| 2120 | p += 48; |
| 2121 | } |
| 2122 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2123 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 2124 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 2125 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2126 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2127 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 2128 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2129 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 2130 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2131 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 2132 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2133 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2134 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2135 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2136 | return( ret ); |
| 2137 | } |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 2138 | *(p++) = (unsigned char)( len >> 8 ); |
| 2139 | *(p++) = (unsigned char)( len ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2140 | p += len; |
| 2141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2142 | 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] | 2143 | } |
| 2144 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2145 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 2146 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 2147 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2148 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2149 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2150 | size_t zlen; |
| 2151 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2152 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2153 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2154 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2155 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2156 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2157 | return( ret ); |
| 2158 | } |
| 2159 | |
| 2160 | *(p++) = (unsigned char)( zlen >> 8 ); |
| 2161 | *(p++) = (unsigned char)( zlen ); |
| 2162 | p += zlen; |
| 2163 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2164 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 2165 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2166 | } |
| 2167 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2168 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2169 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2170 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2171 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2175 | if( end - p < 2 ) |
| 2176 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 2177 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2178 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 2179 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2180 | |
| 2181 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 2182 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2183 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2184 | memcpy( p, psk, psk_len ); |
| 2185 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2186 | |
| 2187 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 2188 | |
| 2189 | return( 0 ); |
| 2190 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2191 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2192 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2193 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2194 | /* |
| 2195 | * SSLv3.0 MAC functions |
| 2196 | */ |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2197 | #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] | 2198 | static void ssl_mac( mbedtls_md_context_t *md_ctx, |
| 2199 | const unsigned char *secret, |
| 2200 | const unsigned char *buf, size_t len, |
| 2201 | const unsigned char *ctr, int type, |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2202 | unsigned char out[SSL_MAC_MAX_BYTES] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2203 | { |
| 2204 | unsigned char header[11]; |
| 2205 | unsigned char padding[48]; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 2206 | int padlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2207 | int md_size = mbedtls_md_get_size( md_ctx->md_info ); |
| 2208 | int md_type = mbedtls_md_get_type( md_ctx->md_info ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2209 | |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 2210 | /* Only MD5 and SHA-1 supported */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2211 | if( md_type == MBEDTLS_MD_MD5 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2212 | padlen = 48; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 2213 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2214 | padlen = 40; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2215 | |
| 2216 | memcpy( header, ctr, 8 ); |
| 2217 | header[ 8] = (unsigned char) type; |
| 2218 | header[ 9] = (unsigned char)( len >> 8 ); |
| 2219 | header[10] = (unsigned char)( len ); |
| 2220 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2221 | memset( padding, 0x36, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2222 | mbedtls_md_starts( md_ctx ); |
| 2223 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 2224 | mbedtls_md_update( md_ctx, padding, padlen ); |
| 2225 | mbedtls_md_update( md_ctx, header, 11 ); |
| 2226 | mbedtls_md_update( md_ctx, buf, len ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 2227 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2228 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2229 | memset( padding, 0x5C, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2230 | mbedtls_md_starts( md_ctx ); |
| 2231 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 2232 | mbedtls_md_update( md_ctx, padding, padlen ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 2233 | mbedtls_md_update( md_ctx, out, md_size ); |
| 2234 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 2235 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2236 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 2237 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2238 | /* The function below is only used in the Lucky 13 counter-measure in |
Hanno Becker | b2ca87d | 2018-10-18 15:43:13 +0100 | [diff] [blame] | 2239 | * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2240 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \ |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2241 | ( defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2242 | defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2243 | defined(MBEDTLS_SSL_PROTO_TLS1_2) ) |
| 2244 | /* This function makes sure every byte in the memory region is accessed |
| 2245 | * (in ascending addresses order) */ |
| 2246 | static void ssl_read_memory( unsigned char *p, size_t len ) |
| 2247 | { |
| 2248 | unsigned char acc = 0; |
| 2249 | volatile unsigned char force; |
| 2250 | |
| 2251 | for( ; len != 0; p++, len-- ) |
| 2252 | acc ^= *p; |
| 2253 | |
| 2254 | force = acc; |
| 2255 | (void) force; |
| 2256 | } |
| 2257 | #endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */ |
| 2258 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2259 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2260 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2261 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2262 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2263 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d3f8c79 | 2019-05-20 15:06:12 +0100 | [diff] [blame] | 2264 | /* This functions transforms a DTLS plaintext fragment and a record content |
| 2265 | * type into an instance of the DTLSInnerPlaintext structure: |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2266 | * |
| 2267 | * struct { |
| 2268 | * opaque content[DTLSPlaintext.length]; |
| 2269 | * ContentType real_type; |
| 2270 | * uint8 zeros[length_of_padding]; |
| 2271 | * } DTLSInnerPlaintext; |
| 2272 | * |
| 2273 | * Input: |
| 2274 | * - `content`: The beginning of the buffer holding the |
| 2275 | * plaintext to be wrapped. |
| 2276 | * - `*content_size`: The length of the plaintext in Bytes. |
| 2277 | * - `max_len`: The number of Bytes available starting from |
| 2278 | * `content`. This must be `>= *content_size`. |
| 2279 | * - `rec_type`: The desired record content type. |
| 2280 | * |
| 2281 | * Output: |
| 2282 | * - `content`: The beginning of the resulting DTLSInnerPlaintext structure. |
| 2283 | * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure. |
| 2284 | * |
| 2285 | * Returns: |
| 2286 | * - `0` on success. |
| 2287 | * - A negative error code if `max_len` didn't offer enough space |
| 2288 | * for the expansion. |
| 2289 | */ |
| 2290 | static int ssl_cid_build_inner_plaintext( unsigned char *content, |
| 2291 | size_t *content_size, |
| 2292 | size_t remaining, |
| 2293 | uint8_t rec_type ) |
| 2294 | { |
| 2295 | size_t len = *content_size; |
Hanno Becker | b9ec44f | 2019-05-13 15:31:17 +0100 | [diff] [blame] | 2296 | size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY - |
| 2297 | ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) % |
| 2298 | MBEDTLS_SSL_CID_PADDING_GRANULARITY; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2299 | |
| 2300 | /* Write real content type */ |
| 2301 | if( remaining == 0 ) |
| 2302 | return( -1 ); |
| 2303 | content[ len ] = rec_type; |
| 2304 | len++; |
| 2305 | remaining--; |
| 2306 | |
| 2307 | if( remaining < pad ) |
| 2308 | return( -1 ); |
| 2309 | memset( content + len, 0, pad ); |
| 2310 | len += pad; |
| 2311 | remaining -= pad; |
| 2312 | |
| 2313 | *content_size = len; |
| 2314 | return( 0 ); |
| 2315 | } |
| 2316 | |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2317 | /* This function parses a DTLSInnerPlaintext structure. |
| 2318 | * See ssl_cid_build_inner_plaintext() for details. */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2319 | static int ssl_cid_parse_inner_plaintext( unsigned char const *content, |
| 2320 | size_t *content_size, |
| 2321 | uint8_t *rec_type ) |
| 2322 | { |
| 2323 | size_t remaining = *content_size; |
| 2324 | |
| 2325 | /* Determine length of padding by skipping zeroes from the back. */ |
| 2326 | do |
| 2327 | { |
| 2328 | if( remaining == 0 ) |
| 2329 | return( -1 ); |
| 2330 | remaining--; |
| 2331 | } while( content[ remaining ] == 0 ); |
| 2332 | |
| 2333 | *content_size = remaining; |
| 2334 | *rec_type = content[ remaining ]; |
| 2335 | |
| 2336 | return( 0 ); |
| 2337 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2338 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2339 | |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2340 | /* `add_data` must have size 13 Bytes if the CID extension is disabled, |
Hanno Becker | c4a190b | 2019-05-08 18:15:21 +0100 | [diff] [blame] | 2341 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2342 | static void ssl_extract_add_data_from_record( unsigned char* add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2343 | size_t *add_data_len, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2344 | mbedtls_record *rec ) |
| 2345 | { |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2346 | /* Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2347 | * |
| 2348 | * additional_data = seq_num + TLSCompressed.type + |
| 2349 | * TLSCompressed.version + TLSCompressed.length; |
| 2350 | * |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2351 | * For the CID extension, this is extended as follows |
| 2352 | * (quoting draft-ietf-tls-dtls-connection-id-05, |
| 2353 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2354 | * |
| 2355 | * additional_data = seq_num + DTLSPlaintext.type + |
| 2356 | * DTLSPlaintext.version + |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2357 | * cid + |
| 2358 | * cid_length + |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2359 | * length_of_DTLSInnerPlaintext; |
| 2360 | */ |
| 2361 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2362 | memcpy( add_data, rec->ctr, sizeof( rec->ctr ) ); |
| 2363 | add_data[8] = rec->type; |
Hanno Becker | edb24f8 | 2019-05-20 15:01:46 +0100 | [diff] [blame] | 2364 | memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) ); |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2365 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2366 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2367 | if( rec->cid_len != 0 ) |
| 2368 | { |
| 2369 | memcpy( add_data + 11, rec->cid, rec->cid_len ); |
| 2370 | add_data[11 + rec->cid_len + 0] = rec->cid_len; |
| 2371 | add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF; |
| 2372 | add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF; |
| 2373 | *add_data_len = 13 + 1 + rec->cid_len; |
| 2374 | } |
| 2375 | else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2376 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2377 | { |
| 2378 | add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF; |
| 2379 | add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF; |
| 2380 | *add_data_len = 13; |
| 2381 | } |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2384 | int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl, |
| 2385 | mbedtls_ssl_transform *transform, |
| 2386 | mbedtls_record *rec, |
| 2387 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2388 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2389 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2390 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2391 | int auth_done = 0; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2392 | unsigned char * data; |
Hanno Becker | 92fb4fa | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2393 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2394 | size_t add_data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2395 | size_t post_avail; |
| 2396 | |
| 2397 | /* The SSL context is only used for debugging purposes! */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2398 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 2399 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2400 | ((void) ssl); |
| 2401 | #endif |
| 2402 | |
| 2403 | /* The PRNG is used for dynamic IV generation that's used |
| 2404 | * for CBC transformations in TLS 1.1 and TLS 1.2. */ |
| 2405 | #if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
| 2406 | ( defined(MBEDTLS_AES_C) || \ |
| 2407 | defined(MBEDTLS_ARIA_C) || \ |
| 2408 | defined(MBEDTLS_CAMELLIA_C) ) && \ |
| 2409 | ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) ) |
| 2410 | ((void) f_rng); |
| 2411 | ((void) p_rng); |
| 2412 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2413 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2414 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2415 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2416 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2417 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2418 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) ); |
| 2419 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2420 | } |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2421 | if( rec == NULL |
| 2422 | || rec->buf == NULL |
| 2423 | || rec->buf_len < rec->data_offset |
| 2424 | || rec->buf_len - rec->data_offset < rec->data_len |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2425 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2426 | || rec->cid_len != 0 |
| 2427 | #endif |
| 2428 | ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2429 | { |
| 2430 | 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] | 2431 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2432 | } |
| 2433 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2434 | data = rec->buf + rec->data_offset; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2435 | 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] | 2436 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2437 | data, rec->data_len ); |
| 2438 | |
| 2439 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ); |
| 2440 | |
| 2441 | if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 2442 | { |
| 2443 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d", |
| 2444 | (unsigned) rec->data_len, |
| 2445 | MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 2446 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2447 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 2448 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2449 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2450 | /* |
| 2451 | * Add CID information |
| 2452 | */ |
| 2453 | rec->cid_len = transform->out_cid_len; |
| 2454 | memcpy( rec->cid, transform->out_cid, transform->out_cid_len ); |
| 2455 | MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len ); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2456 | |
| 2457 | if( rec->cid_len != 0 ) |
| 2458 | { |
| 2459 | /* |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2460 | * Wrap plaintext into DTLSInnerPlaintext structure. |
| 2461 | * See ssl_cid_build_inner_plaintext() for more information. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2462 | * |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2463 | * Note that this changes `rec->data_len`, and hence |
| 2464 | * `post_avail` needs to be recalculated afterwards. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2465 | */ |
| 2466 | if( ssl_cid_build_inner_plaintext( data, |
| 2467 | &rec->data_len, |
| 2468 | post_avail, |
| 2469 | rec->type ) != 0 ) |
| 2470 | { |
| 2471 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2472 | } |
| 2473 | |
| 2474 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 2475 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2476 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2477 | |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2478 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); |
| 2479 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2480 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2481 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2482 | */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2483 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2484 | if( mode == MBEDTLS_MODE_STREAM || |
| 2485 | ( mode == MBEDTLS_MODE_CBC |
| 2486 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2487 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2488 | #endif |
| 2489 | ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2490 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2491 | if( post_avail < transform->maclen ) |
| 2492 | { |
| 2493 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2494 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2495 | } |
| 2496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2497 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2498 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2499 | { |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2500 | unsigned char mac[SSL_MAC_MAX_BYTES]; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2501 | ssl_mac( &transform->md_ctx_enc, transform->mac_enc, |
| 2502 | data, rec->data_len, rec->ctr, rec->type, mac ); |
| 2503 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2504 | } |
| 2505 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2506 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2507 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2508 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2509 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2510 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2511 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2512 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2513 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2514 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2515 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2516 | add_data_len ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2517 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2518 | data, rec->data_len ); |
| 2519 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2520 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
| 2521 | |
| 2522 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2523 | } |
| 2524 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2525 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2526 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2527 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2528 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2529 | } |
| 2530 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2531 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len, |
| 2532 | transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2533 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2534 | rec->data_len += transform->maclen; |
| 2535 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2536 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2537 | } |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2538 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2539 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2540 | /* |
| 2541 | * Encrypt |
| 2542 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2543 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2544 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2545 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2546 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2547 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2548 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2549 | "including %d bytes of padding", |
| 2550 | rec->data_len, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2551 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2552 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2553 | transform->iv_enc, transform->ivlen, |
| 2554 | data, rec->data_len, |
| 2555 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2556 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2557 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2558 | return( ret ); |
| 2559 | } |
| 2560 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2561 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2562 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2563 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2564 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2565 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2566 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2567 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2568 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2569 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2570 | #if defined(MBEDTLS_GCM_C) || \ |
| 2571 | defined(MBEDTLS_CCM_C) || \ |
| 2572 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2573 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2574 | mode == MBEDTLS_MODE_CCM || |
| 2575 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2576 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2577 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2578 | unsigned char iv[12]; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2579 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2580 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2581 | /* Check that there's space for both the authentication tag |
| 2582 | * and the explicit IV before and after the record content. */ |
| 2583 | if( post_avail < transform->taglen || |
| 2584 | rec->data_offset < explicit_iv_len ) |
| 2585 | { |
| 2586 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2587 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2588 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2589 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2590 | /* |
| 2591 | * Generate IV |
| 2592 | */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2593 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2594 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2595 | /* GCM and CCM: fixed || explicit (=seqnum) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2596 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2597 | memcpy( iv + transform->fixed_ivlen, rec->ctr, |
| 2598 | explicit_iv_len ); |
| 2599 | /* Prefix record content with explicit IV. */ |
| 2600 | memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2601 | } |
| 2602 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 2603 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2604 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2605 | unsigned char i; |
| 2606 | |
| 2607 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
| 2608 | |
| 2609 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2610 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2611 | } |
| 2612 | else |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2613 | { |
| 2614 | /* 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] | 2615 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2616 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2617 | } |
| 2618 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2619 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2620 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2621 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)", |
| 2622 | iv, transform->ivlen ); |
| 2623 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2624 | data - explicit_iv_len, explicit_iv_len ); |
| 2625 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2626 | add_data, add_data_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2627 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2628 | "including 0 bytes of padding", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2629 | rec->data_len ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2630 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2631 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2632 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2633 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2634 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2635 | if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2636 | iv, transform->ivlen, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2637 | add_data, add_data_len, /* add data */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2638 | data, rec->data_len, /* source */ |
| 2639 | data, &rec->data_len, /* destination */ |
| 2640 | data + rec->data_len, transform->taglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2641 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2642 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2643 | return( ret ); |
| 2644 | } |
| 2645 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2646 | MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", |
| 2647 | data + rec->data_len, transform->taglen ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2648 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2649 | rec->data_len += transform->taglen + explicit_iv_len; |
| 2650 | rec->data_offset -= explicit_iv_len; |
| 2651 | post_avail -= transform->taglen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2652 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2653 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2654 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2655 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2656 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2657 | ( 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] | 2658 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2659 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2660 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2661 | size_t padlen, i; |
| 2662 | size_t olen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2663 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2664 | /* Currently we're always using minimal padding |
| 2665 | * (up to 255 bytes would be allowed). */ |
| 2666 | padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen; |
| 2667 | if( padlen == transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2668 | padlen = 0; |
| 2669 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2670 | /* Check there's enough space in the buffer for the padding. */ |
| 2671 | if( post_avail < padlen + 1 ) |
| 2672 | { |
| 2673 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2674 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2675 | } |
| 2676 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2677 | for( i = 0; i <= padlen; i++ ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2678 | data[rec->data_len + i] = (unsigned char) padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2679 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2680 | rec->data_len += padlen + 1; |
| 2681 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2682 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2683 | #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] | 2684 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2685 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per |
| 2686 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2687 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2688 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2689 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2690 | if( f_rng == NULL ) |
| 2691 | { |
| 2692 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) ); |
| 2693 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2694 | } |
| 2695 | |
| 2696 | if( rec->data_offset < transform->ivlen ) |
| 2697 | { |
| 2698 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2699 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2700 | } |
| 2701 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2702 | /* |
| 2703 | * Generate IV |
| 2704 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2705 | ret = f_rng( p_rng, transform->iv_enc, transform->ivlen ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2706 | if( ret != 0 ) |
| 2707 | return( ret ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2708 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2709 | memcpy( data - transform->ivlen, transform->iv_enc, |
| 2710 | transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2711 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2712 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2713 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2715 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2716 | "including %d bytes of IV and %d bytes of padding", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2717 | rec->data_len, transform->ivlen, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2718 | padlen + 1 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2719 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2720 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2721 | transform->iv_enc, |
| 2722 | transform->ivlen, |
| 2723 | data, rec->data_len, |
| 2724 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2725 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2726 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2727 | return( ret ); |
| 2728 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2729 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2730 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2731 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2732 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2733 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2734 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2736 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2737 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2738 | { |
| 2739 | /* |
| 2740 | * Save IV in SSL3 and TLS1 |
| 2741 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2742 | memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv, |
| 2743 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2744 | } |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2745 | else |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2746 | #endif |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2747 | { |
| 2748 | data -= transform->ivlen; |
| 2749 | rec->data_offset -= transform->ivlen; |
| 2750 | rec->data_len += transform->ivlen; |
| 2751 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2753 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2754 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2755 | { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 2756 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2757 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2758 | /* |
| 2759 | * MAC(MAC_write_key, seq_num + |
| 2760 | * TLSCipherText.type + |
| 2761 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2762 | * length_of( (IV +) ENC(...) ) + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2763 | * IV + // except for TLS 1.0 |
| 2764 | * ENC(content + padding + padding_length)); |
| 2765 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2766 | |
| 2767 | if( post_avail < transform->maclen) |
| 2768 | { |
| 2769 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2770 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2771 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2772 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2773 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2774 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2775 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2776 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2777 | add_data_len ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2778 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2779 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2780 | add_data_len ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2781 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2782 | data, rec->data_len ); |
| 2783 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2784 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2785 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2786 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2787 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2788 | rec->data_len += transform->maclen; |
| 2789 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2790 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2791 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2792 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2793 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2794 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2795 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2796 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2797 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2798 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2799 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2800 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2801 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2802 | /* Make extra sure authentication was performed, exactly once */ |
| 2803 | if( auth_done != 1 ) |
| 2804 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2805 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2806 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2807 | } |
| 2808 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2809 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2810 | |
| 2811 | return( 0 ); |
| 2812 | } |
| 2813 | |
Hanno Becker | 605949f | 2019-07-12 08:23:59 +0100 | [diff] [blame] | 2814 | int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl, |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2815 | mbedtls_ssl_transform *transform, |
| 2816 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2817 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2818 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2819 | mbedtls_cipher_mode_t mode; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2820 | int ret, auth_done = 0; |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2821 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2822 | size_t padlen = 0, correct = 1; |
| 2823 | #endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2824 | unsigned char* data; |
Hanno Becker | 92fb4fa | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2825 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2826 | size_t add_data_len; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2827 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2828 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 2829 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2830 | ((void) ssl); |
| 2831 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2832 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2833 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2834 | if( rec == NULL || |
| 2835 | rec->buf == NULL || |
| 2836 | rec->buf_len < rec->data_offset || |
| 2837 | rec->buf_len - rec->data_offset < rec->data_len ) |
| 2838 | { |
| 2839 | 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] | 2840 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2841 | } |
| 2842 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2843 | data = rec->buf + rec->data_offset; |
| 2844 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2845 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2846 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2847 | /* |
| 2848 | * Match record's CID with incoming CID. |
| 2849 | */ |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2850 | if( rec->cid_len != transform->in_cid_len || |
| 2851 | memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) |
| 2852 | { |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 2853 | return( MBEDTLS_ERR_SSL_UNEXPECTED_CID ); |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2854 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2855 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2856 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2857 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2858 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2859 | { |
| 2860 | padlen = 0; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2861 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2862 | transform->iv_dec, |
| 2863 | transform->ivlen, |
| 2864 | data, rec->data_len, |
| 2865 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2866 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2867 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2868 | return( ret ); |
| 2869 | } |
| 2870 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2871 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2872 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2873 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2874 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2875 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2876 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2877 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2878 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2879 | #if defined(MBEDTLS_GCM_C) || \ |
| 2880 | defined(MBEDTLS_CCM_C) || \ |
| 2881 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2882 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2883 | mode == MBEDTLS_MODE_CCM || |
| 2884 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2885 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2886 | unsigned char iv[12]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2887 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2888 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2889 | /* |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2890 | * Prepare IV from explicit and implicit data. |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2891 | */ |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2892 | |
| 2893 | /* Check that there's enough space for the explicit IV |
| 2894 | * (at the beginning of the record) and the MAC (at the |
| 2895 | * end of the record). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2896 | if( rec->data_len < explicit_iv_len + transform->taglen ) |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2897 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2898 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2899 | "+ taglen (%d)", rec->data_len, |
| 2900 | explicit_iv_len, transform->taglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2901 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2902 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2903 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2904 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2905 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2906 | { |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2907 | /* GCM and CCM: fixed || explicit */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2908 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2909 | /* Fixed */ |
| 2910 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2911 | /* Explicit */ |
| 2912 | memcpy( iv + transform->fixed_ivlen, data, 8 ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2913 | } |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2914 | else |
| 2915 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2916 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 2917 | if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2918 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2919 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2920 | unsigned char i; |
| 2921 | |
| 2922 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2923 | |
| 2924 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2925 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2926 | } |
| 2927 | else |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2928 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2929 | { |
| 2930 | /* Reminder if we ever add an AEAD mode with a different size */ |
| 2931 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2932 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2933 | } |
| 2934 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2935 | /* Group changes to data, data_len, and add_data, because |
| 2936 | * add_data depends on data_len. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2937 | data += explicit_iv_len; |
| 2938 | rec->data_offset += explicit_iv_len; |
| 2939 | rec->data_len -= explicit_iv_len + transform->taglen; |
| 2940 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2941 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2942 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2943 | add_data, add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2944 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2945 | /* Because of the check above, we know that there are |
| 2946 | * explicit_iv_len Bytes preceeding data, and taglen |
| 2947 | * bytes following data + data_len. This justifies |
Hanno Becker | 2001665 | 2019-07-10 11:44:13 +0100 | [diff] [blame] | 2948 | * the debug message and the invocation of |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2949 | * mbedtls_cipher_auth_decrypt() below. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2950 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2951 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2952 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len, |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2953 | transform->taglen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2954 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2955 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2956 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2957 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2958 | if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec, |
| 2959 | iv, transform->ivlen, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2960 | add_data, add_data_len, |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2961 | data, rec->data_len, |
| 2962 | data, &olen, |
| 2963 | data + rec->data_len, |
| 2964 | transform->taglen ) ) != 0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2965 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2966 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2967 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2968 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 2969 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2970 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2971 | return( ret ); |
| 2972 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2973 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2974 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2975 | /* Double-check that AEAD decryption doesn't change content length. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2976 | if( olen != rec->data_len ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2977 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2978 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2979 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2980 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2981 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2982 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2983 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2984 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2985 | ( 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] | 2986 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2987 | { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2988 | size_t minlen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2989 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2990 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2991 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2992 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2993 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2994 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 2995 | { |
| 2996 | /* The ciphertext is prefixed with the CBC IV. */ |
| 2997 | minlen += transform->ivlen; |
| 2998 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2999 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3000 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3001 | /* Size considerations: |
| 3002 | * |
| 3003 | * - The CBC cipher text must not be empty and hence |
| 3004 | * at least of size transform->ivlen. |
| 3005 | * |
| 3006 | * Together with the potential IV-prefix, this explains |
| 3007 | * the first of the two checks below. |
| 3008 | * |
| 3009 | * - The record must contain a MAC, either in plain or |
| 3010 | * encrypted, depending on whether Encrypt-then-MAC |
| 3011 | * is used or not. |
| 3012 | * - If it is, the message contains the IV-prefix, |
| 3013 | * the CBC ciphertext, and the MAC. |
| 3014 | * - If it is not, the padded plaintext, and hence |
| 3015 | * the CBC ciphertext, has at least length maclen + 1 |
| 3016 | * because there is at least the padding length byte. |
| 3017 | * |
| 3018 | * As the CBC ciphertext is not empty, both cases give the |
| 3019 | * lower bound minlen + maclen + 1 on the record size, which |
| 3020 | * we test for in the second check below. |
| 3021 | */ |
| 3022 | if( rec->data_len < minlen + transform->ivlen || |
| 3023 | rec->data_len < minlen + transform->maclen + 1 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3024 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3025 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3026 | "+ 1 ) ( + expl IV )", rec->data_len, |
| 3027 | transform->ivlen, |
| 3028 | transform->maclen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3029 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3030 | } |
| 3031 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3032 | /* |
| 3033 | * Authenticate before decrypt if enabled |
| 3034 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3035 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3036 | if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3037 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 3038 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3040 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3041 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3042 | /* Update data_len in tandem with add_data. |
| 3043 | * |
| 3044 | * The subtraction is safe because of the previous check |
| 3045 | * data_len >= minlen + maclen + 1. |
| 3046 | * |
| 3047 | * Afterwards, we know that data + data_len is followed by at |
| 3048 | * least maclen Bytes, which justifies the call to |
| 3049 | * mbedtls_ssl_safer_memcmp() below. |
| 3050 | * |
| 3051 | * Further, we still know that data_len > minlen */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3052 | rec->data_len -= transform->maclen; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3053 | 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] | 3054 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3055 | /* Calculate expected MAC. */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3056 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
| 3057 | add_data_len ); |
| 3058 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 3059 | add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3060 | mbedtls_md_hmac_update( &transform->md_ctx_dec, |
| 3061 | data, rec->data_len ); |
| 3062 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
| 3063 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 3064 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3065 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, |
| 3066 | transform->maclen ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 3067 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3068 | transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3069 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3070 | /* Compare expected MAC with MAC at the end of the record. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3071 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 3072 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3073 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3074 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3075 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3076 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3077 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3078 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3079 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3080 | |
| 3081 | /* |
| 3082 | * Check length sanity |
| 3083 | */ |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3084 | |
| 3085 | /* We know from above that data_len > minlen >= 0, |
| 3086 | * so the following check in particular implies that |
| 3087 | * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3088 | if( rec->data_len % transform->ivlen != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3089 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3090 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3091 | rec->data_len, transform->ivlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3092 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3093 | } |
| 3094 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3095 | #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] | 3096 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3097 | * 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] | 3098 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3099 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3100 | { |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3101 | /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3102 | memcpy( transform->iv_dec, data, transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3103 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3104 | data += transform->ivlen; |
| 3105 | rec->data_offset += transform->ivlen; |
| 3106 | rec->data_len -= transform->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3107 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3108 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3109 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3110 | /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */ |
| 3111 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3112 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 3113 | transform->iv_dec, transform->ivlen, |
| 3114 | data, rec->data_len, data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 3115 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3116 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3117 | return( ret ); |
| 3118 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 3119 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3120 | /* Double-check that length hasn't changed during decryption. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3121 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3122 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3123 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3124 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3125 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 3126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3127 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3128 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3129 | { |
| 3130 | /* |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3131 | * Save IV in SSL3 and TLS1, where CBC decryption of consecutive |
| 3132 | * records is equivalent to CBC decryption of the concatenation |
| 3133 | * of the records; in other words, IVs are maintained across |
| 3134 | * record decryptions. |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3135 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3136 | memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv, |
| 3137 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3138 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3139 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3140 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3141 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 3142 | * subtracted at most minlen and maclen up to this point, |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3143 | * data_len > 0 (because of data_len % ivlen == 0, it's actually |
| 3144 | * >= ivlen ). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3145 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3146 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3147 | if( auth_done == 1 ) |
| 3148 | { |
| 3149 | correct *= ( rec->data_len >= padlen + 1 ); |
| 3150 | padlen *= ( rec->data_len >= padlen + 1 ); |
| 3151 | } |
| 3152 | else |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3153 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3154 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3155 | if( rec->data_len < transform->maclen + padlen + 1 ) |
| 3156 | { |
| 3157 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", |
| 3158 | rec->data_len, |
| 3159 | transform->maclen, |
| 3160 | padlen + 1 ) ); |
| 3161 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 3162 | #endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3163 | |
| 3164 | correct *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
| 3165 | padlen *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3166 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3167 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3168 | padlen++; |
| 3169 | |
| 3170 | /* Regardless of the validity of the padding, |
| 3171 | * we have data_len >= padlen here. */ |
| 3172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3173 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3174 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3175 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3176 | if( padlen > transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3177 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3178 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 3179 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3180 | "should be no more than %d", |
| 3181 | padlen, transform->ivlen ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 3182 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3183 | correct = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3184 | } |
| 3185 | } |
| 3186 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3187 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 3188 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3189 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3190 | if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3191 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3192 | /* The padding check involves a series of up to 256 |
| 3193 | * consecutive memory reads at the end of the record |
| 3194 | * plaintext buffer. In order to hide the length and |
| 3195 | * validity of the padding, always perform exactly |
| 3196 | * `min(256,plaintext_len)` reads (but take into account |
| 3197 | * only the last `padlen` bytes for the padding check). */ |
| 3198 | size_t pad_count = 0; |
| 3199 | size_t real_count = 0; |
| 3200 | volatile unsigned char* const check = data; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3201 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3202 | /* Index of first padding byte; it has been ensured above |
| 3203 | * that the subtraction is safe. */ |
| 3204 | size_t const padding_idx = rec->data_len - padlen; |
| 3205 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 3206 | size_t const start_idx = rec->data_len - num_checks; |
| 3207 | size_t idx; |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 3208 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3209 | for( idx = start_idx; idx < rec->data_len; idx++ ) |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 3210 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3211 | real_count |= ( idx >= padding_idx ); |
| 3212 | pad_count += real_count * ( check[idx] == padlen - 1 ); |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 3213 | } |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3214 | correct &= ( pad_count == padlen ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3216 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 3217 | if( padlen > 0 && correct == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3218 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 3219 | #endif |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3220 | padlen &= correct * 0x1FF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3221 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3222 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3223 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3224 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3225 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3226 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3227 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3228 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3229 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3230 | /* If the padding was found to be invalid, padlen == 0 |
| 3231 | * and the subtraction is safe. If the padding was found valid, |
| 3232 | * padlen hasn't been changed and the previous assertion |
| 3233 | * data_len >= padlen still holds. */ |
| 3234 | rec->data_len -= padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3235 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3236 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3237 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 3238 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3239 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3240 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3241 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3242 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3243 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 3244 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3245 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3246 | data, rec->data_len ); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 3247 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3248 | |
| 3249 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3250 | * Authenticate if not done yet. |
| 3251 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3252 | */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 3253 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3254 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3255 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 3256 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 3257 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3258 | /* If the initial value of padlen was such that |
| 3259 | * data_len < maclen + padlen + 1, then padlen |
| 3260 | * got reset to 1, and the initial check |
| 3261 | * data_len >= minlen + maclen + 1 |
| 3262 | * guarantees that at this point we still |
| 3263 | * have at least data_len >= maclen. |
| 3264 | * |
| 3265 | * If the initial value of padlen was such that |
| 3266 | * data_len >= maclen + padlen + 1, then we have |
| 3267 | * subtracted either padlen + 1 (if the padding was correct) |
| 3268 | * or 0 (if the padding was incorrect) since then, |
| 3269 | * hence data_len >= maclen in any case. |
| 3270 | */ |
| 3271 | rec->data_len -= transform->maclen; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3272 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3273 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3274 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3275 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3276 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3277 | ssl_mac( &transform->md_ctx_dec, |
| 3278 | transform->mac_dec, |
| 3279 | data, rec->data_len, |
| 3280 | rec->ctr, rec->type, |
| 3281 | mac_expect ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3282 | } |
| 3283 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3284 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 3285 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3286 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3287 | if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3288 | { |
| 3289 | /* |
| 3290 | * Process MAC and always update for padlen afterwards to make |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3291 | * total time independent of padlen. |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3292 | * |
| 3293 | * Known timing attacks: |
| 3294 | * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf) |
| 3295 | * |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3296 | * To compensate for different timings for the MAC calculation |
| 3297 | * depending on how much padding was removed (which is determined |
| 3298 | * by padlen), process extra_run more blocks through the hash |
| 3299 | * function. |
| 3300 | * |
| 3301 | * The formula in the paper is |
| 3302 | * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 ) |
| 3303 | * where L1 is the size of the header plus the decrypted message |
| 3304 | * plus CBC padding and L2 is the size of the header plus the |
| 3305 | * decrypted message. This is for an underlying hash function |
| 3306 | * with 64-byte blocks. |
| 3307 | * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values |
| 3308 | * correctly. We round down instead of up, so -56 is the correct |
| 3309 | * value for our calculations instead of -55. |
| 3310 | * |
Gilles Peskine | 1bd9d58 | 2018-06-04 11:58:44 +0200 | [diff] [blame] | 3311 | * Repeat the formula rather than defining a block_size variable. |
| 3312 | * This avoids requiring division by a variable at runtime |
| 3313 | * (which would be marginally less efficient and would require |
| 3314 | * linking an extra division function in some builds). |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3315 | */ |
| 3316 | size_t j, extra_run = 0; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3317 | unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE]; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3318 | |
| 3319 | /* |
| 3320 | * The next two sizes are the minimum and maximum values of |
| 3321 | * in_msglen over all padlen values. |
| 3322 | * |
| 3323 | * They're independent of padlen, since we previously did |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3324 | * data_len -= padlen. |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3325 | * |
| 3326 | * Note that max_len + maclen is never more than the buffer |
| 3327 | * length, as we previously did in_msglen -= maclen too. |
| 3328 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3329 | const size_t max_len = rec->data_len + padlen; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3330 | const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0; |
| 3331 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3332 | memset( tmp, 0, sizeof( tmp ) ); |
| 3333 | |
| 3334 | switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) ) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3335 | { |
Gilles Peskine | d0e55a4 | 2018-06-04 12:03:30 +0200 | [diff] [blame] | 3336 | #if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \ |
| 3337 | defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3338 | case MBEDTLS_MD_MD5: |
| 3339 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3340 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3341 | /* 8 bytes of message size, 64-byte compression blocks */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3342 | extra_run = |
| 3343 | ( add_data_len + rec->data_len + padlen + 8 ) / 64 - |
| 3344 | ( add_data_len + rec->data_len + 8 ) / 64; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3345 | break; |
| 3346 | #endif |
Gilles Peskine | a7fe25d | 2018-06-04 12:01:18 +0200 | [diff] [blame] | 3347 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3348 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3349 | /* 16 bytes of message size, 128-byte compression blocks */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3350 | extra_run = |
| 3351 | ( add_data_len + rec->data_len + padlen + 16 ) / 128 - |
| 3352 | ( add_data_len + rec->data_len + 16 ) / 128; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3353 | break; |
| 3354 | #endif |
| 3355 | default: |
Gilles Peskine | 5c38984 | 2018-06-04 12:02:43 +0200 | [diff] [blame] | 3356 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3357 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3358 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3359 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3360 | extra_run &= correct * 0xFF; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3361 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3362 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 3363 | add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3364 | mbedtls_md_hmac_update( &transform->md_ctx_dec, data, |
| 3365 | rec->data_len ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3366 | /* Make sure we access everything even when padlen > 0. This |
| 3367 | * makes the synchronisation requirements for just-in-time |
| 3368 | * Prime+Probe attacks much tighter and hopefully impractical. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3369 | ssl_read_memory( data + rec->data_len, padlen ); |
| 3370 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3371 | |
| 3372 | /* Call mbedtls_md_process at least once due to cache attacks |
| 3373 | * that observe whether md_process() was called of not */ |
Manuel Pégourié-Gonnard | 47fede0 | 2015-04-29 01:35:48 +0200 | [diff] [blame] | 3374 | for( j = 0; j < extra_run + 1; j++ ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3375 | mbedtls_md_process( &transform->md_ctx_dec, tmp ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3376 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3377 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3378 | |
| 3379 | /* Make sure we access all the memory that could contain the MAC, |
| 3380 | * before we check it in the next code block. This makes the |
| 3381 | * synchronisation requirements for just-in-time Prime+Probe |
| 3382 | * attacks much tighter and hopefully impractical. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3383 | ssl_read_memory( data + min_len, |
| 3384 | max_len - min_len + transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3385 | } |
| 3386 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3387 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3388 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3389 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3390 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3391 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3392 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3393 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3394 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3395 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen ); |
| 3396 | 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] | 3397 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3398 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3399 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 3400 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3401 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3402 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 3403 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3404 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3405 | correct = 0; |
| 3406 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3407 | auth_done++; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3408 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 3409 | |
| 3410 | /* |
| 3411 | * Finally check the correct flag |
| 3412 | */ |
| 3413 | if( correct == 0 ) |
| 3414 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 3415 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3416 | |
| 3417 | /* Make extra sure authentication was performed, exactly once */ |
| 3418 | if( auth_done != 1 ) |
| 3419 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3420 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3421 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3422 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3423 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3424 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3425 | if( rec->cid_len != 0 ) |
| 3426 | { |
| 3427 | ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len, |
| 3428 | &rec->type ); |
| 3429 | if( ret != 0 ) |
| 3430 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3431 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3432 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3433 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3434 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3435 | |
| 3436 | return( 0 ); |
| 3437 | } |
| 3438 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 3439 | #undef MAC_NONE |
| 3440 | #undef MAC_PLAINTEXT |
| 3441 | #undef MAC_CIPHERTEXT |
| 3442 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3443 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3444 | /* |
| 3445 | * Compression/decompression functions |
| 3446 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3447 | static int ssl_compress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3448 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3449 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3450 | unsigned char *msg_post = ssl->out_msg; |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3451 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3452 | size_t len_pre = ssl->out_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3453 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3454 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3455 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3456 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3457 | if( len_pre == 0 ) |
| 3458 | return( 0 ); |
| 3459 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3460 | memcpy( msg_pre, ssl->out_msg, len_pre ); |
| 3461 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3462 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3463 | ssl->out_msglen ) ); |
| 3464 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3465 | MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3466 | ssl->out_msg, ssl->out_msglen ); |
| 3467 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3468 | ssl->transform_out->ctx_deflate.next_in = msg_pre; |
| 3469 | ssl->transform_out->ctx_deflate.avail_in = len_pre; |
| 3470 | ssl->transform_out->ctx_deflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3471 | 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] | 3472 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3473 | ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3474 | if( ret != Z_OK ) |
| 3475 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3476 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) ); |
| 3477 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3480 | ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN - |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3481 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3482 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3483 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3484 | ssl->out_msglen ) ); |
| 3485 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3486 | MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3487 | ssl->out_msg, ssl->out_msglen ); |
| 3488 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3489 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3490 | |
| 3491 | return( 0 ); |
| 3492 | } |
| 3493 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3494 | static int ssl_decompress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3495 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3496 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3497 | unsigned char *msg_post = ssl->in_msg; |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3498 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3499 | size_t len_pre = ssl->in_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3500 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3501 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3502 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3503 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3504 | if( len_pre == 0 ) |
| 3505 | return( 0 ); |
| 3506 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3507 | memcpy( msg_pre, ssl->in_msg, len_pre ); |
| 3508 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3509 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3510 | ssl->in_msglen ) ); |
| 3511 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3512 | MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3513 | ssl->in_msg, ssl->in_msglen ); |
| 3514 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3515 | ssl->transform_in->ctx_inflate.next_in = msg_pre; |
| 3516 | ssl->transform_in->ctx_inflate.avail_in = len_pre; |
| 3517 | ssl->transform_in->ctx_inflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3518 | ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3519 | header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3520 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3521 | ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3522 | if( ret != Z_OK ) |
| 3523 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3524 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) ); |
| 3525 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3528 | ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3529 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3530 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3531 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3532 | ssl->in_msglen ) ); |
| 3533 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3534 | MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3535 | ssl->in_msg, ssl->in_msglen ); |
| 3536 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3537 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3538 | |
| 3539 | return( 0 ); |
| 3540 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3541 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3543 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3544 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3545 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3546 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3547 | static int ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3548 | { |
| 3549 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 3550 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3551 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3552 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3553 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3554 | unsigned char doublings = 1; |
| 3555 | |
| 3556 | while( ratio != 0 ) |
| 3557 | { |
| 3558 | ++doublings; |
| 3559 | ratio >>= 1; |
| 3560 | } |
| 3561 | |
| 3562 | if( ++ssl->renego_records_seen > doublings ) |
| 3563 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 3564 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3565 | return( 0 ); |
| 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | return( ssl_write_hello_request( ssl ) ); |
| 3570 | } |
| 3571 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3572 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3573 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3574 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3575 | * Fill the input message buffer by appending data to it. |
| 3576 | * 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] | 3577 | * |
| 3578 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 3579 | * available (from this read and/or a previous one). Otherwise, an error code |
| 3580 | * is returned (possibly EOF or WANT_READ). |
| 3581 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3582 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 3583 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 3584 | * since we always read a whole datagram at once. |
| 3585 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3586 | * 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] | 3587 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3588 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3589 | 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] | 3590 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3591 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3592 | size_t len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3593 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3594 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3595 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3596 | if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL ) |
| 3597 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3598 | 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] | 3599 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3600 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3601 | } |
| 3602 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3603 | 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] | 3604 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3605 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); |
| 3606 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3607 | } |
| 3608 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3609 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3610 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3611 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3612 | uint32_t timeout; |
| 3613 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3614 | /* Just to be sure */ |
| 3615 | if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) |
| 3616 | { |
| 3617 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3618 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3619 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3620 | } |
| 3621 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3622 | /* |
| 3623 | * The point is, we need to always read a full datagram at once, so we |
| 3624 | * sometimes read more then requested, and handle the additional data. |
| 3625 | * It could be the rest of the current record (while fetching the |
| 3626 | * header) and/or some other records in the same datagram. |
| 3627 | */ |
| 3628 | |
| 3629 | /* |
| 3630 | * Move to the next record in the already read datagram if applicable |
| 3631 | */ |
| 3632 | if( ssl->next_record_offset != 0 ) |
| 3633 | { |
| 3634 | if( ssl->in_left < ssl->next_record_offset ) |
| 3635 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3636 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3637 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | ssl->in_left -= ssl->next_record_offset; |
| 3641 | |
| 3642 | if( ssl->in_left != 0 ) |
| 3643 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3644 | 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] | 3645 | ssl->next_record_offset ) ); |
| 3646 | memmove( ssl->in_hdr, |
| 3647 | ssl->in_hdr + ssl->next_record_offset, |
| 3648 | ssl->in_left ); |
| 3649 | } |
| 3650 | |
| 3651 | ssl->next_record_offset = 0; |
| 3652 | } |
| 3653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3654 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3655 | ssl->in_left, nb_want ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3656 | |
| 3657 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3658 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3659 | */ |
| 3660 | if( nb_want <= ssl->in_left) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3661 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3662 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3663 | return( 0 ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3664 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3665 | |
| 3666 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 3667 | * 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] | 3668 | * are not at the beginning of a new record, the caller did something |
| 3669 | * wrong. |
| 3670 | */ |
| 3671 | if( ssl->in_left != 0 ) |
| 3672 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3673 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3674 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3675 | } |
| 3676 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3677 | /* |
| 3678 | * Don't even try to read if time's out already. |
| 3679 | * This avoids by-passing the timer when repeatedly receiving messages |
| 3680 | * that will end up being dropped. |
| 3681 | */ |
Hanno Becker | 7876d12 | 2020-02-05 10:39:31 +0000 | [diff] [blame] | 3682 | if( mbedtls_ssl_check_timer( ssl ) != 0 ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3683 | { |
| 3684 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3685 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3686 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3687 | else |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3688 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3689 | 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] | 3690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3691 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3692 | timeout = ssl->handshake->retransmit_timeout; |
| 3693 | else |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3694 | timeout = ssl->conf->read_timeout; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3696 | 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] | 3697 | |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3698 | if( ssl->f_recv_timeout != NULL ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3699 | ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, |
| 3700 | timeout ); |
| 3701 | else |
| 3702 | ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len ); |
| 3703 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3704 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3705 | |
| 3706 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3707 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3708 | } |
| 3709 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3710 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3711 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3712 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3713 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3715 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3716 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3717 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) |
| 3718 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3719 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3720 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3721 | } |
| 3722 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3723 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3724 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3725 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3726 | return( ret ); |
| 3727 | } |
| 3728 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3729 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3730 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3731 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3732 | else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3733 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3734 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3735 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3736 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3737 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3738 | return( ret ); |
| 3739 | } |
| 3740 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3741 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3742 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3743 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3744 | } |
| 3745 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3746 | if( ret < 0 ) |
| 3747 | return( ret ); |
| 3748 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3749 | ssl->in_left = ret; |
| 3750 | } |
| 3751 | else |
| 3752 | #endif |
| 3753 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3754 | 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] | 3755 | ssl->in_left, nb_want ) ); |
| 3756 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3757 | while( ssl->in_left < nb_want ) |
| 3758 | { |
| 3759 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3760 | |
Hanno Becker | 7876d12 | 2020-02-05 10:39:31 +0000 | [diff] [blame] | 3761 | if( mbedtls_ssl_check_timer( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3762 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
| 3763 | else |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3764 | { |
| 3765 | if( ssl->f_recv_timeout != NULL ) |
| 3766 | { |
| 3767 | ret = ssl->f_recv_timeout( ssl->p_bio, |
| 3768 | ssl->in_hdr + ssl->in_left, len, |
| 3769 | ssl->conf->read_timeout ); |
| 3770 | } |
| 3771 | else |
| 3772 | { |
| 3773 | ret = ssl->f_recv( ssl->p_bio, |
| 3774 | ssl->in_hdr + ssl->in_left, len ); |
| 3775 | } |
| 3776 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3777 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3778 | 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] | 3779 | ssl->in_left, nb_want ) ); |
| 3780 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3781 | |
| 3782 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3783 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3784 | |
| 3785 | if( ret < 0 ) |
| 3786 | return( ret ); |
| 3787 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3788 | if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3789 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3790 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3791 | ( "f_recv returned %d bytes but only %lu were requested", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3792 | ret, (unsigned long)len ) ); |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3793 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3794 | } |
| 3795 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3796 | ssl->in_left += ret; |
| 3797 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3800 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3801 | |
| 3802 | return( 0 ); |
| 3803 | } |
| 3804 | |
| 3805 | /* |
| 3806 | * Flush any data not yet written |
| 3807 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3808 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3809 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3810 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3811 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3812 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3813 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3814 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3815 | if( ssl->f_send == NULL ) |
| 3816 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3817 | 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] | 3818 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3819 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3820 | } |
| 3821 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3822 | /* Avoid incrementing counter if data is flushed */ |
| 3823 | if( ssl->out_left == 0 ) |
| 3824 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3825 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3826 | return( 0 ); |
| 3827 | } |
| 3828 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3829 | while( ssl->out_left > 0 ) |
| 3830 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3831 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d", |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 3832 | mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3833 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3834 | buf = ssl->out_hdr - ssl->out_left; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3835 | ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left ); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 3836 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3837 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3838 | |
| 3839 | if( ret <= 0 ) |
| 3840 | return( ret ); |
| 3841 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3842 | 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] | 3843 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3844 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3845 | ( "f_send returned %d bytes but only %lu bytes were sent", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3846 | ret, (unsigned long)ssl->out_left ) ); |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 3847 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3848 | } |
| 3849 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3850 | ssl->out_left -= ret; |
| 3851 | } |
| 3852 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3853 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3854 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3855 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3856 | ssl->out_hdr = ssl->out_buf; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3857 | } |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3858 | else |
| 3859 | #endif |
| 3860 | { |
| 3861 | ssl->out_hdr = ssl->out_buf + 8; |
| 3862 | } |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3863 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3864 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3865 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3866 | |
| 3867 | return( 0 ); |
| 3868 | } |
| 3869 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3870 | /* |
| 3871 | * Functions to handle the DTLS retransmission state machine |
| 3872 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3873 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3874 | /* |
| 3875 | * Append current handshake message to current outgoing flight |
| 3876 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3877 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3878 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3879 | mbedtls_ssl_flight_item *msg; |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3880 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) ); |
| 3881 | MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight", |
| 3882 | ssl->out_msg, ssl->out_msglen ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3883 | |
| 3884 | /* Allocate space for current message */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3885 | 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] | 3886 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3887 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3888 | sizeof( mbedtls_ssl_flight_item ) ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3889 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3890 | } |
| 3891 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3892 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3893 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3894 | 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] | 3895 | mbedtls_free( msg ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3896 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3897 | } |
| 3898 | |
| 3899 | /* Copy current handshake message with headers */ |
| 3900 | memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); |
| 3901 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3902 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3903 | msg->next = NULL; |
| 3904 | |
| 3905 | /* Append to the current flight */ |
| 3906 | if( ssl->handshake->flight == NULL ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3907 | ssl->handshake->flight = msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3908 | else |
| 3909 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3910 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3911 | while( cur->next != NULL ) |
| 3912 | cur = cur->next; |
| 3913 | cur->next = msg; |
| 3914 | } |
| 3915 | |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3916 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3917 | return( 0 ); |
| 3918 | } |
| 3919 | |
| 3920 | /* |
| 3921 | * Free the current flight of handshake messages |
| 3922 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3923 | static void ssl_flight_free( mbedtls_ssl_flight_item *flight ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3924 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3925 | mbedtls_ssl_flight_item *cur = flight; |
| 3926 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3927 | |
| 3928 | while( cur != NULL ) |
| 3929 | { |
| 3930 | next = cur->next; |
| 3931 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3932 | mbedtls_free( cur->p ); |
| 3933 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3934 | |
| 3935 | cur = next; |
| 3936 | } |
| 3937 | } |
| 3938 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3939 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3940 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3941 | #endif |
| 3942 | |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3943 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3944 | * Swap transform_out and out_ctr with the alternative ones |
| 3945 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3946 | static void ssl_swap_epochs( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3947 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3948 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3949 | unsigned char tmp_out_ctr[8]; |
| 3950 | |
| 3951 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) |
| 3952 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3953 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3954 | return; |
| 3955 | } |
| 3956 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3957 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3958 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3959 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3960 | tmp_transform = ssl->transform_out; |
| 3961 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 3962 | ssl->handshake->alt_transform_out = tmp_transform; |
| 3963 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3964 | /* Swap epoch + sequence_number */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3965 | memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 ); |
| 3966 | 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] | 3967 | memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3968 | |
| 3969 | /* Adjust to the newly activated transform */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3970 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3972 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3973 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3974 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3975 | 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] | 3976 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3977 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 3978 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3979 | } |
| 3980 | } |
| 3981 | #endif |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | /* |
| 3985 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3986 | */ |
| 3987 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) |
| 3988 | { |
| 3989 | int ret = 0; |
| 3990 | |
| 3991 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); |
| 3992 | |
| 3993 | ret = mbedtls_ssl_flight_transmit( ssl ); |
| 3994 | |
| 3995 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); |
| 3996 | |
| 3997 | return( ret ); |
| 3998 | } |
| 3999 | |
| 4000 | /* |
| 4001 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4002 | * |
| 4003 | * Need to remember the current message in case flush_output returns |
| 4004 | * 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] | 4005 | * 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] | 4006 | */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4007 | int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4008 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4009 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4010 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4011 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4012 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4013 | { |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 4014 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4015 | |
| 4016 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4017 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4018 | ssl_swap_epochs( ssl ); |
| 4019 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4020 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4021 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4022 | |
| 4023 | while( ssl->handshake->cur_msg != NULL ) |
| 4024 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4025 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4026 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4027 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 4028 | int const is_finished = |
| 4029 | ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4030 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ); |
| 4031 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 4032 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
| 4033 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
| 4034 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 4035 | /* Swap epochs before sending Finished: we can't do it after |
| 4036 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 4037 | * Must be done before copying, may change out_msg pointer */ |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 4038 | 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] | 4039 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4040 | 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] | 4041 | ssl_swap_epochs( ssl ); |
| 4042 | } |
| 4043 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4044 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 4045 | if( ret < 0 ) |
| 4046 | return( ret ); |
| 4047 | max_frag_len = (size_t) ret; |
| 4048 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4049 | /* CCS is copied as is, while HS messages may need fragmentation */ |
| 4050 | if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 4051 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4052 | if( max_frag_len == 0 ) |
| 4053 | { |
| 4054 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 4055 | return( ret ); |
| 4056 | |
| 4057 | continue; |
| 4058 | } |
| 4059 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4060 | memcpy( ssl->out_msg, cur->p, cur->len ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4061 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4062 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4063 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4064 | /* Update position inside current message */ |
| 4065 | ssl->handshake->cur_msg_p += cur->len; |
| 4066 | } |
| 4067 | else |
| 4068 | { |
| 4069 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 4070 | const size_t hs_len = cur->len - 12; |
| 4071 | const size_t frag_off = p - ( cur->p + 12 ); |
| 4072 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4073 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4074 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 4075 | 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] | 4076 | { |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 4077 | if( is_finished ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4078 | ssl_swap_epochs( ssl ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4079 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4080 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 4081 | return( ret ); |
| 4082 | |
| 4083 | continue; |
| 4084 | } |
| 4085 | max_hs_frag_len = max_frag_len - 12; |
| 4086 | |
| 4087 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
| 4088 | max_hs_frag_len : rem_len; |
| 4089 | |
| 4090 | if( frag_off == 0 && cur_hs_frag_len != hs_len ) |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 4091 | { |
| 4092 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)", |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4093 | (unsigned) cur_hs_frag_len, |
| 4094 | (unsigned) max_hs_frag_len ) ); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 4095 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 4096 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4097 | /* Messages are stored with handshake headers as if not fragmented, |
| 4098 | * copy beginning of headers then fill fragmentation fields. |
| 4099 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
| 4100 | memcpy( ssl->out_msg, cur->p, 6 ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4101 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4102 | ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); |
| 4103 | ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff ); |
| 4104 | ssl->out_msg[8] = ( ( frag_off ) & 0xff ); |
| 4105 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4106 | ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); |
| 4107 | ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff ); |
| 4108 | ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff ); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4109 | |
| 4110 | MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); |
| 4111 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 4112 | /* Copy the handshake message content and set records fields */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4113 | memcpy( ssl->out_msg + 12, p, cur_hs_frag_len ); |
| 4114 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4115 | ssl->out_msgtype = cur->type; |
| 4116 | |
| 4117 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4118 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | /* If done with the current message move to the next one if any */ |
| 4122 | if( ssl->handshake->cur_msg_p >= cur->p + cur->len ) |
| 4123 | { |
| 4124 | if( cur->next != NULL ) |
| 4125 | { |
| 4126 | ssl->handshake->cur_msg = cur->next; |
| 4127 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
| 4128 | } |
| 4129 | else |
| 4130 | { |
| 4131 | ssl->handshake->cur_msg = NULL; |
| 4132 | ssl->handshake->cur_msg_p = NULL; |
| 4133 | } |
| 4134 | } |
| 4135 | |
| 4136 | /* Actually send the message out */ |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 4137 | if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4138 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4139 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4140 | return( ret ); |
| 4141 | } |
| 4142 | } |
| 4143 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4144 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 4145 | return( ret ); |
| 4146 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4147 | /* Update state and set timer */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4148 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 4149 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 4150 | else |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 4151 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4152 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 4153 | mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 4154 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4155 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4156 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4157 | |
| 4158 | return( 0 ); |
| 4159 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4160 | |
| 4161 | /* |
| 4162 | * To be called when the last message of an incoming flight is received. |
| 4163 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4164 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4165 | { |
| 4166 | /* We won't need to resend that one any more */ |
| 4167 | ssl_flight_free( ssl->handshake->flight ); |
| 4168 | ssl->handshake->flight = NULL; |
| 4169 | ssl->handshake->cur_msg = NULL; |
| 4170 | |
| 4171 | /* The next incoming flight will start with this msg_seq */ |
| 4172 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 4173 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4174 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 4175 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4176 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4177 | /* Clear future message buffering structure. */ |
| 4178 | ssl_buffering_free( ssl ); |
| 4179 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 4180 | /* Cancel timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 4181 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4183 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4184 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4185 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4186 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4187 | } |
| 4188 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4189 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4190 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4191 | |
| 4192 | /* |
| 4193 | * To be called when the last message of an outgoing flight is send. |
| 4194 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4195 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4196 | { |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 4197 | ssl_reset_retransmit_timeout( ssl ); |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 4198 | mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4199 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4200 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4201 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4202 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4203 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4204 | } |
| 4205 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4206 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4207 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4208 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4209 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4210 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4211 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4212 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4213 | |
| 4214 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4215 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4216 | * |
| 4217 | * - fill in handshake headers |
| 4218 | * - update handshake checksum |
| 4219 | * - DTLS: save message for resending |
| 4220 | * - then pass to the record layer |
| 4221 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4222 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 4223 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4224 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4225 | * Inputs: |
| 4226 | * - ssl->out_msglen: 4 + actual handshake message len |
| 4227 | * (4 is the size of handshake headers for TLS) |
| 4228 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 4229 | * - ssl->out_msg + 4: the handshake message body |
| 4230 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 4231 | * 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] | 4232 | * - ssl->out_msglen: the length of the record contents |
| 4233 | * (including handshake headers but excluding record headers) |
| 4234 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4235 | */ |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4236 | int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4237 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4238 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4239 | const size_t hs_len = ssl->out_msglen - 4; |
| 4240 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4241 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4242 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) ); |
| 4243 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4244 | /* |
| 4245 | * Sanity checks |
| 4246 | */ |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4247 | if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4248 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 4249 | { |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4250 | /* In SSLv3, the client might send a NoCertificate alert. */ |
| 4251 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
| 4252 | if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 4253 | ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 4254 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ) |
| 4255 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 4256 | { |
| 4257 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4258 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4259 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4260 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4261 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 4262 | /* Whenever we send anything different from a |
| 4263 | * HelloRequest we should be in a handshake - double check. */ |
| 4264 | if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4265 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) && |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4266 | ssl->handshake == NULL ) |
| 4267 | { |
| 4268 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4269 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4270 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4271 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4272 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4273 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4274 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4275 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4276 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4277 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4278 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4279 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4280 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4281 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 4282 | /* Double-check that we did not exceed the bounds |
| 4283 | * of the outgoing record buffer. |
| 4284 | * This should never fail as the various message |
| 4285 | * writing functions must obey the bounds of the |
| 4286 | * outgoing record buffer, but better be safe. |
| 4287 | * |
| 4288 | * Note: We deliberately do not check for the MTU or MFL here. |
| 4289 | */ |
| 4290 | if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 4291 | { |
| 4292 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: " |
| 4293 | "size %u, maximum %u", |
| 4294 | (unsigned) ssl->out_msglen, |
| 4295 | (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 4296 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4297 | } |
| 4298 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4299 | /* |
| 4300 | * Fill handshake headers |
| 4301 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4302 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4303 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4304 | ssl->out_msg[1] = (unsigned char)( hs_len >> 16 ); |
| 4305 | ssl->out_msg[2] = (unsigned char)( hs_len >> 8 ); |
| 4306 | ssl->out_msg[3] = (unsigned char)( hs_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4307 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4308 | /* |
| 4309 | * DTLS has additional fields in the Handshake layer, |
| 4310 | * between the length field and the actual payload: |
| 4311 | * uint16 message_seq; |
| 4312 | * uint24 fragment_offset; |
| 4313 | * uint24 fragment_length; |
| 4314 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4315 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4316 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4317 | { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4318 | /* Make room for the additional DTLS fields */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4319 | if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 ) |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4320 | { |
| 4321 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: " |
| 4322 | "size %u, maximum %u", |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4323 | (unsigned) ( hs_len ), |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4324 | (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) ); |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4325 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4326 | } |
| 4327 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4328 | 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] | 4329 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4330 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4331 | /* Write message_seq and update it, except for HelloRequest */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4332 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4333 | { |
Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 4334 | ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; |
| 4335 | ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF; |
| 4336 | ++( ssl->handshake->out_msg_seq ); |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4337 | } |
| 4338 | else |
| 4339 | { |
| 4340 | ssl->out_msg[4] = 0; |
| 4341 | ssl->out_msg[5] = 0; |
| 4342 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4343 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4344 | /* Handshake hashes are computed without fragmentation, |
| 4345 | * 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] | 4346 | memset( ssl->out_msg + 6, 0x00, 3 ); |
| 4347 | memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4348 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4349 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4350 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 4351 | /* Update running hashes of handshake messages seen */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4352 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
| 4353 | ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4356 | /* 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] | 4357 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4358 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 4359 | ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4360 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4361 | { |
| 4362 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) |
| 4363 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4364 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4365 | return( ret ); |
| 4366 | } |
| 4367 | } |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4368 | else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4369 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4370 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4371 | 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] | 4372 | { |
| 4373 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 4374 | return( ret ); |
| 4375 | } |
| 4376 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4377 | |
| 4378 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) ); |
| 4379 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4380 | return( 0 ); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | /* |
| 4384 | * Record layer functions |
| 4385 | */ |
| 4386 | |
| 4387 | /* |
| 4388 | * Write current record. |
| 4389 | * |
| 4390 | * Uses: |
| 4391 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 4392 | * - ssl->out_msglen: length of the record content (excl headers) |
| 4393 | * - ssl->out_msg: record content |
| 4394 | */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4395 | 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] | 4396 | { |
| 4397 | int ret, done = 0; |
| 4398 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4399 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4400 | |
| 4401 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4402 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4403 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4404 | if( ssl->transform_out != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4405 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4406 | { |
| 4407 | if( ( ret = ssl_compress_buf( ssl ) ) != 0 ) |
| 4408 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4409 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4410 | return( ret ); |
| 4411 | } |
| 4412 | |
| 4413 | len = ssl->out_msglen; |
| 4414 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4415 | #endif /*MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4417 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 4418 | if( mbedtls_ssl_hw_record_write != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4419 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4420 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4422 | ret = mbedtls_ssl_hw_record_write( ssl ); |
| 4423 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4424 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4425 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret ); |
| 4426 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4427 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 4428 | |
| 4429 | if( ret == 0 ) |
| 4430 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4431 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4432 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4433 | if( !done ) |
| 4434 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4435 | unsigned i; |
| 4436 | size_t protected_record_size; |
| 4437 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4438 | /* Skip writing the record content type to after the encryption, |
| 4439 | * as it may change when using the CID extension. */ |
| 4440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4441 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4442 | ssl->conf->transport, ssl->out_hdr + 1 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4443 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 4444 | memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4445 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 4446 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4447 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4448 | if( ssl->transform_out != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4449 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4450 | mbedtls_record rec; |
| 4451 | |
| 4452 | rec.buf = ssl->out_iv; |
| 4453 | rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - |
| 4454 | ( ssl->out_iv - ssl->out_buf ); |
| 4455 | rec.data_len = ssl->out_msglen; |
| 4456 | rec.data_offset = ssl->out_msg - rec.buf; |
| 4457 | |
| 4458 | memcpy( &rec.ctr[0], ssl->out_ctr, 8 ); |
| 4459 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
| 4460 | ssl->conf->transport, rec.ver ); |
| 4461 | rec.type = ssl->out_msgtype; |
| 4462 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4463 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 4464 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4465 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4466 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4467 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 4468 | if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4469 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4470 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4471 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4472 | return( ret ); |
| 4473 | } |
| 4474 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4475 | if( rec.data_offset != 0 ) |
| 4476 | { |
| 4477 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4478 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4479 | } |
| 4480 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4481 | /* Update the record content type and CID. */ |
| 4482 | ssl->out_msgtype = rec.type; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4483 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4484 | memcpy( ssl->out_cid, rec.cid, rec.cid_len ); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4485 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 78f839d | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 4486 | ssl->out_msglen = len = rec.data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4487 | ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 4488 | ssl->out_len[1] = (unsigned char)( rec.data_len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 4491 | protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4492 | |
| 4493 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4494 | /* In case of DTLS, double-check that we don't exceed |
| 4495 | * the remaining space in the datagram. */ |
| 4496 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4497 | { |
Hanno Becker | 554b0af | 2018-08-22 20:33:41 +0100 | [diff] [blame] | 4498 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4499 | if( ret < 0 ) |
| 4500 | return( ret ); |
| 4501 | |
| 4502 | if( protected_record_size > (size_t) ret ) |
| 4503 | { |
| 4504 | /* Should never happen */ |
| 4505 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4506 | } |
| 4507 | } |
| 4508 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4509 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4510 | /* Now write the potentially updated record content type. */ |
| 4511 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
| 4512 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4513 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, " |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4514 | "version = [%d:%d], msglen = %d", |
| 4515 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 4516 | ssl->out_hdr[2], len ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4517 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4518 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4519 | ssl->out_hdr, protected_record_size ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4520 | |
| 4521 | ssl->out_left += protected_record_size; |
| 4522 | ssl->out_hdr += protected_record_size; |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4523 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4524 | |
Hanno Becker | dd77229 | 2020-02-05 10:38:31 +0000 | [diff] [blame] | 4525 | for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- ) |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 4526 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 4527 | break; |
| 4528 | |
| 4529 | /* The loop goes to its end iff the counter is wrapping */ |
Hanno Becker | dd77229 | 2020-02-05 10:38:31 +0000 | [diff] [blame] | 4530 | if( i == mbedtls_ssl_ep_len( ssl ) ) |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 4531 | { |
| 4532 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); |
| 4533 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 4534 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4537 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 47db877 | 2018-08-21 13:32:13 +0100 | [diff] [blame] | 4538 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4539 | flush == SSL_DONT_FORCE_FLUSH ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4540 | { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 4541 | size_t remaining; |
| 4542 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 4543 | if( ret < 0 ) |
| 4544 | { |
| 4545 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram", |
| 4546 | ret ); |
| 4547 | return( ret ); |
| 4548 | } |
| 4549 | |
| 4550 | remaining = (size_t) ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4551 | if( remaining == 0 ) |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4552 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4553 | flush = SSL_FORCE_FLUSH; |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4554 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4555 | else |
| 4556 | { |
Hanno Becker | 513815a | 2018-08-20 11:56:09 +0100 | [diff] [blame] | 4557 | 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] | 4558 | } |
| 4559 | } |
| 4560 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4561 | |
| 4562 | if( ( flush == SSL_FORCE_FLUSH ) && |
| 4563 | ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4564 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4565 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4566 | return( ret ); |
| 4567 | } |
| 4568 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4569 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4570 | |
| 4571 | return( 0 ); |
| 4572 | } |
| 4573 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4574 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4575 | |
| 4576 | static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl ) |
| 4577 | { |
| 4578 | if( ssl->in_msglen < ssl->in_hslen || |
| 4579 | memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 || |
| 4580 | memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ) |
| 4581 | { |
| 4582 | return( 1 ); |
| 4583 | } |
| 4584 | return( 0 ); |
| 4585 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4586 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4587 | 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] | 4588 | { |
| 4589 | return( ( ssl->in_msg[9] << 16 ) | |
| 4590 | ( ssl->in_msg[10] << 8 ) | |
| 4591 | ssl->in_msg[11] ); |
| 4592 | } |
| 4593 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4594 | 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] | 4595 | { |
| 4596 | return( ( ssl->in_msg[6] << 16 ) | |
| 4597 | ( ssl->in_msg[7] << 8 ) | |
| 4598 | ssl->in_msg[8] ); |
| 4599 | } |
| 4600 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4601 | static int ssl_check_hs_header( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4602 | { |
| 4603 | uint32_t msg_len, frag_off, frag_len; |
| 4604 | |
| 4605 | msg_len = ssl_get_hs_total_len( ssl ); |
| 4606 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 4607 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 4608 | |
| 4609 | if( frag_off > msg_len ) |
| 4610 | return( -1 ); |
| 4611 | |
| 4612 | if( frag_len > msg_len - frag_off ) |
| 4613 | return( -1 ); |
| 4614 | |
| 4615 | if( frag_len + 12 > ssl->in_msglen ) |
| 4616 | return( -1 ); |
| 4617 | |
| 4618 | return( 0 ); |
| 4619 | } |
| 4620 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4621 | /* |
| 4622 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 4623 | */ |
| 4624 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) |
| 4625 | { |
| 4626 | unsigned int start_bits, end_bits; |
| 4627 | |
| 4628 | start_bits = 8 - ( offset % 8 ); |
| 4629 | if( start_bits != 8 ) |
| 4630 | { |
| 4631 | size_t first_byte_idx = offset / 8; |
| 4632 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 4633 | /* Special case */ |
| 4634 | if( len <= start_bits ) |
| 4635 | { |
| 4636 | for( ; len != 0; len-- ) |
| 4637 | mask[first_byte_idx] |= 1 << ( start_bits - len ); |
| 4638 | |
| 4639 | /* Avoid potential issues with offset or len becoming invalid */ |
| 4640 | return; |
| 4641 | } |
| 4642 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4643 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 4644 | len -= start_bits; |
| 4645 | |
| 4646 | for( ; start_bits != 0; start_bits-- ) |
| 4647 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); |
| 4648 | } |
| 4649 | |
| 4650 | end_bits = len % 8; |
| 4651 | if( end_bits != 0 ) |
| 4652 | { |
| 4653 | size_t last_byte_idx = ( offset + len ) / 8; |
| 4654 | |
| 4655 | len -= end_bits; /* Now len % 8 == 0 */ |
| 4656 | |
| 4657 | for( ; end_bits != 0; end_bits-- ) |
| 4658 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); |
| 4659 | } |
| 4660 | |
| 4661 | memset( mask + offset / 8, 0xFF, len / 8 ); |
| 4662 | } |
| 4663 | |
| 4664 | /* |
| 4665 | * Check that bitmask is full |
| 4666 | */ |
| 4667 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) |
| 4668 | { |
| 4669 | size_t i; |
| 4670 | |
| 4671 | for( i = 0; i < len / 8; i++ ) |
| 4672 | if( mask[i] != 0xFF ) |
| 4673 | return( -1 ); |
| 4674 | |
| 4675 | for( i = 0; i < len % 8; i++ ) |
| 4676 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) |
| 4677 | return( -1 ); |
| 4678 | |
| 4679 | return( 0 ); |
| 4680 | } |
| 4681 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4682 | /* msg_len does not include the handshake header */ |
Hanno Becker | 65dc885 | 2018-08-23 09:40:49 +0100 | [diff] [blame] | 4683 | static size_t ssl_get_reassembly_buffer_size( size_t msg_len, |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4684 | unsigned add_bitmap ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4685 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4686 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4687 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4688 | alloc_len = 12; /* Handshake header */ |
| 4689 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4690 | |
Hanno Becker | d07df86 | 2018-08-16 09:14:58 +0100 | [diff] [blame] | 4691 | if( add_bitmap ) |
| 4692 | alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4693 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4694 | return( alloc_len ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4695 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4696 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4697 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4698 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4699 | 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] | 4700 | { |
| 4701 | return( ( ssl->in_msg[1] << 16 ) | |
| 4702 | ( ssl->in_msg[2] << 8 ) | |
| 4703 | ssl->in_msg[3] ); |
| 4704 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4705 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4706 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4707 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4708 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4709 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4710 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d", |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4711 | ssl->in_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4712 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4713 | } |
| 4714 | |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 4715 | 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] | 4716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4717 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4718 | " %d, type = %d, hslen = %d", |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4719 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4720 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4721 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4722 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4723 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4724 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4725 | 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] | 4726 | |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4727 | if( ssl_check_hs_header( ssl ) != 0 ) |
| 4728 | { |
| 4729 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) ); |
| 4730 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4731 | } |
| 4732 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4733 | if( ssl->handshake != NULL && |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4734 | ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4735 | recv_msg_seq != ssl->handshake->in_msg_seq ) || |
| 4736 | ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4737 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4738 | { |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 4739 | if( recv_msg_seq > ssl->handshake->in_msg_seq ) |
| 4740 | { |
| 4741 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)", |
| 4742 | recv_msg_seq, |
| 4743 | ssl->handshake->in_msg_seq ) ); |
| 4744 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4745 | } |
| 4746 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 4747 | /* Retransmit only on last message from previous flight, to avoid |
| 4748 | * too many retransmissions. |
| 4749 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
| 4750 | 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] | 4751 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4752 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4753 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4754 | "message_seq = %d, start_of_flight = %d", |
| 4755 | recv_msg_seq, |
| 4756 | ssl->handshake->in_flight_start_seq ) ); |
| 4757 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4758 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4759 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4760 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4761 | return( ret ); |
| 4762 | } |
| 4763 | } |
| 4764 | else |
| 4765 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4766 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4767 | "message_seq = %d, expected = %d", |
| 4768 | recv_msg_seq, |
| 4769 | ssl->handshake->in_msg_seq ) ); |
| 4770 | } |
| 4771 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4772 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4773 | } |
| 4774 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4775 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4776 | /* Message reassembly is handled alongside buffering of future |
| 4777 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 4778 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4779 | * handshake logic layer. */ |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4780 | if( ssl_hs_is_proper_fragment( ssl ) == 1 ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4781 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4782 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4783 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4784 | } |
| 4785 | } |
| 4786 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4787 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4788 | /* With TLS we don't handle fragmentation (for now) */ |
| 4789 | if( ssl->in_msglen < ssl->in_hslen ) |
| 4790 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4791 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); |
| 4792 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4793 | } |
| 4794 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4795 | return( 0 ); |
| 4796 | } |
| 4797 | |
| 4798 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ) |
| 4799 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4800 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4801 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4802 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL ) |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 4803 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4804 | 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] | 4805 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4806 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4807 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4808 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4809 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4810 | ssl->handshake != NULL ) |
| 4811 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4812 | unsigned offset; |
| 4813 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4814 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4815 | /* Increment handshake sequence number */ |
| 4816 | hs->in_msg_seq++; |
| 4817 | |
| 4818 | /* |
| 4819 | * Clear up handshake buffering and reassembly structure. |
| 4820 | */ |
| 4821 | |
| 4822 | /* Free first entry */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4823 | ssl_buffering_free_slot( ssl, 0 ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4824 | |
| 4825 | /* Shift all other entries */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4826 | for( offset = 0, hs_buf = &hs->buffering.hs[0]; |
| 4827 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4828 | offset++, hs_buf++ ) |
| 4829 | { |
| 4830 | *hs_buf = *(hs_buf + 1); |
| 4831 | } |
| 4832 | |
| 4833 | /* Create a fresh last entry */ |
| 4834 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4835 | } |
| 4836 | #endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4837 | } |
| 4838 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4839 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4840 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 4841 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4842 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 4843 | * Bit n is set iff record number in_window_top - n has been seen. |
| 4844 | * |
| 4845 | * Usually, in_window_top is the last record number seen and the lsb of |
| 4846 | * in_window is set. The only exception is the initial state (record number 0 |
| 4847 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4848 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4849 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4850 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4851 | { |
| 4852 | ssl->in_window_top = 0; |
| 4853 | ssl->in_window = 0; |
| 4854 | } |
| 4855 | |
| 4856 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) |
| 4857 | { |
| 4858 | return( ( (uint64_t) buf[0] << 40 ) | |
| 4859 | ( (uint64_t) buf[1] << 32 ) | |
| 4860 | ( (uint64_t) buf[2] << 24 ) | |
| 4861 | ( (uint64_t) buf[3] << 16 ) | |
| 4862 | ( (uint64_t) buf[4] << 8 ) | |
| 4863 | ( (uint64_t) buf[5] ) ); |
| 4864 | } |
| 4865 | |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 4866 | static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr ) |
| 4867 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4868 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 4869 | unsigned char *original_in_ctr; |
| 4870 | |
| 4871 | // save original in_ctr |
| 4872 | original_in_ctr = ssl->in_ctr; |
| 4873 | |
| 4874 | // use counter from record |
| 4875 | ssl->in_ctr = record_in_ctr; |
| 4876 | |
| 4877 | ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl ); |
| 4878 | |
| 4879 | // restore the counter |
| 4880 | ssl->in_ctr = original_in_ctr; |
| 4881 | |
| 4882 | return ret; |
| 4883 | } |
| 4884 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4885 | /* |
| 4886 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 4887 | */ |
Hanno Becker | 0183d69 | 2019-07-12 08:50:37 +0100 | [diff] [blame] | 4888 | 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] | 4889 | { |
| 4890 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4891 | uint64_t bit; |
| 4892 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4893 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4894 | return( 0 ); |
| 4895 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4896 | if( rec_seqnum > ssl->in_window_top ) |
| 4897 | return( 0 ); |
| 4898 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4899 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4900 | |
| 4901 | if( bit >= 64 ) |
| 4902 | return( -1 ); |
| 4903 | |
| 4904 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) |
| 4905 | return( -1 ); |
| 4906 | |
| 4907 | return( 0 ); |
| 4908 | } |
| 4909 | |
| 4910 | /* |
| 4911 | * Update replay window on new validated record |
| 4912 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4913 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4914 | { |
| 4915 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4916 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4917 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4918 | return; |
| 4919 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4920 | if( rec_seqnum > ssl->in_window_top ) |
| 4921 | { |
| 4922 | /* Update window_top and the contents of the window */ |
| 4923 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 4924 | |
| 4925 | if( shift >= 64 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4926 | ssl->in_window = 1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4927 | else |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4928 | { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4929 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4930 | ssl->in_window |= 1; |
| 4931 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4932 | |
| 4933 | ssl->in_window_top = rec_seqnum; |
| 4934 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4935 | else |
| 4936 | { |
| 4937 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4938 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4939 | |
| 4940 | if( bit < 64 ) /* Always true, but be extra sure */ |
| 4941 | ssl->in_window |= (uint64_t) 1 << bit; |
| 4942 | } |
| 4943 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4944 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4945 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4946 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4947 | /* |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4948 | * Without any SSL context, check if a datagram looks like a ClientHello with |
| 4949 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 4950 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4951 | * |
| 4952 | * - if cookie is valid, return 0 |
| 4953 | * - if ClientHello looks superficially valid but cookie is not, |
| 4954 | * fill obuf and set olen, then |
| 4955 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 4956 | * - otherwise return a specific error code |
| 4957 | */ |
| 4958 | static int ssl_check_dtls_clihlo_cookie( |
| 4959 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 4960 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
| 4961 | void *p_cookie, |
| 4962 | const unsigned char *cli_id, size_t cli_id_len, |
| 4963 | const unsigned char *in, size_t in_len, |
| 4964 | unsigned char *obuf, size_t buf_len, size_t *olen ) |
| 4965 | { |
| 4966 | size_t sid_len, cookie_len; |
| 4967 | unsigned char *p; |
| 4968 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4969 | /* |
| 4970 | * Structure of ClientHello with record and handshake headers, |
| 4971 | * and expected values. We don't need to check a lot, more checks will be |
| 4972 | * done when actually parsing the ClientHello - skipping those checks |
| 4973 | * avoids code duplication and does not make cookie forging any easier. |
| 4974 | * |
| 4975 | * 0-0 ContentType type; copied, must be handshake |
| 4976 | * 1-2 ProtocolVersion version; copied |
| 4977 | * 3-4 uint16 epoch; copied, must be 0 |
| 4978 | * 5-10 uint48 sequence_number; copied |
| 4979 | * 11-12 uint16 length; (ignored) |
| 4980 | * |
| 4981 | * 13-13 HandshakeType msg_type; (ignored) |
| 4982 | * 14-16 uint24 length; (ignored) |
| 4983 | * 17-18 uint16 message_seq; copied |
| 4984 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 4985 | * 22-24 uint24 fragment_length; (ignored) |
| 4986 | * |
| 4987 | * 25-26 ProtocolVersion client_version; (ignored) |
| 4988 | * 27-58 Random random; (ignored) |
| 4989 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 4990 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 4991 | * ... |
| 4992 | * |
| 4993 | * Minimum length is 61 bytes. |
| 4994 | */ |
| 4995 | if( in_len < 61 || |
| 4996 | in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4997 | in[3] != 0 || in[4] != 0 || |
| 4998 | in[19] != 0 || in[20] != 0 || in[21] != 0 ) |
| 4999 | { |
| 5000 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 5001 | } |
| 5002 | |
| 5003 | sid_len = in[59]; |
| 5004 | if( sid_len > in_len - 61 ) |
| 5005 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 5006 | |
| 5007 | cookie_len = in[60 + sid_len]; |
| 5008 | if( cookie_len > in_len - 60 ) |
| 5009 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 5010 | |
| 5011 | if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len, |
| 5012 | cli_id, cli_id_len ) == 0 ) |
| 5013 | { |
| 5014 | /* Valid cookie */ |
| 5015 | return( 0 ); |
| 5016 | } |
| 5017 | |
| 5018 | /* |
| 5019 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 5020 | * |
| 5021 | * 0-0 ContentType type; copied |
| 5022 | * 1-2 ProtocolVersion version; copied |
| 5023 | * 3-4 uint16 epoch; copied |
| 5024 | * 5-10 uint48 sequence_number; copied |
| 5025 | * 11-12 uint16 length; olen - 13 |
| 5026 | * |
| 5027 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 5028 | * 14-16 uint24 length; olen - 25 |
| 5029 | * 17-18 uint16 message_seq; copied |
| 5030 | * 19-21 uint24 fragment_offset; copied |
| 5031 | * 22-24 uint24 fragment_length; olen - 25 |
| 5032 | * |
| 5033 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 5034 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 5035 | * |
| 5036 | * Minimum length is 28. |
| 5037 | */ |
| 5038 | if( buf_len < 28 ) |
| 5039 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 5040 | |
| 5041 | /* Copy most fields and adapt others */ |
| 5042 | memcpy( obuf, in, 25 ); |
| 5043 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 5044 | obuf[25] = 0xfe; |
| 5045 | obuf[26] = 0xff; |
| 5046 | |
| 5047 | /* Generate and write actual cookie */ |
| 5048 | p = obuf + 28; |
| 5049 | if( f_cookie_write( p_cookie, |
| 5050 | &p, obuf + buf_len, cli_id, cli_id_len ) != 0 ) |
| 5051 | { |
| 5052 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5053 | } |
| 5054 | |
| 5055 | *olen = p - obuf; |
| 5056 | |
| 5057 | /* Go back and fill length fields */ |
| 5058 | obuf[27] = (unsigned char)( *olen - 28 ); |
| 5059 | |
| 5060 | obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 ); |
| 5061 | obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 ); |
| 5062 | obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) ); |
| 5063 | |
| 5064 | obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 ); |
| 5065 | obuf[12] = (unsigned char)( ( *olen - 13 ) ); |
| 5066 | |
| 5067 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
| 5068 | } |
| 5069 | |
| 5070 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5071 | * Handle possible client reconnect with the same UDP quadruplet |
| 5072 | * (RFC 6347 Section 4.2.8). |
| 5073 | * |
| 5074 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 5075 | * that looks like a ClientHello. |
| 5076 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5077 | * - if the input looks like a ClientHello without cookies, |
| 5078 | * send back HelloVerifyRequest, then |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5079 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5080 | * - if the input looks like a ClientHello with a valid cookie, |
| 5081 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 5082 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5083 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5084 | * |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5085 | * mbedtls_ssl_read_record() will ignore the record if anything else than |
Simon Butcher | d0bf6a3 | 2015-09-11 17:34:49 +0100 | [diff] [blame] | 5086 | * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function |
| 5087 | * cannot not return 0. |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5088 | */ |
| 5089 | static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) |
| 5090 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5091 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5092 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5093 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5094 | if( ssl->conf->f_cookie_write == NULL || |
| 5095 | ssl->conf->f_cookie_check == NULL ) |
| 5096 | { |
| 5097 | /* If we can't use cookies to verify reachability of the peer, |
| 5098 | * drop the record. */ |
| 5099 | return( 0 ); |
| 5100 | } |
| 5101 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5102 | ret = ssl_check_dtls_clihlo_cookie( |
| 5103 | ssl->conf->f_cookie_write, |
| 5104 | ssl->conf->f_cookie_check, |
| 5105 | ssl->conf->p_cookie, |
| 5106 | ssl->cli_id, ssl->cli_id_len, |
| 5107 | ssl->in_buf, ssl->in_left, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5108 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5109 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5110 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret ); |
| 5111 | |
| 5112 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5113 | { |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 5114 | /* 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] | 5115 | * If the error is permanent we'll catch it later, |
| 5116 | * if it's not, then hopefully it'll work next time. */ |
| 5117 | (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len ); |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5118 | ret = 0; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5119 | } |
| 5120 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5121 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5122 | { |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5123 | /* Got a valid cookie, partially reset context */ |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame^] | 5124 | if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5125 | { |
| 5126 | MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret ); |
| 5127 | return( ret ); |
| 5128 | } |
| 5129 | |
| 5130 | return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5131 | } |
| 5132 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5133 | return( ret ); |
| 5134 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 5135 | #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] | 5136 | |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 5137 | static int ssl_check_record_type( uint8_t record_type ) |
| 5138 | { |
| 5139 | if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 5140 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 5141 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
| 5142 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 5143 | { |
| 5144 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5145 | } |
| 5146 | |
| 5147 | return( 0 ); |
| 5148 | } |
| 5149 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5150 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5151 | * ContentType type; |
| 5152 | * ProtocolVersion version; |
| 5153 | * uint16 epoch; // DTLS only |
| 5154 | * uint48 sequence_number; // DTLS only |
| 5155 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5156 | * |
| 5157 | * 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] | 5158 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5159 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 5160 | * |
| 5161 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 5162 | * 1. proceed with the record if this function returns 0 |
| 5163 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 5164 | * 3. return CLIENT_RECONNECT if this function return that value |
| 5165 | * 4. drop the whole datagram if this function returns anything else. |
| 5166 | * Point 2 is needed when the peer is resending, and we have already received |
| 5167 | * 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] | 5168 | */ |
Hanno Becker | 331de3d | 2019-07-12 11:10:16 +0100 | [diff] [blame] | 5169 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5170 | unsigned char *buf, |
| 5171 | size_t len, |
| 5172 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5173 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5174 | int major_ver, minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5175 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5176 | size_t const rec_hdr_type_offset = 0; |
| 5177 | size_t const rec_hdr_type_len = 1; |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 5178 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5179 | size_t const rec_hdr_version_offset = rec_hdr_type_offset + |
| 5180 | rec_hdr_type_len; |
| 5181 | size_t const rec_hdr_version_len = 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5182 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5183 | size_t const rec_hdr_ctr_len = 8; |
| 5184 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 5185 | uint32_t rec_epoch; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5186 | size_t const rec_hdr_ctr_offset = rec_hdr_version_offset + |
| 5187 | rec_hdr_version_len; |
| 5188 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5189 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5190 | size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset + |
| 5191 | rec_hdr_ctr_len; |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 5192 | size_t rec_hdr_cid_len = 0; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5193 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5194 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5195 | |
| 5196 | size_t rec_hdr_len_offset; /* To be determined */ |
| 5197 | size_t const rec_hdr_len_len = 2; |
| 5198 | |
| 5199 | /* |
| 5200 | * Check minimum lengths for record header. |
| 5201 | */ |
| 5202 | |
| 5203 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5204 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5205 | { |
| 5206 | rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len; |
| 5207 | } |
| 5208 | else |
| 5209 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5210 | { |
| 5211 | rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len; |
| 5212 | } |
| 5213 | |
| 5214 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) |
| 5215 | { |
| 5216 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u", |
| 5217 | (unsigned) len, |
| 5218 | (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) ); |
| 5219 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5220 | } |
| 5221 | |
| 5222 | /* |
| 5223 | * Parse and validate record content type |
| 5224 | */ |
| 5225 | |
| 5226 | rec->type = buf[ rec_hdr_type_offset ]; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5227 | |
| 5228 | /* Check record content type */ |
| 5229 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5230 | rec->cid_len = 0; |
| 5231 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5232 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5233 | ssl->conf->cid_len != 0 && |
| 5234 | rec->type == MBEDTLS_SSL_MSG_CID ) |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5235 | { |
| 5236 | /* Shift pointers to account for record header including CID |
| 5237 | * struct { |
| 5238 | * ContentType special_type = tls12_cid; |
| 5239 | * ProtocolVersion version; |
| 5240 | * uint16 epoch; |
| 5241 | * uint48 sequence_number; |
Hanno Becker | 8e55b0f | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 5242 | * opaque cid[cid_length]; // Additional field compared to |
| 5243 | * // default DTLS record format |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5244 | * uint16 length; |
| 5245 | * opaque enc_content[DTLSCiphertext.length]; |
| 5246 | * } DTLSCiphertext; |
| 5247 | */ |
| 5248 | |
| 5249 | /* So far, we only support static CID lengths |
| 5250 | * fixed in the configuration. */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5251 | rec_hdr_cid_len = ssl->conf->cid_len; |
| 5252 | rec_hdr_len_offset += rec_hdr_cid_len; |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5253 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5254 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5255 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5256 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u", |
| 5257 | (unsigned) len, |
| 5258 | (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) ); |
Hanno Becker | 59be60e | 2019-07-10 14:53:43 +0100 | [diff] [blame] | 5259 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5260 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5261 | |
Manuel Pégourié-Gonnard | 7e821b5 | 2019-08-02 10:17:15 +0200 | [diff] [blame] | 5262 | /* configured CID len is guaranteed at most 255, see |
| 5263 | * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */ |
| 5264 | rec->cid_len = (uint8_t) rec_hdr_cid_len; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5265 | memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len ); |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5266 | } |
| 5267 | else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5268 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 5269 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5270 | if( ssl_check_record_type( rec->type ) ) |
| 5271 | { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 5272 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u", |
| 5273 | (unsigned) rec->type ) ); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5274 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5275 | } |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 5276 | } |
| 5277 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5278 | /* |
| 5279 | * Parse and validate record version |
| 5280 | */ |
| 5281 | |
Hanno Becker | d0b66d0 | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 5282 | rec->ver[0] = buf[ rec_hdr_version_offset + 0 ]; |
| 5283 | rec->ver[1] = buf[ rec_hdr_version_offset + 1 ]; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5284 | mbedtls_ssl_read_version( &major_ver, &minor_ver, |
| 5285 | ssl->conf->transport, |
Hanno Becker | d0b66d0 | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 5286 | &rec->ver[0] ); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5287 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5288 | if( major_ver != ssl->major_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5289 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5290 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); |
| 5291 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5294 | if( minor_ver > ssl->conf->max_minor_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5295 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5296 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); |
| 5297 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5298 | } |
| 5299 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5300 | /* |
| 5301 | * Parse/Copy record sequence number. |
| 5302 | */ |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5303 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5304 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5305 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 5306 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5307 | /* Copy explicit record sequence number from input buffer. */ |
| 5308 | memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset, |
| 5309 | rec_hdr_ctr_len ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 5310 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5311 | else |
| 5312 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5313 | { |
| 5314 | /* Copy implicit record sequence number from SSL context structure. */ |
| 5315 | memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len ); |
| 5316 | } |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 5317 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5318 | /* |
| 5319 | * Parse record length. |
| 5320 | */ |
| 5321 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5322 | rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len; |
Hanno Becker | 9eca276 | 2019-07-25 10:16:37 +0100 | [diff] [blame] | 5323 | rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) | |
| 5324 | ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 ); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5325 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5326 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5327 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, " |
Hanno Becker | 92d30f5 | 2019-05-23 17:03:44 +0100 | [diff] [blame] | 5328 | "version = [%d:%d], msglen = %d", |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5329 | rec->type, |
| 5330 | major_ver, minor_ver, rec->data_len ) ); |
| 5331 | |
| 5332 | rec->buf = buf; |
| 5333 | rec->buf_len = rec->data_offset + rec->data_len; |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5334 | |
Hanno Becker | d417cc9 | 2019-07-26 08:20:27 +0100 | [diff] [blame] | 5335 | if( rec->data_len == 0 ) |
| 5336 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5337 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5338 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 5339 | * DTLS-related tests. |
| 5340 | * Check epoch before checking length constraint because |
| 5341 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 5342 | * message gets duplicated before the corresponding Finished message, |
| 5343 | * the second ChangeCipherSpec should be discarded because it belongs |
| 5344 | * to an old epoch, but not because its length is shorter than |
| 5345 | * the minimum record length for packets using the new record transform. |
| 5346 | * Note that these two kinds of failures are handled differently, |
| 5347 | * as an unexpected record is silently skipped but an invalid |
| 5348 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5349 | */ |
| 5350 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5351 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5352 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5353 | rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1]; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5354 | |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5355 | /* Check that the datagram is large enough to contain a record |
| 5356 | * of the advertised length. */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5357 | if( len < rec->data_offset + rec->data_len ) |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5358 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5359 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.", |
| 5360 | (unsigned) len, |
| 5361 | (unsigned)( rec->data_offset + rec->data_len ) ) ); |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5362 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5363 | } |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5364 | |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5365 | /* Records from other, non-matching epochs are silently discarded. |
| 5366 | * (The case of same-port Client reconnects must be considered in |
| 5367 | * the caller). */ |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5368 | if( rec_epoch != ssl->in_epoch ) |
| 5369 | { |
| 5370 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " |
| 5371 | "expected %d, received %d", |
| 5372 | ssl->in_epoch, rec_epoch ) ); |
| 5373 | |
Hanno Becker | 552f747 | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 5374 | /* Records from the next epoch are considered for buffering |
| 5375 | * (concretely: early Finished messages). */ |
| 5376 | if( rec_epoch == (unsigned) ssl->in_epoch + 1 ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5377 | { |
Hanno Becker | 552f747 | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 5378 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) ); |
| 5379 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5380 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5381 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5382 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5383 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5384 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5385 | /* For records from the correct epoch, check whether their |
| 5386 | * sequence number has been seen before. */ |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 5387 | else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl, |
| 5388 | &rec->ctr[0] ) != 0 ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5389 | { |
| 5390 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); |
| 5391 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 5392 | } |
| 5393 | #endif |
| 5394 | } |
| 5395 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5396 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5397 | return( 0 ); |
| 5398 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5399 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5400 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5401 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
| 5402 | static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl ) |
| 5403 | { |
| 5404 | unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1]; |
| 5405 | |
| 5406 | /* |
| 5407 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 5408 | * access the first byte of record content (handshake type), as we |
| 5409 | * have an active transform (possibly iv_len != 0), so use the |
| 5410 | * fact that the record header len is 13 instead. |
| 5411 | */ |
| 5412 | if( rec_epoch == 0 && |
| 5413 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5414 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 5415 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 5416 | ssl->in_left > 13 && |
| 5417 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO ) |
| 5418 | { |
| 5419 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect " |
| 5420 | "from the same port" ) ); |
| 5421 | return( ssl_handle_possible_reconnect( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
| 5424 | return( 0 ); |
| 5425 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5426 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5427 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5428 | /* |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 5429 | * If applicable, decrypt record content |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5430 | */ |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5431 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl, |
| 5432 | mbedtls_record *rec ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5433 | { |
| 5434 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 5435 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5436 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5437 | rec->buf, rec->buf_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5438 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5439 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 5440 | if( mbedtls_ssl_hw_record_read != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5441 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5442 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5443 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5444 | ret = mbedtls_ssl_hw_record_read( ssl ); |
| 5445 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5446 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5447 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret ); |
| 5448 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5449 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 5450 | |
| 5451 | if( ret == 0 ) |
| 5452 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5453 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5454 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5455 | if( !done && ssl->transform_in != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5456 | { |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5457 | unsigned char const old_msg_type = rec->type; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5458 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 5459 | if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5460 | rec ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5461 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5462 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5463 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5464 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5465 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
| 5466 | ssl->conf->ignore_unexpected_cid |
| 5467 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 5468 | { |
Hanno Becker | e8d6afd | 2019-05-24 10:11:06 +0100 | [diff] [blame] | 5469 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) ); |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5470 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5471 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5472 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5473 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5474 | return( ret ); |
| 5475 | } |
| 5476 | |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5477 | if( old_msg_type != rec->type ) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5478 | { |
| 5479 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d", |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5480 | old_msg_type, rec->type ) ); |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5481 | } |
| 5482 | |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 5483 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5484 | rec->buf + rec->data_offset, rec->data_len ); |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 5485 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5486 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5487 | /* We have already checked the record content type |
| 5488 | * in ssl_parse_record_header(), failing or silently |
| 5489 | * dropping the record in the case of an unknown type. |
| 5490 | * |
| 5491 | * Since with the use of CIDs, the record content type |
| 5492 | * might change during decryption, re-check the record |
| 5493 | * content type, but treat a failure as fatal this time. */ |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5494 | if( ssl_check_record_type( rec->type ) ) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5495 | { |
| 5496 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
| 5497 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5498 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5499 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5500 | |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5501 | if( rec->data_len == 0 ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5502 | { |
| 5503 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5504 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5505 | && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5506 | { |
| 5507 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ |
| 5508 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) ); |
| 5509 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5510 | } |
| 5511 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5512 | |
| 5513 | ssl->nb_zero++; |
| 5514 | |
| 5515 | /* |
| 5516 | * Three or more empty messages may be a DoS attack |
| 5517 | * (excessive CPU consumption). |
| 5518 | */ |
| 5519 | if( ssl->nb_zero > 3 ) |
| 5520 | { |
| 5521 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " |
Hanno Becker | 6e7700d | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 5522 | "messages, possible DoS attack" ) ); |
| 5523 | /* Treat the records as if they were not properly authenticated, |
| 5524 | * thereby failing the connection if we see more than allowed |
| 5525 | * by the configured bad MAC threshold. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5526 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
| 5527 | } |
| 5528 | } |
| 5529 | else |
| 5530 | ssl->nb_zero = 0; |
| 5531 | |
| 5532 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5533 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5534 | { |
| 5535 | ; /* in_ctr read from peer, not maintained internally */ |
| 5536 | } |
| 5537 | else |
| 5538 | #endif |
| 5539 | { |
| 5540 | unsigned i; |
Hanno Becker | dd77229 | 2020-02-05 10:38:31 +0000 | [diff] [blame] | 5541 | for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5542 | if( ++ssl->in_ctr[i - 1] != 0 ) |
| 5543 | break; |
| 5544 | |
| 5545 | /* The loop goes to its end iff the counter is wrapping */ |
Hanno Becker | dd77229 | 2020-02-05 10:38:31 +0000 | [diff] [blame] | 5546 | if( i == mbedtls_ssl_ep_len( ssl ) ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5547 | { |
| 5548 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); |
| 5549 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 5550 | } |
| 5551 | } |
| 5552 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5553 | } |
| 5554 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5555 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5556 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5557 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5558 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5559 | } |
| 5560 | #endif |
| 5561 | |
Hanno Becker | d96e10b | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 5562 | /* Check actual (decrypted) record content length against |
| 5563 | * configured maximum. */ |
| 5564 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5565 | { |
| 5566 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 5567 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5568 | } |
| 5569 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5570 | return( 0 ); |
| 5571 | } |
| 5572 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5573 | 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] | 5574 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5575 | /* |
| 5576 | * Read a record. |
| 5577 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5578 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 5579 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 5580 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5581 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5582 | |
| 5583 | /* Helper functions for mbedtls_ssl_read_record(). */ |
| 5584 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5585 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ); |
| 5586 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 5587 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5588 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl, |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5589 | unsigned update_hs_digest ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5590 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5591 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5592 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5593 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5594 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5595 | if( ssl->keep_current_message == 0 ) |
| 5596 | { |
| 5597 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5598 | |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5599 | ret = ssl_consume_current_message( ssl ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5600 | if( ret != 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5601 | return( ret ); |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5602 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5603 | if( ssl_record_is_in_progress( ssl ) == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5604 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5605 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5606 | int have_buffered = 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5607 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5608 | /* We only check for buffered messages if the |
| 5609 | * current datagram is fully consumed. */ |
| 5610 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5611 | ssl_next_record_is_in_datagram( ssl ) == 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5612 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5613 | if( ssl_load_buffered_message( ssl ) == 0 ) |
| 5614 | have_buffered = 1; |
| 5615 | } |
| 5616 | |
| 5617 | if( have_buffered == 0 ) |
| 5618 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5619 | { |
| 5620 | ret = ssl_get_next_record( ssl ); |
| 5621 | if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ) |
| 5622 | continue; |
| 5623 | |
| 5624 | if( ret != 0 ) |
| 5625 | { |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 5626 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5627 | return( ret ); |
| 5628 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5629 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5630 | } |
| 5631 | |
| 5632 | ret = mbedtls_ssl_handle_message_type( ssl ); |
| 5633 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5634 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5635 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 5636 | { |
| 5637 | /* Buffer future message */ |
| 5638 | ret = ssl_buffer_message( ssl ); |
| 5639 | if( ret != 0 ) |
| 5640 | return( ret ); |
| 5641 | |
| 5642 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 5643 | } |
| 5644 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5645 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5646 | } while( MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 5647 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5648 | |
| 5649 | if( 0 != ret ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5650 | { |
Hanno Becker | 05c4fc8 | 2017-11-09 14:34:06 +0000 | [diff] [blame] | 5651 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5652 | return( ret ); |
| 5653 | } |
| 5654 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5655 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5656 | update_hs_digest == 1 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5657 | { |
| 5658 | mbedtls_ssl_update_handshake_status( ssl ); |
| 5659 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5660 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5661 | else |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5662 | { |
Hanno Becker | 02f5907 | 2018-08-15 14:00:24 +0100 | [diff] [blame] | 5663 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5664 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5665 | } |
| 5666 | |
| 5667 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); |
| 5668 | |
| 5669 | return( 0 ); |
| 5670 | } |
| 5671 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5672 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5673 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5674 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5675 | if( ssl->in_left > ssl->next_record_offset ) |
| 5676 | return( 1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5677 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5678 | return( 0 ); |
| 5679 | } |
| 5680 | |
| 5681 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ) |
| 5682 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5683 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5684 | mbedtls_ssl_hs_buffer * hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5685 | int ret = 0; |
| 5686 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5687 | if( hs == NULL ) |
| 5688 | return( -1 ); |
| 5689 | |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 5690 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) ); |
| 5691 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5692 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 5693 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 5694 | { |
| 5695 | /* Check if we have seen a ChangeCipherSpec before. |
| 5696 | * If yes, synthesize a CCS record. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5697 | if( !hs->buffering.seen_ccs ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5698 | { |
| 5699 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) ); |
| 5700 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 5701 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5702 | } |
| 5703 | |
Hanno Becker | 39b8bc9 | 2018-08-28 17:17:13 +0100 | [diff] [blame] | 5704 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5705 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 5706 | ssl->in_msglen = 1; |
| 5707 | ssl->in_msg[0] = 1; |
| 5708 | |
| 5709 | /* As long as they are equal, the exact value doesn't matter. */ |
| 5710 | ssl->in_left = 0; |
| 5711 | ssl->next_record_offset = 0; |
| 5712 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5713 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5714 | goto exit; |
| 5715 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5716 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5717 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5718 | /* Debug only */ |
| 5719 | { |
| 5720 | unsigned offset; |
| 5721 | for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
| 5722 | { |
| 5723 | hs_buf = &hs->buffering.hs[offset]; |
| 5724 | if( hs_buf->is_valid == 1 ) |
| 5725 | { |
| 5726 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.", |
| 5727 | hs->in_msg_seq + offset, |
Hanno Becker | a591c48 | 2018-08-28 17:20:00 +0100 | [diff] [blame] | 5728 | hs_buf->is_complete ? "fully" : "partially" ) ); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5729 | } |
| 5730 | } |
| 5731 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5732 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5733 | |
| 5734 | /* Check if we have buffered and/or fully reassembled the |
| 5735 | * next handshake message. */ |
| 5736 | hs_buf = &hs->buffering.hs[0]; |
| 5737 | if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) ) |
| 5738 | { |
| 5739 | /* Synthesize a record containing the buffered HS message. */ |
| 5740 | size_t msg_len = ( hs_buf->data[1] << 16 ) | |
| 5741 | ( hs_buf->data[2] << 8 ) | |
| 5742 | hs_buf->data[3]; |
| 5743 | |
| 5744 | /* Double-check that we haven't accidentally buffered |
| 5745 | * a message that doesn't fit into the input buffer. */ |
| 5746 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5747 | { |
| 5748 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5749 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5750 | } |
| 5751 | |
| 5752 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) ); |
| 5753 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)", |
| 5754 | hs_buf->data, msg_len + 12 ); |
| 5755 | |
| 5756 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5757 | ssl->in_hslen = msg_len + 12; |
| 5758 | ssl->in_msglen = msg_len + 12; |
| 5759 | memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen ); |
| 5760 | |
| 5761 | ret = 0; |
| 5762 | goto exit; |
| 5763 | } |
| 5764 | else |
| 5765 | { |
| 5766 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered", |
| 5767 | hs->in_msg_seq ) ); |
| 5768 | } |
| 5769 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5770 | ret = -1; |
| 5771 | |
| 5772 | exit: |
| 5773 | |
| 5774 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) ); |
| 5775 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5776 | } |
| 5777 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5778 | static int ssl_buffer_make_space( mbedtls_ssl_context *ssl, |
| 5779 | size_t desired ) |
| 5780 | { |
| 5781 | int offset; |
| 5782 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5783 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available", |
| 5784 | (unsigned) desired ) ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5785 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5786 | /* Get rid of future records epoch first, if such exist. */ |
| 5787 | ssl_free_buffered_record( ssl ); |
| 5788 | |
| 5789 | /* Check if we have enough space available now. */ |
| 5790 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5791 | hs->buffering.total_bytes_buffered ) ) |
| 5792 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5793 | 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] | 5794 | return( 0 ); |
| 5795 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5796 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 5797 | /* We don't have enough space to buffer the next expected handshake |
| 5798 | * message. Remove buffers used for future messages to gain space, |
| 5799 | * starting with the most distant one. */ |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5800 | for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 5801 | offset >= 0; offset-- ) |
| 5802 | { |
| 5803 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message", |
| 5804 | offset ) ); |
| 5805 | |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5806 | ssl_buffering_free_slot( ssl, (uint8_t) offset ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5807 | |
| 5808 | /* Check if we have enough space available now. */ |
| 5809 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5810 | hs->buffering.total_bytes_buffered ) ) |
| 5811 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5812 | 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] | 5813 | return( 0 ); |
| 5814 | } |
| 5815 | } |
| 5816 | |
| 5817 | return( -1 ); |
| 5818 | } |
| 5819 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5820 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ) |
| 5821 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5822 | int ret = 0; |
| 5823 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5824 | |
| 5825 | if( hs == NULL ) |
| 5826 | return( 0 ); |
| 5827 | |
| 5828 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) ); |
| 5829 | |
| 5830 | switch( ssl->in_msgtype ) |
| 5831 | { |
| 5832 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
| 5833 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) ); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5834 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5835 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5836 | break; |
| 5837 | |
| 5838 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5839 | { |
| 5840 | unsigned recv_msg_seq_offset; |
| 5841 | unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
| 5842 | mbedtls_ssl_hs_buffer *hs_buf; |
| 5843 | size_t msg_len = ssl->in_hslen - 12; |
| 5844 | |
| 5845 | /* We should never receive an old handshake |
| 5846 | * message - double-check nonetheless. */ |
| 5847 | if( recv_msg_seq < ssl->handshake->in_msg_seq ) |
| 5848 | { |
| 5849 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5850 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5851 | } |
| 5852 | |
| 5853 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
| 5854 | if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 5855 | { |
| 5856 | /* Silently ignore -- message too far in the future */ |
| 5857 | MBEDTLS_SSL_DEBUG_MSG( 2, |
| 5858 | ( "Ignore future HS message with sequence number %u, " |
| 5859 | "buffering window %u - %u", |
| 5860 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 5861 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) ); |
| 5862 | |
| 5863 | goto exit; |
| 5864 | } |
| 5865 | |
| 5866 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ", |
| 5867 | recv_msg_seq, recv_msg_seq_offset ) ); |
| 5868 | |
| 5869 | hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ]; |
| 5870 | |
| 5871 | /* Check if the buffering for this seq nr has already commenced. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5872 | if( !hs_buf->is_valid ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5873 | { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5874 | size_t reassembly_buf_sz; |
| 5875 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5876 | hs_buf->is_fragmented = |
| 5877 | ( ssl_hs_is_proper_fragment( ssl ) == 1 ); |
| 5878 | |
| 5879 | /* We copy the message back into the input buffer |
| 5880 | * after reassembly, so check that it's not too large. |
| 5881 | * This is an implementation-specific limitation |
| 5882 | * and not one from the standard, hence it is not |
| 5883 | * checked in ssl_check_hs_header(). */ |
Hanno Becker | 96a6c69 | 2018-08-21 15:56:03 +0100 | [diff] [blame] | 5884 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5885 | { |
| 5886 | /* Ignore message */ |
| 5887 | goto exit; |
| 5888 | } |
| 5889 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5890 | /* Check if we have enough space to buffer the message. */ |
| 5891 | if( hs->buffering.total_bytes_buffered > |
| 5892 | MBEDTLS_SSL_DTLS_MAX_BUFFERING ) |
| 5893 | { |
| 5894 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5895 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5896 | } |
| 5897 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5898 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len, |
| 5899 | hs_buf->is_fragmented ); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5900 | |
| 5901 | if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5902 | hs->buffering.total_bytes_buffered ) ) |
| 5903 | { |
| 5904 | if( recv_msg_seq_offset > 0 ) |
| 5905 | { |
| 5906 | /* If we can't buffer a future message because |
| 5907 | * of space limitations -- ignore. */ |
| 5908 | 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", |
| 5909 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5910 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5911 | goto exit; |
| 5912 | } |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 5913 | else |
| 5914 | { |
| 5915 | 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", |
| 5916 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5917 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5918 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5919 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5920 | if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 ) |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5921 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5922 | 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", |
| 5923 | (unsigned) msg_len, |
| 5924 | (unsigned) reassembly_buf_sz, |
| 5925 | MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5926 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5927 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 5928 | goto exit; |
| 5929 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5930 | } |
| 5931 | |
| 5932 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d", |
| 5933 | msg_len ) ); |
| 5934 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5935 | hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz ); |
| 5936 | if( hs_buf->data == NULL ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5937 | { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5938 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5939 | goto exit; |
| 5940 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5941 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5942 | |
| 5943 | /* Prepare final header: copy msg_type, length and message_seq, |
| 5944 | * then add standardised fragment_offset and fragment_length */ |
| 5945 | memcpy( hs_buf->data, ssl->in_msg, 6 ); |
| 5946 | memset( hs_buf->data + 6, 0, 3 ); |
| 5947 | memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 ); |
| 5948 | |
| 5949 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5950 | |
| 5951 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5952 | } |
| 5953 | else |
| 5954 | { |
| 5955 | /* Make sure msg_type and length are consistent */ |
| 5956 | if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) |
| 5957 | { |
| 5958 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) ); |
| 5959 | /* Ignore */ |
| 5960 | goto exit; |
| 5961 | } |
| 5962 | } |
| 5963 | |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5964 | if( !hs_buf->is_complete ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5965 | { |
| 5966 | size_t frag_len, frag_off; |
| 5967 | unsigned char * const msg = hs_buf->data + 12; |
| 5968 | |
| 5969 | /* |
| 5970 | * Check and copy current fragment |
| 5971 | */ |
| 5972 | |
| 5973 | /* Validation of header fields already done in |
| 5974 | * mbedtls_ssl_prepare_handshake_record(). */ |
| 5975 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 5976 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 5977 | |
| 5978 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d", |
| 5979 | frag_off, frag_len ) ); |
| 5980 | memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); |
| 5981 | |
| 5982 | if( hs_buf->is_fragmented ) |
| 5983 | { |
| 5984 | unsigned char * const bitmask = msg + msg_len; |
| 5985 | ssl_bitmask_set( bitmask, frag_off, frag_len ); |
| 5986 | hs_buf->is_complete = ( ssl_bitmask_check( bitmask, |
| 5987 | msg_len ) == 0 ); |
| 5988 | } |
| 5989 | else |
| 5990 | { |
| 5991 | hs_buf->is_complete = 1; |
| 5992 | } |
| 5993 | |
| 5994 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete", |
| 5995 | hs_buf->is_complete ? "" : "not yet " ) ); |
| 5996 | } |
| 5997 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5998 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5999 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6000 | |
| 6001 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 6002 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6003 | break; |
| 6004 | } |
| 6005 | |
| 6006 | exit: |
| 6007 | |
| 6008 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) ); |
| 6009 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 6010 | } |
| 6011 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6012 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6013 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6014 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6015 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6016 | * Consume last content-layer message and potentially |
| 6017 | * update in_msglen which keeps track of the contents' |
| 6018 | * consumption state. |
| 6019 | * |
| 6020 | * (1) Handshake messages: |
| 6021 | * Remove last handshake message, move content |
| 6022 | * and adapt in_msglen. |
| 6023 | * |
| 6024 | * (2) Alert messages: |
| 6025 | * Consume whole record content, in_msglen = 0. |
| 6026 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6027 | * (3) Change cipher spec: |
| 6028 | * Consume whole record content, in_msglen = 0. |
| 6029 | * |
| 6030 | * (4) Application data: |
| 6031 | * Don't do anything - the record layer provides |
| 6032 | * the application data as a stream transport |
| 6033 | * and consumes through mbedtls_ssl_read only. |
| 6034 | * |
| 6035 | */ |
| 6036 | |
| 6037 | /* Case (1): Handshake messages */ |
| 6038 | if( ssl->in_hslen != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6039 | { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 6040 | /* Hard assertion to be sure that no application data |
| 6041 | * is in flight, as corrupting ssl->in_msglen during |
| 6042 | * ssl->in_offt != NULL is fatal. */ |
| 6043 | if( ssl->in_offt != NULL ) |
| 6044 | { |
| 6045 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6046 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6047 | } |
| 6048 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6049 | /* |
| 6050 | * Get next Handshake message in the current record |
| 6051 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6052 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6053 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 6054 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6055 | * current handshake content: If DTLS handshake |
| 6056 | * fragmentation is used, that's the fragment |
| 6057 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 6058 | * size here is faulty and should be changed at |
| 6059 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6060 | * (2) While it doesn't seem to cause problems, one |
| 6061 | * has to be very careful not to assume that in_hslen |
| 6062 | * is always <= in_msglen in a sensible communication. |
| 6063 | * Again, it's wrong for DTLS handshake fragmentation. |
| 6064 | * The following check is therefore mandatory, and |
| 6065 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 6066 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 6067 | * bounds after handling a DTLS message with an unexpected |
| 6068 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6069 | */ |
| 6070 | if( ssl->in_hslen < ssl->in_msglen ) |
| 6071 | { |
| 6072 | ssl->in_msglen -= ssl->in_hslen; |
| 6073 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 6074 | ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6075 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6076 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", |
| 6077 | ssl->in_msg, ssl->in_msglen ); |
| 6078 | } |
| 6079 | else |
| 6080 | { |
| 6081 | ssl->in_msglen = 0; |
| 6082 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 6083 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6084 | ssl->in_hslen = 0; |
| 6085 | } |
| 6086 | /* Case (4): Application data */ |
| 6087 | else if( ssl->in_offt != NULL ) |
| 6088 | { |
| 6089 | return( 0 ); |
| 6090 | } |
| 6091 | /* Everything else (CCS & Alerts) */ |
| 6092 | else |
| 6093 | { |
| 6094 | ssl->in_msglen = 0; |
| 6095 | } |
| 6096 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6097 | return( 0 ); |
| 6098 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6099 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 6100 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ) |
| 6101 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6102 | if( ssl->in_msglen > 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 6103 | return( 1 ); |
| 6104 | |
| 6105 | return( 0 ); |
| 6106 | } |
| 6107 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6108 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6109 | |
| 6110 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ) |
| 6111 | { |
| 6112 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 6113 | if( hs == NULL ) |
| 6114 | return; |
| 6115 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6116 | if( hs->buffering.future_record.data != NULL ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6117 | { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6118 | hs->buffering.total_bytes_buffered -= |
| 6119 | hs->buffering.future_record.len; |
| 6120 | |
| 6121 | mbedtls_free( hs->buffering.future_record.data ); |
| 6122 | hs->buffering.future_record.data = NULL; |
| 6123 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6124 | } |
| 6125 | |
| 6126 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ) |
| 6127 | { |
| 6128 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 6129 | unsigned char * rec; |
| 6130 | size_t rec_len; |
| 6131 | unsigned rec_epoch; |
| 6132 | |
| 6133 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6134 | return( 0 ); |
| 6135 | |
| 6136 | if( hs == NULL ) |
| 6137 | return( 0 ); |
| 6138 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6139 | rec = hs->buffering.future_record.data; |
| 6140 | rec_len = hs->buffering.future_record.len; |
| 6141 | rec_epoch = hs->buffering.future_record.epoch; |
| 6142 | |
| 6143 | if( rec == NULL ) |
| 6144 | return( 0 ); |
| 6145 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 6146 | /* Only consider loading future records if the |
| 6147 | * input buffer is empty. */ |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 6148 | if( ssl_next_record_is_in_datagram( ssl ) == 1 ) |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 6149 | return( 0 ); |
| 6150 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6151 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) ); |
| 6152 | |
| 6153 | if( rec_epoch != ssl->in_epoch ) |
| 6154 | { |
| 6155 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) ); |
| 6156 | goto exit; |
| 6157 | } |
| 6158 | |
| 6159 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) ); |
| 6160 | |
| 6161 | /* Double-check that the record is not too large */ |
| 6162 | if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN - |
| 6163 | (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
| 6164 | { |
| 6165 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6166 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6167 | } |
| 6168 | |
| 6169 | memcpy( ssl->in_hdr, rec, rec_len ); |
| 6170 | ssl->in_left = rec_len; |
| 6171 | ssl->next_record_offset = 0; |
| 6172 | |
| 6173 | ssl_free_buffered_record( ssl ); |
| 6174 | |
| 6175 | exit: |
| 6176 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) ); |
| 6177 | return( 0 ); |
| 6178 | } |
| 6179 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6180 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, |
| 6181 | mbedtls_record const *rec ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6182 | { |
| 6183 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6184 | |
| 6185 | /* Don't buffer future records outside handshakes. */ |
| 6186 | if( hs == NULL ) |
| 6187 | return( 0 ); |
| 6188 | |
| 6189 | /* Only buffer handshake records (we are only interested |
| 6190 | * in Finished messages). */ |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6191 | if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6192 | return( 0 ); |
| 6193 | |
| 6194 | /* Don't buffer more than one future epoch record. */ |
| 6195 | if( hs->buffering.future_record.data != NULL ) |
| 6196 | return( 0 ); |
| 6197 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6198 | /* Don't buffer record if there's not enough buffering space remaining. */ |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6199 | if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6200 | hs->buffering.total_bytes_buffered ) ) |
| 6201 | { |
| 6202 | 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 | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6203 | (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6204 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6205 | return( 0 ); |
| 6206 | } |
| 6207 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6208 | /* Buffer record */ |
| 6209 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u", |
| 6210 | ssl->in_epoch + 1 ) ); |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6211 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6212 | |
| 6213 | /* ssl_parse_record_header() only considers records |
| 6214 | * of the next epoch as candidates for buffering. */ |
| 6215 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6216 | hs->buffering.future_record.len = rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6217 | |
| 6218 | hs->buffering.future_record.data = |
| 6219 | mbedtls_calloc( 1, hs->buffering.future_record.len ); |
| 6220 | if( hs->buffering.future_record.data == NULL ) |
| 6221 | { |
| 6222 | /* If we run out of RAM trying to buffer a |
| 6223 | * record from the next epoch, just ignore. */ |
| 6224 | return( 0 ); |
| 6225 | } |
| 6226 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6227 | memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6228 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6229 | hs->buffering.total_bytes_buffered += rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6230 | return( 0 ); |
| 6231 | } |
| 6232 | |
| 6233 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6234 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 6235 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6236 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6237 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 6238 | mbedtls_record rec; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6239 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6240 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6241 | /* We might have buffered a future record; if so, |
| 6242 | * and if the epoch matches now, load it. |
| 6243 | * On success, this call will set ssl->in_left to |
| 6244 | * the length of the buffered record, so that |
| 6245 | * the calls to ssl_fetch_input() below will |
| 6246 | * essentially be no-ops. */ |
| 6247 | ret = ssl_load_buffered_record( ssl ); |
| 6248 | if( ret != 0 ) |
| 6249 | return( ret ); |
| 6250 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6251 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 6252 | /* Ensure that we have enough space available for the default form |
| 6253 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 6254 | * with no space for CIDs counted in). */ |
| 6255 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 6256 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6257 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6258 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6259 | return( ret ); |
| 6260 | } |
| 6261 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 6262 | ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec ); |
| 6263 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6264 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6265 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6266 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6267 | { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6268 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 6269 | { |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6270 | ret = ssl_buffer_future_record( ssl, &rec ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6271 | if( ret != 0 ) |
| 6272 | return( ret ); |
| 6273 | |
| 6274 | /* Fall through to handling of unexpected records */ |
| 6275 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 6276 | } |
| 6277 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6278 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ) |
| 6279 | { |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6280 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | d8bf8ce | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 6281 | /* Reset in pointers to default state for TLS/DTLS records, |
| 6282 | * assuming no CID and no offset between record content and |
| 6283 | * record plaintext. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 6284 | mbedtls_ssl_update_in_pointers( ssl ); |
Hanno Becker | d8bf8ce | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 6285 | |
Hanno Becker | 7ae20e0 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 6286 | /* Setup internal message pointers from record structure. */ |
| 6287 | ssl->in_msgtype = rec.type; |
| 6288 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6289 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 6290 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6291 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
| 6292 | ssl->in_msglen = rec.data_len; |
| 6293 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6294 | ret = ssl_check_client_reconnect( ssl ); |
| 6295 | if( ret != 0 ) |
| 6296 | return( ret ); |
| 6297 | #endif |
| 6298 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6299 | /* Skip unexpected record (but not whole datagram) */ |
Hanno Becker | 4acada3 | 2019-07-11 12:48:53 +0100 | [diff] [blame] | 6300 | ssl->next_record_offset = rec.buf_len; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6301 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6302 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record " |
| 6303 | "(header)" ) ); |
| 6304 | } |
| 6305 | else |
| 6306 | { |
| 6307 | /* Skip invalid record and the rest of the datagram */ |
| 6308 | ssl->next_record_offset = 0; |
| 6309 | ssl->in_left = 0; |
| 6310 | |
| 6311 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record " |
| 6312 | "(header)" ) ); |
| 6313 | } |
| 6314 | |
| 6315 | /* Get next record */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6316 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6317 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6318 | else |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6319 | #endif |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6320 | { |
| 6321 | return( ret ); |
| 6322 | } |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6323 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6324 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6325 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6326 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 6327 | { |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6328 | /* Remember offset of next record within datagram. */ |
Hanno Becker | f50da50 | 2019-07-11 12:50:10 +0100 | [diff] [blame] | 6329 | ssl->next_record_offset = rec.buf_len; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 6330 | if( ssl->next_record_offset < ssl->in_left ) |
| 6331 | { |
| 6332 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) ); |
| 6333 | } |
| 6334 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6335 | else |
| 6336 | #endif |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6337 | { |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 6338 | /* |
| 6339 | * Fetch record contents from underlying transport. |
| 6340 | */ |
Hanno Becker | a317566 | 2019-07-11 12:50:29 +0100 | [diff] [blame] | 6341 | ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len ); |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6342 | if( ret != 0 ) |
| 6343 | { |
| 6344 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
| 6345 | return( ret ); |
| 6346 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6347 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6348 | ssl->in_left = 0; |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6349 | } |
| 6350 | |
| 6351 | /* |
| 6352 | * Decrypt record contents. |
| 6353 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6354 | |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 6355 | if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6356 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6357 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6358 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6359 | { |
| 6360 | /* Silently discard invalid records */ |
Hanno Becker | 82e2a39 | 2019-05-03 16:36:59 +0100 | [diff] [blame] | 6361 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6362 | { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 6363 | /* Except when waiting for Finished as a bad mac here |
| 6364 | * probably means something went wrong in the handshake |
| 6365 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
| 6366 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 6367 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) |
| 6368 | { |
| 6369 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 6370 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 6371 | { |
| 6372 | mbedtls_ssl_send_alert_message( ssl, |
| 6373 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6374 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
| 6375 | } |
| 6376 | #endif |
| 6377 | return( ret ); |
| 6378 | } |
| 6379 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6380 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6381 | if( ssl->conf->badmac_limit != 0 && |
| 6382 | ++ssl->badmac_seen >= ssl->conf->badmac_limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6383 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6384 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); |
| 6385 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6386 | } |
| 6387 | #endif |
| 6388 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6389 | /* As above, invalid records cause |
| 6390 | * dismissal of the whole datagram. */ |
| 6391 | |
| 6392 | ssl->next_record_offset = 0; |
| 6393 | ssl->in_left = 0; |
| 6394 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6395 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6396 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6397 | } |
| 6398 | |
| 6399 | return( ret ); |
| 6400 | } |
| 6401 | else |
| 6402 | #endif |
| 6403 | { |
| 6404 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6405 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 6406 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6407 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6408 | mbedtls_ssl_send_alert_message( ssl, |
| 6409 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6410 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6411 | } |
| 6412 | #endif |
| 6413 | return( ret ); |
| 6414 | } |
| 6415 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6416 | |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6417 | |
| 6418 | /* Reset in pointers to default state for TLS/DTLS records, |
| 6419 | * assuming no CID and no offset between record content and |
| 6420 | * record plaintext. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 6421 | mbedtls_ssl_update_in_pointers( ssl ); |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6422 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6423 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 6424 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
irwir | 89af51f | 2019-09-26 21:04:56 +0300 | [diff] [blame] | 6425 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6426 | |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 6427 | /* The record content type may change during decryption, |
| 6428 | * so re-read it. */ |
| 6429 | ssl->in_msgtype = rec.type; |
| 6430 | /* Also update the input buffer, because unfortunately |
| 6431 | * the server-side ssl_parse_client_hello() reparses the |
| 6432 | * record header when receiving a ClientHello initiating |
| 6433 | * a renegotiation. */ |
| 6434 | ssl->in_hdr[0] = rec.type; |
| 6435 | ssl->in_msg = rec.buf + rec.data_offset; |
| 6436 | ssl->in_msglen = rec.data_len; |
| 6437 | ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 6438 | ssl->in_len[1] = (unsigned char)( rec.data_len ); |
| 6439 | |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 6440 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 6441 | if( ssl->transform_in != NULL && |
| 6442 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
| 6443 | { |
| 6444 | if( ( ret = ssl_decompress_buf( ssl ) ) != 0 ) |
| 6445 | { |
| 6446 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret ); |
| 6447 | return( ret ); |
| 6448 | } |
| 6449 | |
| 6450 | /* Check actual (decompress) record content length against |
| 6451 | * configured maximum. */ |
| 6452 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 6453 | { |
| 6454 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 6455 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6456 | } |
| 6457 | } |
| 6458 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
| 6459 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6460 | return( 0 ); |
| 6461 | } |
| 6462 | |
| 6463 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ) |
| 6464 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6465 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6466 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6467 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6468 | * Handle particular types of records |
| 6469 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6470 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6471 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6472 | if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 ) |
| 6473 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 6474 | return( ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6475 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6476 | } |
| 6477 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6478 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6479 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6480 | if( ssl->in_msglen != 1 ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6481 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6482 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d", |
| 6483 | ssl->in_msglen ) ); |
| 6484 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6485 | } |
| 6486 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6487 | if( ssl->in_msg[0] != 1 ) |
| 6488 | { |
| 6489 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x", |
| 6490 | ssl->in_msg[0] ) ); |
| 6491 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6492 | } |
| 6493 | |
| 6494 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6495 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6496 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
| 6497 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 6498 | { |
| 6499 | if( ssl->handshake == NULL ) |
| 6500 | { |
| 6501 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) ); |
| 6502 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 6503 | } |
| 6504 | |
| 6505 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) ); |
| 6506 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 6507 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6508 | #endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6509 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6511 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6512 | { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 6513 | if( ssl->in_msglen != 2 ) |
| 6514 | { |
| 6515 | /* Note: Standard allows for more than one 2 byte alert |
| 6516 | to be packed in a single message, but Mbed TLS doesn't |
| 6517 | currently support this. */ |
| 6518 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d", |
| 6519 | ssl->in_msglen ) ); |
| 6520 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6521 | } |
| 6522 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6523 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6524 | ssl->in_msg[0], ssl->in_msg[1] ) ); |
| 6525 | |
| 6526 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 6527 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6528 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6529 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6530 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6531 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 6532 | ssl->in_msg[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6533 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6534 | } |
| 6535 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6536 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6537 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6538 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6539 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); |
| 6540 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6541 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 6542 | |
| 6543 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
| 6544 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6545 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) |
| 6546 | { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6547 | 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] | 6548 | /* Will be handled when trying to parse ServerHello */ |
| 6549 | return( 0 ); |
| 6550 | } |
| 6551 | #endif |
| 6552 | |
| 6553 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) |
| 6554 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 6555 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 6556 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6557 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 6558 | { |
| 6559 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) ); |
| 6560 | /* Will be handled in mbedtls_ssl_parse_certificate() */ |
| 6561 | return( 0 ); |
| 6562 | } |
| 6563 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 6564 | |
| 6565 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6566 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6567 | } |
| 6568 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6569 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6570 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6571 | { |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6572 | /* Drop unexpected ApplicationData records, |
| 6573 | * except at the beginning of renegotiations */ |
| 6574 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
| 6575 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 6576 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6577 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 6578 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6579 | #endif |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6580 | ) |
| 6581 | { |
| 6582 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); |
| 6583 | return( MBEDTLS_ERR_SSL_NON_FATAL ); |
| 6584 | } |
| 6585 | |
| 6586 | if( ssl->handshake != NULL && |
| 6587 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 6588 | { |
| 6589 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 6590 | } |
| 6591 | } |
Hanno Becker | 4a4af9f | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 6592 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6593 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6594 | return( 0 ); |
| 6595 | } |
| 6596 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6597 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6598 | { |
irwir | 6c0da64 | 2019-09-26 21:07:41 +0300 | [diff] [blame] | 6599 | return( mbedtls_ssl_send_alert_message( ssl, |
| 6600 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6601 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6602 | } |
| 6603 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6604 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6605 | unsigned char level, |
| 6606 | unsigned char message ) |
| 6607 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6608 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6609 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 6610 | if( ssl == NULL || ssl->conf == NULL ) |
| 6611 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6612 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6613 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6614 | 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] | 6615 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6616 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6617 | ssl->out_msglen = 2; |
| 6618 | ssl->out_msg[0] = level; |
| 6619 | ssl->out_msg[1] = message; |
| 6620 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 6621 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6622 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6623 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6624 | return( ret ); |
| 6625 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6626 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6627 | |
| 6628 | return( 0 ); |
| 6629 | } |
| 6630 | |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6631 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6632 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 6633 | { |
| 6634 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6635 | if( session->peer_cert != NULL ) |
| 6636 | { |
| 6637 | mbedtls_x509_crt_free( session->peer_cert ); |
| 6638 | mbedtls_free( session->peer_cert ); |
| 6639 | session->peer_cert = NULL; |
| 6640 | } |
| 6641 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6642 | if( session->peer_cert_digest != NULL ) |
| 6643 | { |
| 6644 | /* Zeroization is not necessary. */ |
| 6645 | mbedtls_free( session->peer_cert_digest ); |
| 6646 | session->peer_cert_digest = NULL; |
| 6647 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 6648 | session->peer_cert_digest_len = 0; |
| 6649 | } |
| 6650 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6651 | } |
| 6652 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 6653 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6654 | /* |
| 6655 | * Handshake functions |
| 6656 | */ |
Hanno Becker | 2148993 | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6657 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6658 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6659 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6660 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6661 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6662 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6664 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6665 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6666 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6667 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6668 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6669 | ssl->state++; |
| 6670 | return( 0 ); |
| 6671 | } |
| 6672 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6673 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6674 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6675 | } |
| 6676 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6677 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6678 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6679 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6680 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6681 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6682 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6683 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6684 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6685 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6686 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6687 | ssl->state++; |
| 6688 | return( 0 ); |
| 6689 | } |
| 6690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6691 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6692 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6693 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6694 | |
Hanno Becker | 2148993 | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6695 | #else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6696 | /* Some certificate support -> implement write and parse */ |
| 6697 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6698 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6699 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6700 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6701 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6702 | const mbedtls_x509_crt *crt; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6703 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6704 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6705 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6706 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6707 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6708 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6709 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6710 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6711 | ssl->state++; |
| 6712 | return( 0 ); |
| 6713 | } |
| 6714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6715 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6716 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6717 | { |
| 6718 | if( ssl->client_auth == 0 ) |
| 6719 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6720 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6721 | ssl->state++; |
| 6722 | return( 0 ); |
| 6723 | } |
| 6724 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6725 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6726 | /* |
| 6727 | * If using SSLv3 and got no cert, send an Alert message |
| 6728 | * (otherwise an empty Certificate message will be sent). |
| 6729 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6730 | if( mbedtls_ssl_own_cert( ssl ) == NULL && |
| 6731 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6732 | { |
| 6733 | ssl->out_msglen = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6734 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
| 6735 | ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING; |
| 6736 | ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6737 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6738 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6739 | goto write_msg; |
| 6740 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6741 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6742 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6743 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6744 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6745 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6746 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6747 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6748 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6749 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); |
| 6750 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6751 | } |
| 6752 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6753 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6754 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6755 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6756 | |
| 6757 | /* |
| 6758 | * 0 . 0 handshake type |
| 6759 | * 1 . 3 handshake length |
| 6760 | * 4 . 6 length of all certs |
| 6761 | * 7 . 9 length of cert. 1 |
| 6762 | * 10 . n-1 peer certificate |
| 6763 | * n . n+2 length of cert. 2 |
| 6764 | * n+3 . ... upper level cert, etc. |
| 6765 | */ |
| 6766 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6767 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6768 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 6769 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6770 | { |
| 6771 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6772 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6773 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6774 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6775 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6776 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6777 | } |
| 6778 | |
| 6779 | ssl->out_msg[i ] = (unsigned char)( n >> 16 ); |
| 6780 | ssl->out_msg[i + 1] = (unsigned char)( n >> 8 ); |
| 6781 | ssl->out_msg[i + 2] = (unsigned char)( n ); |
| 6782 | |
| 6783 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6784 | i += n; crt = crt->next; |
| 6785 | } |
| 6786 | |
| 6787 | ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 ); |
| 6788 | ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 ); |
| 6789 | ssl->out_msg[6] = (unsigned char)( ( i - 7 ) ); |
| 6790 | |
| 6791 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6792 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6793 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6794 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 6795 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6796 | write_msg: |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6797 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6798 | |
| 6799 | ssl->state++; |
| 6800 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6801 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6802 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6803 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6804 | return( ret ); |
| 6805 | } |
| 6806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6807 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6808 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 6809 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6810 | } |
| 6811 | |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6812 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6813 | |
| 6814 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6815 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6816 | unsigned char *crt_buf, |
| 6817 | size_t crt_buf_len ) |
| 6818 | { |
| 6819 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6820 | |
| 6821 | if( peer_crt == NULL ) |
| 6822 | return( -1 ); |
| 6823 | |
| 6824 | if( peer_crt->raw.len != crt_buf_len ) |
| 6825 | return( -1 ); |
| 6826 | |
k-stachowiak | 95b68ef | 2019-09-16 12:21:00 +0200 | [diff] [blame] | 6827 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6828 | } |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6829 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6830 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6831 | unsigned char *crt_buf, |
| 6832 | size_t crt_buf_len ) |
| 6833 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6834 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6835 | unsigned char const * const peer_cert_digest = |
| 6836 | ssl->session->peer_cert_digest; |
| 6837 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6838 | ssl->session->peer_cert_digest_type; |
| 6839 | mbedtls_md_info_t const * const digest_info = |
| 6840 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6841 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6842 | size_t digest_len; |
| 6843 | |
| 6844 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6845 | return( -1 ); |
| 6846 | |
| 6847 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6848 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6849 | return( -1 ); |
| 6850 | |
| 6851 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6852 | if( ret != 0 ) |
| 6853 | return( -1 ); |
| 6854 | |
| 6855 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6856 | } |
| 6857 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6858 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6859 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6860 | /* |
| 6861 | * Once the certificate message is read, parse it into a cert chain and |
| 6862 | * perform basic checks, but leave actual verification to the caller |
| 6863 | */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6864 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6865 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6866 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6867 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6868 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6869 | int crt_cnt=0; |
| 6870 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6871 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 6872 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6873 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6874 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6875 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6876 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6877 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6878 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6879 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6880 | } |
| 6881 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6882 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || |
| 6883 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6884 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6885 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6886 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6887 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6888 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6889 | } |
| 6890 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6891 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6892 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6893 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6894 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6895 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6896 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6897 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6898 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6899 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6900 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6901 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6902 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6903 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6904 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6905 | } |
| 6906 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6907 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6908 | i += 3; |
| 6909 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6910 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6911 | while( i < ssl->in_hslen ) |
| 6912 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6913 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6914 | if ( i + 3 > ssl->in_hslen ) { |
| 6915 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6916 | mbedtls_ssl_send_alert_message( ssl, |
| 6917 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6918 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6919 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 6920 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6921 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6922 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6923 | if( ssl->in_msg[i] != 0 ) |
| 6924 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6925 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6926 | mbedtls_ssl_send_alert_message( ssl, |
| 6927 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6928 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6929 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6930 | } |
| 6931 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6932 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6933 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6934 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6935 | i += 3; |
| 6936 | |
| 6937 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6938 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6939 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6940 | mbedtls_ssl_send_alert_message( ssl, |
| 6941 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6942 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6943 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6946 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6947 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6948 | if( crt_cnt++ == 0 && |
| 6949 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6950 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6951 | { |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6952 | /* During client-side renegotiation, check that the server's |
| 6953 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6954 | * mitigating the triple handshake attack. On success, reuse |
| 6955 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6956 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6957 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6958 | &ssl->in_msg[i], |
| 6959 | n ) != 0 ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6960 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6961 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6962 | mbedtls_ssl_send_alert_message( ssl, |
| 6963 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6964 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6965 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6966 | } |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6967 | |
| 6968 | /* Now we can safely free the original chain. */ |
| 6969 | ssl_clear_peer_cert( ssl->session ); |
| 6970 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6971 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6972 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6973 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6974 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6975 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6976 | #else |
Hanno Becker | 353a6f0 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 6977 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6978 | * it in-place from the input buffer instead of making a copy. */ |
| 6979 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6980 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6981 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6982 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6983 | case 0: /*ok*/ |
| 6984 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6985 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6986 | prior certificate was already trusted. */ |
| 6987 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6988 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6989 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6990 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6991 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6992 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6993 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6994 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6995 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6996 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6997 | default: |
| 6998 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6999 | crt_parse_der_failed: |
| 7000 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 7001 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 7002 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7003 | } |
| 7004 | |
| 7005 | i += n; |
| 7006 | } |
| 7007 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7008 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7009 | return( 0 ); |
| 7010 | } |
| 7011 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7012 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7013 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 7014 | { |
| 7015 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7016 | return( -1 ); |
| 7017 | |
| 7018 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 7019 | /* |
| 7020 | * Check if the client sent an empty certificate |
| 7021 | */ |
| 7022 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
| 7023 | { |
| 7024 | if( ssl->in_msglen == 2 && |
| 7025 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 7026 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 7027 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 7028 | { |
| 7029 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); |
| 7030 | return( 0 ); |
| 7031 | } |
| 7032 | |
| 7033 | return( -1 ); |
| 7034 | } |
| 7035 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 7036 | |
| 7037 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 7038 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7039 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 7040 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 7041 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 7042 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 7043 | { |
| 7044 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 7045 | return( 0 ); |
| 7046 | } |
| 7047 | |
| 7048 | return( -1 ); |
| 7049 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 7050 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7051 | } |
| 7052 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7053 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7054 | /* Check if a certificate message is expected. |
| 7055 | * Return either |
| 7056 | * - SSL_CERTIFICATE_EXPECTED, or |
| 7057 | * - SSL_CERTIFICATE_SKIP |
| 7058 | * indicating whether a Certificate message is expected or not. |
| 7059 | */ |
| 7060 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 7061 | #define SSL_CERTIFICATE_SKIP 1 |
| 7062 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 7063 | int authmode ) |
| 7064 | { |
| 7065 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 7066 | ssl->handshake->ciphersuite_info; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7067 | |
| 7068 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 7069 | return( SSL_CERTIFICATE_SKIP ); |
| 7070 | |
| 7071 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7072 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7073 | { |
| 7074 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 7075 | return( SSL_CERTIFICATE_SKIP ); |
| 7076 | |
| 7077 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 7078 | { |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7079 | ssl->session_negotiate->verify_result = |
| 7080 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 7081 | return( SSL_CERTIFICATE_SKIP ); |
| 7082 | } |
| 7083 | } |
Hanno Becker | 84d9d27 | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 7084 | #else |
| 7085 | ((void) authmode); |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7086 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7087 | |
| 7088 | return( SSL_CERTIFICATE_EXPECTED ); |
| 7089 | } |
| 7090 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7091 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 7092 | int authmode, |
| 7093 | mbedtls_x509_crt *chain, |
| 7094 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7095 | { |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7096 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7097 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 7098 | ssl->handshake->ciphersuite_info; |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7099 | int have_ca_chain = 0; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7100 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 7101 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 7102 | void *p_vrfy; |
| 7103 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7104 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 7105 | return( 0 ); |
| 7106 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 7107 | if( ssl->f_vrfy != NULL ) |
| 7108 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 7109 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 7110 | f_vrfy = ssl->f_vrfy; |
| 7111 | p_vrfy = ssl->p_vrfy; |
| 7112 | } |
| 7113 | else |
| 7114 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 7115 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 7116 | f_vrfy = ssl->conf->f_vrfy; |
| 7117 | p_vrfy = ssl->conf->p_vrfy; |
| 7118 | } |
| 7119 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7120 | /* |
| 7121 | * Main check: verify certificate |
| 7122 | */ |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7123 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 7124 | if( ssl->conf->f_ca_cb != NULL ) |
| 7125 | { |
| 7126 | ((void) rs_ctx); |
| 7127 | have_ca_chain = 1; |
| 7128 | |
| 7129 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
Jarno Lamsa | 9822c0d | 2019-04-01 16:59:48 +0300 | [diff] [blame] | 7130 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7131 | chain, |
| 7132 | ssl->conf->f_ca_cb, |
| 7133 | ssl->conf->p_ca_cb, |
| 7134 | ssl->conf->cert_profile, |
| 7135 | ssl->hostname, |
| 7136 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 7137 | f_vrfy, p_vrfy ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7138 | } |
| 7139 | else |
| 7140 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 7141 | { |
| 7142 | mbedtls_x509_crt *ca_chain; |
| 7143 | mbedtls_x509_crl *ca_crl; |
| 7144 | |
| 7145 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7146 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 7147 | { |
| 7148 | ca_chain = ssl->handshake->sni_ca_chain; |
| 7149 | ca_crl = ssl->handshake->sni_ca_crl; |
| 7150 | } |
| 7151 | else |
| 7152 | #endif |
| 7153 | { |
| 7154 | ca_chain = ssl->conf->ca_chain; |
| 7155 | ca_crl = ssl->conf->ca_crl; |
| 7156 | } |
| 7157 | |
| 7158 | if( ca_chain != NULL ) |
| 7159 | have_ca_chain = 1; |
| 7160 | |
| 7161 | ret = mbedtls_x509_crt_verify_restartable( |
| 7162 | chain, |
| 7163 | ca_chain, ca_crl, |
| 7164 | ssl->conf->cert_profile, |
| 7165 | ssl->hostname, |
| 7166 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 7167 | f_vrfy, p_vrfy, rs_ctx ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7168 | } |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7169 | |
| 7170 | if( ret != 0 ) |
| 7171 | { |
| 7172 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 7173 | } |
| 7174 | |
| 7175 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7176 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 7177 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 7178 | #endif |
| 7179 | |
| 7180 | /* |
| 7181 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 7182 | */ |
| 7183 | |
| 7184 | #if defined(MBEDTLS_ECP_C) |
| 7185 | { |
| 7186 | const mbedtls_pk_context *pk = &chain->pk; |
| 7187 | |
| 7188 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 7189 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 7190 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 7191 | { |
| 7192 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 7193 | |
| 7194 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 7195 | if( ret == 0 ) |
| 7196 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
| 7197 | } |
| 7198 | } |
| 7199 | #endif /* MBEDTLS_ECP_C */ |
| 7200 | |
| 7201 | if( mbedtls_ssl_check_cert_usage( chain, |
| 7202 | ciphersuite_info, |
| 7203 | ! ssl->conf->endpoint, |
| 7204 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 7205 | { |
| 7206 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 7207 | if( ret == 0 ) |
| 7208 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
| 7209 | } |
| 7210 | |
| 7211 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 7212 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 7213 | * with details encoded in the verification flags. All other kinds |
| 7214 | * of error codes, including those from the user provided f_vrfy |
| 7215 | * functions, are treated as fatal and lead to a failure of |
| 7216 | * ssl_parse_certificate even if verification was optional. */ |
| 7217 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 7218 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 7219 | ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) |
| 7220 | { |
| 7221 | ret = 0; |
| 7222 | } |
| 7223 | |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7224 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7225 | { |
| 7226 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 7227 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 7228 | } |
| 7229 | |
| 7230 | if( ret != 0 ) |
| 7231 | { |
| 7232 | uint8_t alert; |
| 7233 | |
| 7234 | /* The certificate may have been rejected for several reasons. |
| 7235 | Pick one and send the corresponding alert. Which alert to send |
| 7236 | may be a subject of debate in some cases. */ |
| 7237 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 7238 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 7239 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 7240 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 7241 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 7242 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7243 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 7244 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7245 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 7246 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7247 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 7248 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7249 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 7250 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7251 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 7252 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 7253 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 7254 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 7255 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 7256 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 7257 | else |
| 7258 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 7259 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7260 | alert ); |
| 7261 | } |
| 7262 | |
| 7263 | #if defined(MBEDTLS_DEBUG_C) |
| 7264 | if( ssl->session_negotiate->verify_result != 0 ) |
| 7265 | { |
| 7266 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", |
| 7267 | ssl->session_negotiate->verify_result ) ); |
| 7268 | } |
| 7269 | else |
| 7270 | { |
| 7271 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 7272 | } |
| 7273 | #endif /* MBEDTLS_DEBUG_C */ |
| 7274 | |
| 7275 | return( ret ); |
| 7276 | } |
| 7277 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7278 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7279 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 7280 | unsigned char *start, size_t len ) |
| 7281 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 7282 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7283 | /* Remember digest of the peer's end-CRT. */ |
| 7284 | ssl->session_negotiate->peer_cert_digest = |
| 7285 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 7286 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 7287 | { |
| 7288 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
irwir | 40883e9 | 2019-09-21 17:55:33 +0300 | [diff] [blame] | 7289 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7290 | mbedtls_ssl_send_alert_message( ssl, |
| 7291 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7292 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7293 | |
| 7294 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7295 | } |
| 7296 | |
| 7297 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 7298 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 7299 | start, len, |
| 7300 | ssl->session_negotiate->peer_cert_digest ); |
| 7301 | |
| 7302 | ssl->session_negotiate->peer_cert_digest_type = |
| 7303 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7304 | ssl->session_negotiate->peer_cert_digest_len = |
| 7305 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7306 | |
| 7307 | return( ret ); |
| 7308 | } |
| 7309 | |
| 7310 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 7311 | unsigned char *start, size_t len ) |
| 7312 | { |
| 7313 | unsigned char *end = start + len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 7314 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7315 | |
| 7316 | /* Make a copy of the peer's raw public key. */ |
| 7317 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 7318 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 7319 | &ssl->handshake->peer_pubkey ); |
| 7320 | if( ret != 0 ) |
| 7321 | { |
| 7322 | /* We should have parsed the public key before. */ |
| 7323 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7324 | } |
| 7325 | |
| 7326 | return( 0 ); |
| 7327 | } |
| 7328 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7329 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7330 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 7331 | { |
| 7332 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7333 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7334 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7335 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7336 | ? ssl->handshake->sni_authmode |
| 7337 | : ssl->conf->authmode; |
| 7338 | #else |
| 7339 | const int authmode = ssl->conf->authmode; |
| 7340 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7341 | void *rs_ctx = NULL; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7342 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7343 | |
| 7344 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 7345 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7346 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 7347 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7348 | { |
| 7349 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7350 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7351 | } |
| 7352 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7353 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7354 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7355 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7356 | { |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7357 | chain = ssl->handshake->ecrs_peer_cert; |
| 7358 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7359 | goto crt_verify; |
| 7360 | } |
| 7361 | #endif |
| 7362 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 7363 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7364 | { |
| 7365 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7366 | let it decide whether to alert. */ |
| 7367 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7368 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7369 | } |
| 7370 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7371 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7372 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 7373 | { |
| 7374 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7375 | |
| 7376 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7377 | ret = 0; |
| 7378 | else |
| 7379 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7380 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7381 | goto exit; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7382 | } |
| 7383 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7384 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7385 | /* Clear existing peer CRT structure in case we tried to |
| 7386 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7387 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7388 | |
| 7389 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7390 | if( chain == NULL ) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7391 | { |
| 7392 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7393 | sizeof( mbedtls_x509_crt ) ) ); |
| 7394 | mbedtls_ssl_send_alert_message( ssl, |
| 7395 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7396 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7397 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7398 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7399 | goto exit; |
| 7400 | } |
| 7401 | mbedtls_x509_crt_init( chain ); |
| 7402 | |
| 7403 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7404 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7405 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7406 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7407 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7408 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7409 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7410 | |
| 7411 | crt_verify: |
| 7412 | if( ssl->handshake->ecrs_enabled) |
| 7413 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 7414 | #endif |
| 7415 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7416 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7417 | chain, rs_ctx ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7418 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7419 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7420 | |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7421 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7422 | { |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7423 | unsigned char *crt_start, *pk_start; |
| 7424 | size_t crt_len, pk_len; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7425 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7426 | /* We parse the CRT chain without copying, so |
| 7427 | * these pointers point into the input buffer, |
| 7428 | * and are hence still valid after freeing the |
| 7429 | * CRT chain. */ |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7430 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7431 | crt_start = chain->raw.p; |
| 7432 | crt_len = chain->raw.len; |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7433 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7434 | pk_start = chain->pk_raw.p; |
| 7435 | pk_len = chain->pk_raw.len; |
| 7436 | |
| 7437 | /* Free the CRT structures before computing |
| 7438 | * digest and copying the peer's public key. */ |
| 7439 | mbedtls_x509_crt_free( chain ); |
| 7440 | mbedtls_free( chain ); |
| 7441 | chain = NULL; |
| 7442 | |
| 7443 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7444 | if( ret != 0 ) |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7445 | goto exit; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7446 | |
| 7447 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 7448 | if( ret != 0 ) |
| 7449 | goto exit; |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7450 | } |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7451 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7452 | /* Pass ownership to session structure. */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7453 | ssl->session_negotiate->peer_cert = chain; |
| 7454 | chain = NULL; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7455 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7456 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7457 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7458 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7459 | exit: |
| 7460 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7461 | if( ret == 0 ) |
| 7462 | ssl->state++; |
| 7463 | |
| 7464 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7465 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7466 | { |
| 7467 | ssl->handshake->ecrs_peer_cert = chain; |
| 7468 | chain = NULL; |
| 7469 | } |
| 7470 | #endif |
| 7471 | |
| 7472 | if( chain != NULL ) |
| 7473 | { |
| 7474 | mbedtls_x509_crt_free( chain ); |
| 7475 | mbedtls_free( chain ); |
| 7476 | } |
| 7477 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7478 | return( ret ); |
| 7479 | } |
Hanno Becker | 2148993 | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 7480 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7481 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7482 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7483 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 7484 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7485 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7486 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7487 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7488 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7489 | ssl->out_msglen = 1; |
| 7490 | ssl->out_msg[0] = 1; |
| 7491 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7492 | ssl->state++; |
| 7493 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7494 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7495 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7496 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7497 | return( ret ); |
| 7498 | } |
| 7499 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7500 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7501 | |
| 7502 | return( 0 ); |
| 7503 | } |
| 7504 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7505 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7506 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 7507 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7508 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7509 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7510 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 7511 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7512 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7513 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7514 | return( ret ); |
| 7515 | } |
| 7516 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7517 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7518 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7519 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7520 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7521 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7522 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7523 | } |
| 7524 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 7525 | /* CCS records are only accepted if they have length 1 and content '1', |
| 7526 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7527 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7528 | /* |
| 7529 | * Switch to our negotiated transform and session parameters for inbound |
| 7530 | * data. |
| 7531 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7532 | 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] | 7533 | ssl->transform_in = ssl->transform_negotiate; |
| 7534 | ssl->session_in = ssl->session_negotiate; |
| 7535 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7536 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7537 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7538 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7539 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7540 | ssl_dtls_replay_reset( ssl ); |
| 7541 | #endif |
| 7542 | |
| 7543 | /* Increment epoch */ |
| 7544 | if( ++ssl->in_epoch == 0 ) |
| 7545 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7546 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7547 | /* This is highly unlikely to happen for legitimate reasons, so |
| 7548 | 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] | 7549 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7550 | } |
| 7551 | } |
| 7552 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7553 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7554 | memset( ssl->in_ctr, 0, 8 ); |
| 7555 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 7556 | mbedtls_ssl_update_in_pointers( ssl ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7557 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7558 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7559 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7560 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7561 | 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] | 7562 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7563 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7564 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7565 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7566 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7567 | } |
| 7568 | } |
| 7569 | #endif |
| 7570 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7571 | ssl->state++; |
| 7572 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7573 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7574 | |
| 7575 | return( 0 ); |
| 7576 | } |
| 7577 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7578 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 7579 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7580 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 7581 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7582 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7583 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7584 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7585 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7586 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7587 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7588 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7589 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7590 | #if defined(MBEDTLS_SHA512_C) |
| 7591 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7592 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 7593 | else |
| 7594 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7595 | #if defined(MBEDTLS_SHA256_C) |
| 7596 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7597 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7598 | else |
| 7599 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7600 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 7601 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7602 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7603 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 7604 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7605 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 7606 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7607 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7608 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7609 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7610 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7611 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 ); |
| 7612 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7613 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7614 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7615 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7616 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 7617 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7618 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 7619 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7620 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7621 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7622 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7623 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7624 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 7625 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7626 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7627 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7628 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7629 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7630 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7631 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7632 | } |
| 7633 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7634 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7635 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7636 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7637 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7638 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7639 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7640 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7641 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7642 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7643 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7644 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7645 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 7646 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7647 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7648 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7649 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7650 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7651 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7652 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7653 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7654 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7655 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7656 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7657 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7658 | } |
| 7659 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7660 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7661 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7662 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7663 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7664 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7665 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7666 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7667 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7668 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7669 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7670 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7671 | #if defined(MBEDTLS_SHA256_C) |
| 7672 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7673 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7674 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7675 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7676 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 7677 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7678 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7679 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7680 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7681 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7682 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7683 | #if defined(MBEDTLS_SHA512_C) |
| 7684 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7685 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7686 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7687 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7688 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7689 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7690 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7691 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7692 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7693 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7694 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7696 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7697 | static void ssl_calc_finished_ssl( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7698 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7699 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7700 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7701 | mbedtls_md5_context md5; |
| 7702 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7703 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7704 | unsigned char padbuf[48]; |
| 7705 | unsigned char md5sum[16]; |
| 7706 | unsigned char sha1sum[20]; |
| 7707 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7708 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7709 | if( !session ) |
| 7710 | session = ssl->session; |
| 7711 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7712 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7713 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7714 | mbedtls_md5_init( &md5 ); |
| 7715 | mbedtls_sha1_init( &sha1 ); |
| 7716 | |
| 7717 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7718 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7719 | |
| 7720 | /* |
| 7721 | * SSLv3: |
| 7722 | * hash = |
| 7723 | * MD5( master + pad2 + |
| 7724 | * MD5( handshake + sender + master + pad1 ) ) |
| 7725 | * + SHA1( master + pad2 + |
| 7726 | * SHA1( handshake + sender + master + pad1 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7727 | */ |
| 7728 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7729 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7730 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7731 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7732 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7733 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7734 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7735 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7736 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7737 | #endif |
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 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7740 | : "SRVR"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7741 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7742 | memset( padbuf, 0x36, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7743 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7744 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); |
| 7745 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7746 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7747 | mbedtls_md5_finish_ret( &md5, md5sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7748 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7749 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); |
| 7750 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7751 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); |
| 7752 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7753 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7754 | memset( padbuf, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7755 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7756 | mbedtls_md5_starts_ret( &md5 ); |
| 7757 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7758 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7759 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); |
| 7760 | mbedtls_md5_finish_ret( &md5, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7761 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7762 | mbedtls_sha1_starts_ret( &sha1 ); |
| 7763 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7764 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); |
| 7765 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); |
| 7766 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7767 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7768 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7769 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7770 | mbedtls_md5_free( &md5 ); |
| 7771 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7772 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7773 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7774 | mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) ); |
| 7775 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7777 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7778 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7779 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7780 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7781 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7782 | static void ssl_calc_finished_tls( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7783 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7784 | { |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7785 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7786 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7787 | mbedtls_md5_context md5; |
| 7788 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7789 | unsigned char padbuf[36]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7790 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7791 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7792 | if( !session ) |
| 7793 | session = ssl->session; |
| 7794 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7795 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7796 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7797 | mbedtls_md5_init( &md5 ); |
| 7798 | mbedtls_sha1_init( &sha1 ); |
| 7799 | |
| 7800 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7801 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7802 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7803 | /* |
| 7804 | * TLSv1: |
| 7805 | * hash = PRF( master, finished_label, |
| 7806 | * MD5( handshake ) + SHA1( handshake ) )[0..11] |
| 7807 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7808 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7809 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7810 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7811 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7812 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7813 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7814 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7815 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7816 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7817 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7818 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7819 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7820 | ? "client finished" |
| 7821 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7822 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7823 | mbedtls_md5_finish_ret( &md5, padbuf ); |
| 7824 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7825 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7826 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7827 | padbuf, 36, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7828 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7829 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7830 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7831 | mbedtls_md5_free( &md5 ); |
| 7832 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7833 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7834 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7835 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7836 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7837 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7838 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7839 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7840 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7841 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7842 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7843 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7844 | { |
| 7845 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7846 | const char *sender; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7847 | unsigned char padbuf[32]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7848 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7849 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7850 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7851 | psa_status_t status; |
| 7852 | #else |
| 7853 | mbedtls_sha256_context sha256; |
| 7854 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7855 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7856 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7857 | if( !session ) |
| 7858 | session = ssl->session; |
| 7859 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7860 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7861 | ? "client finished" |
| 7862 | : "server finished"; |
| 7863 | |
| 7864 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7865 | sha256_psa = psa_hash_operation_init(); |
| 7866 | |
| 7867 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 7868 | |
| 7869 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 7870 | if( status != PSA_SUCCESS ) |
| 7871 | { |
| 7872 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7873 | return; |
| 7874 | } |
| 7875 | |
| 7876 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7877 | if( status != PSA_SUCCESS ) |
| 7878 | { |
| 7879 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7880 | return; |
| 7881 | } |
| 7882 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 7883 | #else |
| 7884 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7885 | mbedtls_sha256_init( &sha256 ); |
| 7886 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7887 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7888 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7889 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7890 | |
| 7891 | /* |
| 7892 | * TLSv1.2: |
| 7893 | * hash = PRF( master, finished_label, |
| 7894 | * Hash( handshake ) )[0.11] |
| 7895 | */ |
| 7896 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7897 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7898 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7899 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7900 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7901 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7902 | mbedtls_sha256_finish_ret( &sha256, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7903 | mbedtls_sha256_free( &sha256 ); |
| 7904 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7905 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7906 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7907 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7908 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7909 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7910 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7911 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7912 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7913 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7914 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7915 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7916 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7917 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7918 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7919 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7920 | { |
| 7921 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7922 | const char *sender; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7923 | unsigned char padbuf[48]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7924 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7925 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7926 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7927 | psa_status_t status; |
| 7928 | #else |
| 7929 | mbedtls_sha512_context sha512; |
| 7930 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7931 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7932 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7933 | if( !session ) |
| 7934 | session = ssl->session; |
| 7935 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7936 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7937 | ? "client finished" |
| 7938 | : "server finished"; |
| 7939 | |
| 7940 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7941 | sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7942 | |
| 7943 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7944 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7945 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7946 | if( status != PSA_SUCCESS ) |
| 7947 | { |
| 7948 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7949 | return; |
| 7950 | } |
| 7951 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7952 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7953 | if( status != PSA_SUCCESS ) |
| 7954 | { |
| 7955 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7956 | return; |
| 7957 | } |
| 7958 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7959 | #else |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7960 | mbedtls_sha512_init( &sha512 ); |
| 7961 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7962 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7963 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7964 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7965 | |
| 7966 | /* |
| 7967 | * TLSv1.2: |
| 7968 | * hash = PRF( master, finished_label, |
| 7969 | * Hash( handshake ) )[0.11] |
| 7970 | */ |
| 7971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7972 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7973 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7974 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7975 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7976 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7977 | mbedtls_sha512_finish_ret( &sha512, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7978 | mbedtls_sha512_free( &sha512 ); |
| 7979 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7980 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7981 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7982 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7983 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7984 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7985 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7986 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7987 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7988 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7989 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7990 | #endif /* MBEDTLS_SHA512_C */ |
| 7991 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7992 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7993 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7994 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7995 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7996 | |
| 7997 | /* |
| 7998 | * Free our handshake params |
| 7999 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 8000 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8001 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8002 | ssl->handshake = NULL; |
| 8003 | |
| 8004 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8005 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8006 | */ |
| 8007 | if( ssl->transform ) |
| 8008 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8009 | mbedtls_ssl_transform_free( ssl->transform ); |
| 8010 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8011 | } |
| 8012 | ssl->transform = ssl->transform_negotiate; |
| 8013 | ssl->transform_negotiate = NULL; |
| 8014 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8015 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8016 | } |
| 8017 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8018 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8019 | { |
| 8020 | int resume = ssl->handshake->resume; |
| 8021 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8022 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8023 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8024 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8025 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8026 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8027 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8028 | ssl->renego_records_seen = 0; |
| 8029 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8030 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8031 | |
| 8032 | /* |
| 8033 | * Free the previous session and switch in the current one |
| 8034 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8035 | if( ssl->session ) |
| 8036 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8037 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 8038 | /* RFC 7366 3.1: keep the EtM state */ |
| 8039 | ssl->session_negotiate->encrypt_then_mac = |
| 8040 | ssl->session->encrypt_then_mac; |
| 8041 | #endif |
| 8042 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8043 | mbedtls_ssl_session_free( ssl->session ); |
| 8044 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8045 | } |
| 8046 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8047 | ssl->session_negotiate = NULL; |
| 8048 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8049 | /* |
| 8050 | * Add cache entry |
| 8051 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8052 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 8053 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 8054 | resume == 0 ) |
| 8055 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8056 | 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] | 8057 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 8058 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8059 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8060 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8061 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8062 | ssl->handshake->flight != NULL ) |
| 8063 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 8064 | /* Cancel handshake timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 8065 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 8066 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8067 | /* Keep last flight around in case we need to resend it: |
| 8068 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8069 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8070 | } |
| 8071 | else |
| 8072 | #endif |
| 8073 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 8074 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8075 | ssl->state++; |
| 8076 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8077 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8078 | } |
| 8079 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8080 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8081 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8082 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8083 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8084 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8085 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 8086 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 8087 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8088 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8089 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 8090 | /* |
| 8091 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 8092 | * may define some other value. Currently (early 2016), no defined |
| 8093 | * ciphersuite does this (and this is unlikely to change as activity has |
| 8094 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 8095 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8096 | hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8097 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8098 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8099 | ssl->verify_data_len = hash_len; |
| 8100 | 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] | 8101 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8102 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8103 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8104 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 8105 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8106 | |
| 8107 | /* |
| 8108 | * In case of session resuming, invert the client and server |
| 8109 | * ChangeCipherSpec messages order. |
| 8110 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8111 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8112 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8113 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8114 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8115 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8116 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8117 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8118 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8119 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8120 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8121 | } |
| 8122 | else |
| 8123 | ssl->state++; |
| 8124 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8125 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 8126 | * Switch to our negotiated transform and session parameters for outbound |
| 8127 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8128 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8129 | 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] | 8130 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8131 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8132 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8133 | { |
| 8134 | unsigned char i; |
| 8135 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8136 | /* Remember current epoch settings for resending */ |
| 8137 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8138 | 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] | 8139 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8140 | /* Set sequence_number to zero */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8141 | memset( ssl->cur_out_ctr + 2, 0, 6 ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8142 | |
| 8143 | /* Increment epoch */ |
| 8144 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8145 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8146 | break; |
| 8147 | |
| 8148 | /* The loop goes to its end iff the counter is wrapping */ |
| 8149 | if( i == 0 ) |
| 8150 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8151 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 8152 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8153 | } |
| 8154 | } |
| 8155 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8156 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8157 | memset( ssl->cur_out_ctr, 0, 8 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8158 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8159 | ssl->transform_out = ssl->transform_negotiate; |
| 8160 | ssl->session_out = ssl->session_negotiate; |
| 8161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8162 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 8163 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 8164 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8165 | 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] | 8166 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8167 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 8168 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 8169 | } |
| 8170 | } |
| 8171 | #endif |
| 8172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8173 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8174 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8175 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 8176 | #endif |
| 8177 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 8178 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8179 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 8180 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8181 | return( ret ); |
| 8182 | } |
| 8183 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 8184 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8185 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8186 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 8187 | { |
| 8188 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 8189 | return( ret ); |
| 8190 | } |
| 8191 | #endif |
| 8192 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8193 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8194 | |
| 8195 | return( 0 ); |
| 8196 | } |
| 8197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8198 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8199 | #define SSL_MAX_HASH_LEN 36 |
| 8200 | #else |
| 8201 | #define SSL_MAX_HASH_LEN 12 |
| 8202 | #endif |
| 8203 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8204 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8205 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 8206 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8207 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8208 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8210 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8211 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8212 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8213 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 8214 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8215 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8216 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8217 | return( ret ); |
| 8218 | } |
| 8219 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8220 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8221 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8222 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 8223 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8224 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8225 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8226 | } |
| 8227 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8228 | /* 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] | 8229 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 8230 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8231 | hash_len = 36; |
| 8232 | else |
| 8233 | #endif |
| 8234 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8236 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || |
| 8237 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8238 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8239 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 8240 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8241 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8242 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8243 | } |
| 8244 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8245 | 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] | 8246 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8247 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8248 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 8249 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8250 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8251 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8252 | } |
| 8253 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8254 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8255 | ssl->verify_data_len = hash_len; |
| 8256 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8257 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8258 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8259 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8260 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8261 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8262 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8263 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8264 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8265 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8266 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8267 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8268 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8269 | } |
| 8270 | else |
| 8271 | ssl->state++; |
| 8272 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8273 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8274 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8275 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8276 | #endif |
| 8277 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8278 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8279 | |
| 8280 | return( 0 ); |
| 8281 | } |
| 8282 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8283 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8284 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8285 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8286 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8287 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 8288 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 8289 | mbedtls_md5_init( &handshake->fin_md5 ); |
| 8290 | mbedtls_sha1_init( &handshake->fin_sha1 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8291 | mbedtls_md5_starts_ret( &handshake->fin_md5 ); |
| 8292 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8293 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8294 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 8295 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8296 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8297 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 8298 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 8299 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8300 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8301 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8302 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8303 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8304 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8305 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 8306 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 8307 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8308 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8309 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8310 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8311 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8312 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8313 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8314 | |
| 8315 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 8316 | |
| 8317 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 8318 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 8319 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 8320 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8321 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8322 | #if defined(MBEDTLS_DHM_C) |
| 8323 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8324 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8325 | #if defined(MBEDTLS_ECDH_C) |
| 8326 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8327 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8328 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8329 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 8330 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8331 | handshake->ecjpake_cache = NULL; |
| 8332 | handshake->ecjpake_cache_len = 0; |
| 8333 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8334 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8335 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 8336 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 8337 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 8338 | #endif |
| 8339 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8340 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8341 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 8342 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 8343 | |
| 8344 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 8345 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8346 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 8347 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8348 | } |
| 8349 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 8350 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8351 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8352 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 8353 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8354 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 8355 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 8356 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 8357 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8358 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 8359 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 8360 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8361 | } |
| 8362 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8363 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8364 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8365 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8366 | } |
| 8367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8368 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8369 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8370 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8371 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8372 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8373 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8374 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8375 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 8376 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8377 | |
| 8378 | /* |
| 8379 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 8380 | * Now allocate missing structures. |
| 8381 | */ |
| 8382 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8383 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8384 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8385 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8386 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8387 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8388 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8389 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8390 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8391 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 8392 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8393 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8394 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8395 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8396 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8397 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8398 | if( ssl->handshake == NULL || |
| 8399 | ssl->transform_negotiate == NULL || |
| 8400 | ssl->session_negotiate == NULL ) |
| 8401 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 8402 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8404 | mbedtls_free( ssl->handshake ); |
| 8405 | mbedtls_free( ssl->transform_negotiate ); |
| 8406 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8407 | |
| 8408 | ssl->handshake = NULL; |
| 8409 | ssl->transform_negotiate = NULL; |
| 8410 | ssl->session_negotiate = NULL; |
| 8411 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8412 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8413 | } |
| 8414 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8415 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8416 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 8417 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 8418 | ssl_handshake_params_init( ssl->handshake ); |
| 8419 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8420 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8421 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8422 | { |
| 8423 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8424 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8425 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 8426 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 8427 | else |
| 8428 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8429 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 8430 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8431 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8432 | #endif |
| 8433 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8434 | return( 0 ); |
| 8435 | } |
| 8436 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8437 | #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] | 8438 | /* Dummy cookie callbacks for defaults */ |
| 8439 | static int ssl_cookie_write_dummy( void *ctx, |
| 8440 | unsigned char **p, unsigned char *end, |
| 8441 | const unsigned char *cli_id, size_t cli_id_len ) |
| 8442 | { |
| 8443 | ((void) ctx); |
| 8444 | ((void) p); |
| 8445 | ((void) end); |
| 8446 | ((void) cli_id); |
| 8447 | ((void) cli_id_len); |
| 8448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8449 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8450 | } |
| 8451 | |
| 8452 | static int ssl_cookie_check_dummy( void *ctx, |
| 8453 | const unsigned char *cookie, size_t cookie_len, |
| 8454 | const unsigned char *cli_id, size_t cli_id_len ) |
| 8455 | { |
| 8456 | ((void) ctx); |
| 8457 | ((void) cookie); |
| 8458 | ((void) cookie_len); |
| 8459 | ((void) cli_id); |
| 8460 | ((void) cli_id_len); |
| 8461 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8462 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8463 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8464 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8465 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8466 | /* Once ssl->out_hdr as the address of the beginning of the |
| 8467 | * next outgoing record is set, deduce the other pointers. |
| 8468 | * |
| 8469 | * Note: For TLS, we save the implicit record sequence number |
| 8470 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 8471 | * and the caller has to make sure there's space for this. |
| 8472 | */ |
| 8473 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 8474 | void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 8475 | mbedtls_ssl_transform *transform ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8476 | { |
| 8477 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8478 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8479 | { |
| 8480 | ssl->out_ctr = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8481 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8482 | ssl->out_cid = ssl->out_ctr + 8; |
| 8483 | ssl->out_len = ssl->out_cid; |
| 8484 | if( transform != NULL ) |
| 8485 | ssl->out_len += transform->out_cid_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8486 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8487 | ssl->out_len = ssl->out_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8488 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8489 | ssl->out_iv = ssl->out_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8490 | } |
| 8491 | else |
| 8492 | #endif |
| 8493 | { |
| 8494 | ssl->out_ctr = ssl->out_hdr - 8; |
| 8495 | ssl->out_len = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8496 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8497 | ssl->out_cid = ssl->out_len; |
| 8498 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8499 | ssl->out_iv = ssl->out_hdr + 5; |
| 8500 | } |
| 8501 | |
| 8502 | /* Adjust out_msg to make space for explicit IV, if used. */ |
| 8503 | if( transform != NULL && |
| 8504 | ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 8505 | { |
| 8506 | ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen; |
| 8507 | } |
| 8508 | else |
| 8509 | ssl->out_msg = ssl->out_iv; |
| 8510 | } |
| 8511 | |
| 8512 | /* Once ssl->in_hdr as the address of the beginning of the |
| 8513 | * next incoming record is set, deduce the other pointers. |
| 8514 | * |
| 8515 | * Note: For TLS, we save the implicit record sequence number |
| 8516 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 8517 | * and the caller has to make sure there's space for this. |
| 8518 | */ |
| 8519 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 8520 | void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8521 | { |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8522 | /* This function sets the pointers to match the case |
| 8523 | * of unprotected TLS/DTLS records, with both ssl->in_iv |
| 8524 | * and ssl->in_msg pointing to the beginning of the record |
| 8525 | * content. |
| 8526 | * |
| 8527 | * When decrypting a protected record, ssl->in_msg |
| 8528 | * will be shifted to point to the beginning of the |
| 8529 | * record plaintext. |
| 8530 | */ |
| 8531 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8532 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8533 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8534 | { |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8535 | /* This sets the header pointers to match records |
| 8536 | * without CID. When we receive a record containing |
| 8537 | * a CID, the fields are shifted accordingly in |
| 8538 | * ssl_parse_record_header(). */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8539 | ssl->in_ctr = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8540 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8541 | ssl->in_cid = ssl->in_ctr + 8; |
| 8542 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8543 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8544 | ssl->in_len = ssl->in_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8545 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8546 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8547 | } |
| 8548 | else |
| 8549 | #endif |
| 8550 | { |
| 8551 | ssl->in_ctr = ssl->in_hdr - 8; |
| 8552 | ssl->in_len = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8553 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8554 | ssl->in_cid = ssl->in_len; |
| 8555 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8556 | ssl->in_iv = ssl->in_hdr + 5; |
| 8557 | } |
| 8558 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8559 | /* This will be adjusted at record decryption time. */ |
| 8560 | ssl->in_msg = ssl->in_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8561 | } |
| 8562 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8563 | /* |
| 8564 | * Initialize an SSL context |
| 8565 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 8566 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 8567 | { |
| 8568 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 8569 | } |
| 8570 | |
| 8571 | /* |
| 8572 | * Setup an SSL context |
| 8573 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8574 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 8575 | void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8576 | { |
| 8577 | /* Set the incoming and outgoing record pointers. */ |
| 8578 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8579 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8580 | { |
| 8581 | ssl->out_hdr = ssl->out_buf; |
| 8582 | ssl->in_hdr = ssl->in_buf; |
| 8583 | } |
| 8584 | else |
| 8585 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8586 | { |
| 8587 | ssl->out_hdr = ssl->out_buf + 8; |
| 8588 | ssl->in_hdr = ssl->in_buf + 8; |
| 8589 | } |
| 8590 | |
| 8591 | /* Derive other internal pointers. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 8592 | mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ ); |
| 8593 | mbedtls_ssl_update_in_pointers ( ssl ); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8594 | } |
| 8595 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8596 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 8597 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8598 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 8599 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8600 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8601 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8602 | |
| 8603 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 8604 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8605 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 8606 | |
| 8607 | /* Set to NULL in case of an error condition */ |
| 8608 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8609 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8610 | ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 8611 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8612 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8613 | 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] | 8614 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8615 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8616 | } |
| 8617 | |
| 8618 | ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 8619 | if( ssl->out_buf == NULL ) |
| 8620 | { |
| 8621 | 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] | 8622 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8623 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8624 | } |
| 8625 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 8626 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 8627 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8628 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8629 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8630 | |
| 8631 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8632 | |
| 8633 | error: |
| 8634 | mbedtls_free( ssl->in_buf ); |
| 8635 | mbedtls_free( ssl->out_buf ); |
| 8636 | |
| 8637 | ssl->conf = NULL; |
| 8638 | |
| 8639 | ssl->in_buf = NULL; |
| 8640 | ssl->out_buf = NULL; |
| 8641 | |
| 8642 | ssl->in_hdr = NULL; |
| 8643 | ssl->in_ctr = NULL; |
| 8644 | ssl->in_len = NULL; |
| 8645 | ssl->in_iv = NULL; |
| 8646 | ssl->in_msg = NULL; |
| 8647 | |
| 8648 | ssl->out_hdr = NULL; |
| 8649 | ssl->out_ctr = NULL; |
| 8650 | ssl->out_len = NULL; |
| 8651 | ssl->out_iv = NULL; |
| 8652 | ssl->out_msg = NULL; |
| 8653 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 8654 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8655 | } |
| 8656 | |
| 8657 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8658 | * Reset an initialized and used SSL context for re-use while retaining |
| 8659 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8660 | * |
| 8661 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 8662 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8663 | */ |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame^] | 8664 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8665 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 8666 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8667 | |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 8668 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \ |
| 8669 | !defined(MBEDTLS_SSL_SRV_C) |
| 8670 | ((void) partial); |
| 8671 | #endif |
| 8672 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8673 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8674 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8675 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 8676 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8678 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8679 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8680 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8681 | |
| 8682 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8683 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 8684 | 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] | 8685 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8686 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8687 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8688 | ssl->in_offt = NULL; |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 8689 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8690 | |
| 8691 | ssl->in_msgtype = 0; |
| 8692 | ssl->in_msglen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8693 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8694 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 8695 | ssl->in_epoch = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8696 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8697 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 8698 | ssl_dtls_replay_reset( ssl ); |
| 8699 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8700 | |
| 8701 | ssl->in_hslen = 0; |
| 8702 | ssl->nb_zero = 0; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 8703 | |
| 8704 | ssl->keep_current_message = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8705 | |
| 8706 | ssl->out_msgtype = 0; |
| 8707 | ssl->out_msglen = 0; |
| 8708 | ssl->out_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8709 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 8710 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 8711 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8712 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8713 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8714 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 8715 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8716 | ssl->transform_in = NULL; |
| 8717 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8718 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 8719 | ssl->session_in = NULL; |
| 8720 | ssl->session_out = NULL; |
| 8721 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8722 | memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8723 | |
| 8724 | #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] | 8725 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8726 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 8727 | { |
| 8728 | ssl->in_left = 0; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8729 | memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8730 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8731 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8732 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 8733 | if( mbedtls_ssl_hw_record_reset != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8734 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8735 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); |
| 8736 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8737 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8738 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); |
| 8739 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8740 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8741 | } |
| 8742 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8743 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8744 | if( ssl->transform ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8745 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8746 | mbedtls_ssl_transform_free( ssl->transform ); |
| 8747 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8748 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8749 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8750 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8751 | if( ssl->session ) |
| 8752 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8753 | mbedtls_ssl_session_free( ssl->session ); |
| 8754 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8755 | ssl->session = NULL; |
| 8756 | } |
| 8757 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8758 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8759 | ssl->alpn_chosen = NULL; |
| 8760 | #endif |
| 8761 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8762 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8763 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8764 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8765 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8766 | { |
| 8767 | mbedtls_free( ssl->cli_id ); |
| 8768 | ssl->cli_id = NULL; |
| 8769 | ssl->cli_id_len = 0; |
| 8770 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 8771 | #endif |
| 8772 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8773 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 8774 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8775 | |
| 8776 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8777 | } |
| 8778 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 8779 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8780 | * Reset an initialized and used SSL context for re-use while retaining |
| 8781 | * all application-set variables, function pointers and data. |
| 8782 | */ |
| 8783 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 8784 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame^] | 8785 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8786 | } |
| 8787 | |
| 8788 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8789 | * SSL set accessors |
| 8790 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8791 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8792 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8793 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8794 | } |
| 8795 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8796 | 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] | 8797 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8798 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 8799 | } |
| 8800 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8801 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8802 | 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] | 8803 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8804 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 8805 | } |
| 8806 | #endif |
| 8807 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8808 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8809 | void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8810 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8811 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8812 | } |
| 8813 | #endif |
| 8814 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8815 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8816 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 8817 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 8818 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8819 | { |
| 8820 | ssl->disable_datagram_packing = !allow_packing; |
| 8821 | } |
| 8822 | |
| 8823 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 8824 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8825 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8826 | conf->hs_timeout_min = min; |
| 8827 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8828 | } |
| 8829 | #endif |
| 8830 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8831 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8832 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8833 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8834 | } |
| 8835 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8836 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8837 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 8838 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8839 | void *p_vrfy ) |
| 8840 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8841 | conf->f_vrfy = f_vrfy; |
| 8842 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8843 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8844 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8845 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8846 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 8847 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8848 | void *p_rng ) |
| 8849 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 8850 | conf->f_rng = f_rng; |
| 8851 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8852 | } |
| 8853 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8854 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 8855 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8856 | void *p_dbg ) |
| 8857 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8858 | conf->f_dbg = f_dbg; |
| 8859 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8860 | } |
| 8861 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8862 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8863 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8864 | mbedtls_ssl_send_t *f_send, |
| 8865 | mbedtls_ssl_recv_t *f_recv, |
| 8866 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8867 | { |
| 8868 | ssl->p_bio = p_bio; |
| 8869 | ssl->f_send = f_send; |
| 8870 | ssl->f_recv = f_recv; |
| 8871 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 8872 | } |
| 8873 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 8874 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8875 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 8876 | { |
| 8877 | ssl->mtu = mtu; |
| 8878 | } |
| 8879 | #endif |
| 8880 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8881 | 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] | 8882 | { |
| 8883 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8884 | } |
| 8885 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8886 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 8887 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8888 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 8889 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8890 | { |
| 8891 | ssl->p_timer = p_timer; |
| 8892 | ssl->f_set_timer = f_set_timer; |
| 8893 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8894 | |
| 8895 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 8896 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8897 | } |
| 8898 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8899 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8900 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8901 | void *p_cache, |
| 8902 | int (*f_get_cache)(void *, mbedtls_ssl_session *), |
| 8903 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8904 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8905 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8906 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8907 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8908 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8909 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8910 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8911 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8912 | 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] | 8913 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 8914 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8915 | |
| 8916 | if( ssl == NULL || |
| 8917 | session == NULL || |
| 8918 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8919 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8920 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8921 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8922 | } |
| 8923 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 8924 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 8925 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8926 | return( ret ); |
| 8927 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8928 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8929 | |
| 8930 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8931 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8932 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8933 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8934 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8935 | const int *ciphersuites ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8936 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8937 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; |
| 8938 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; |
| 8939 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; |
| 8940 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8941 | } |
| 8942 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8943 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 8944 | const int *ciphersuites, |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8945 | int major, int minor ) |
| 8946 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8947 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8948 | return; |
| 8949 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8950 | 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] | 8951 | return; |
| 8952 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8953 | conf->ciphersuite_list[minor] = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8954 | } |
| 8955 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8956 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8957 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 8958 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8959 | { |
| 8960 | conf->cert_profile = profile; |
| 8961 | } |
| 8962 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8963 | /* Append a new keycert entry to a (possibly empty) list */ |
| 8964 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 8965 | mbedtls_x509_crt *cert, |
| 8966 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8967 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8968 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8969 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8970 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 8971 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8972 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8973 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8974 | new_cert->cert = cert; |
| 8975 | new_cert->key = key; |
| 8976 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8977 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8978 | /* Update head is the list was null, else add to the end */ |
| 8979 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8980 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8981 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8982 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8983 | else |
| 8984 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8985 | mbedtls_ssl_key_cert *cur = *head; |
| 8986 | while( cur->next != NULL ) |
| 8987 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8988 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8989 | } |
| 8990 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8991 | return( 0 ); |
| 8992 | } |
| 8993 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8994 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8995 | mbedtls_x509_crt *own_cert, |
| 8996 | mbedtls_pk_context *pk_key ) |
| 8997 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 8998 | 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] | 8999 | } |
| 9000 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9001 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9002 | mbedtls_x509_crt *ca_chain, |
| 9003 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9004 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9005 | conf->ca_chain = ca_chain; |
| 9006 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 9007 | |
| 9008 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 9009 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 9010 | * cannot be used together. */ |
| 9011 | conf->f_ca_cb = NULL; |
| 9012 | conf->p_ca_cb = NULL; |
| 9013 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9014 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 9015 | |
| 9016 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 9017 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 9018 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 9019 | void *p_ca_cb ) |
| 9020 | { |
| 9021 | conf->f_ca_cb = f_ca_cb; |
| 9022 | conf->p_ca_cb = p_ca_cb; |
| 9023 | |
| 9024 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 9025 | * cannot be used together. */ |
| 9026 | conf->ca_chain = NULL; |
| 9027 | conf->ca_crl = NULL; |
| 9028 | } |
| 9029 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9030 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 9031 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 9032 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 9033 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 9034 | mbedtls_x509_crt *own_cert, |
| 9035 | mbedtls_pk_context *pk_key ) |
| 9036 | { |
| 9037 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 9038 | own_cert, pk_key ) ); |
| 9039 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 9040 | |
| 9041 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 9042 | mbedtls_x509_crt *ca_chain, |
| 9043 | mbedtls_x509_crl *ca_crl ) |
| 9044 | { |
| 9045 | ssl->handshake->sni_ca_chain = ca_chain; |
| 9046 | ssl->handshake->sni_ca_crl = ca_crl; |
| 9047 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 9048 | |
| 9049 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 9050 | int authmode ) |
| 9051 | { |
| 9052 | ssl->handshake->sni_authmode = authmode; |
| 9053 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 9054 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 9055 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 9056 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9057 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 9058 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 9059 | void *p_vrfy ) |
| 9060 | { |
| 9061 | ssl->f_vrfy = f_vrfy; |
| 9062 | ssl->p_vrfy = p_vrfy; |
| 9063 | } |
| 9064 | #endif |
| 9065 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 9066 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 9067 | /* |
| 9068 | * Set EC J-PAKE password for current handshake |
| 9069 | */ |
| 9070 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 9071 | const unsigned char *pw, |
| 9072 | size_t pw_len ) |
| 9073 | { |
| 9074 | mbedtls_ecjpake_role role; |
| 9075 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 9076 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 9077 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9078 | |
| 9079 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 9080 | role = MBEDTLS_ECJPAKE_SERVER; |
| 9081 | else |
| 9082 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 9083 | |
| 9084 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 9085 | role, |
| 9086 | MBEDTLS_MD_SHA256, |
| 9087 | MBEDTLS_ECP_DP_SECP256R1, |
| 9088 | pw, pw_len ) ); |
| 9089 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 9090 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 9091 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9092 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9093 | |
| 9094 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 9095 | { |
| 9096 | /* Remove reference to existing PSK, if any. */ |
| 9097 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9098 | if( conf->psk_opaque != 0 ) |
| 9099 | { |
| 9100 | /* The maintenance of the PSK key slot is the |
| 9101 | * user's responsibility. */ |
| 9102 | conf->psk_opaque = 0; |
| 9103 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 9104 | /* This and the following branch should never |
| 9105 | * be taken simultaenously as we maintain the |
| 9106 | * invariant that raw and opaque PSKs are never |
| 9107 | * configured simultaneously. As a safeguard, |
| 9108 | * though, `else` is omitted here. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9109 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9110 | if( conf->psk != NULL ) |
| 9111 | { |
| 9112 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 9113 | |
| 9114 | mbedtls_free( conf->psk ); |
| 9115 | conf->psk = NULL; |
| 9116 | conf->psk_len = 0; |
| 9117 | } |
| 9118 | |
| 9119 | /* Remove reference to PSK identity, if any. */ |
| 9120 | if( conf->psk_identity != NULL ) |
| 9121 | { |
| 9122 | mbedtls_free( conf->psk_identity ); |
| 9123 | conf->psk_identity = NULL; |
| 9124 | conf->psk_identity_len = 0; |
| 9125 | } |
| 9126 | } |
| 9127 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9128 | /* This function assumes that PSK identity in the SSL config is unset. |
| 9129 | * It checks that the provided identity is well-formed and attempts |
| 9130 | * to make a copy of it in the SSL config. |
| 9131 | * On failure, the PSK identity in the config remains unset. */ |
| 9132 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 9133 | unsigned char const *psk_identity, |
| 9134 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 9135 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 9136 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9137 | if( psk_identity == NULL || |
| 9138 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9139 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 9140 | { |
| 9141 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9142 | } |
| 9143 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9144 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 9145 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 9146 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9147 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 9148 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 9149 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 9150 | |
| 9151 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9152 | } |
| 9153 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9154 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 9155 | const unsigned char *psk, size_t psk_len, |
| 9156 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 9157 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 9158 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9159 | /* Remove opaque/raw PSK + PSK Identity */ |
| 9160 | ssl_conf_remove_psk( conf ); |
| 9161 | |
| 9162 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 9163 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9164 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 9165 | if( psk_len == 0 ) |
| 9166 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9167 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 9168 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9169 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9170 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 9171 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9172 | conf->psk_len = psk_len; |
| 9173 | memcpy( conf->psk, psk, conf->psk_len ); |
| 9174 | |
| 9175 | /* Check and set PSK Identity */ |
| 9176 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 9177 | if( ret != 0 ) |
| 9178 | ssl_conf_remove_psk( conf ); |
| 9179 | |
| 9180 | return( ret ); |
| 9181 | } |
| 9182 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9183 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 9184 | { |
| 9185 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9186 | if( ssl->handshake->psk_opaque != 0 ) |
| 9187 | { |
| 9188 | ssl->handshake->psk_opaque = 0; |
| 9189 | } |
| 9190 | else |
| 9191 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9192 | if( ssl->handshake->psk != NULL ) |
| 9193 | { |
| 9194 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 9195 | ssl->handshake->psk_len ); |
| 9196 | mbedtls_free( ssl->handshake->psk ); |
| 9197 | ssl->handshake->psk_len = 0; |
| 9198 | } |
| 9199 | } |
| 9200 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9201 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 9202 | const unsigned char *psk, size_t psk_len ) |
| 9203 | { |
| 9204 | if( psk == NULL || ssl->handshake == NULL ) |
| 9205 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9206 | |
| 9207 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 9208 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9209 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9210 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9211 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 9212 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 9213 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9214 | |
| 9215 | ssl->handshake->psk_len = psk_len; |
| 9216 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 9217 | |
| 9218 | return( 0 ); |
| 9219 | } |
| 9220 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9221 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9222 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 9223 | psa_key_handle_t psk_slot, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9224 | const unsigned char *psk_identity, |
| 9225 | size_t psk_identity_len ) |
| 9226 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 9227 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9228 | /* Clear opaque/raw PSK + PSK Identity, if present. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9229 | ssl_conf_remove_psk( conf ); |
| 9230 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9231 | /* Check and set opaque PSK */ |
| 9232 | if( psk_slot == 0 ) |
| 9233 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9234 | conf->psk_opaque = psk_slot; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9235 | |
| 9236 | /* Check and set PSK Identity */ |
| 9237 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 9238 | psk_identity_len ); |
| 9239 | if( ret != 0 ) |
| 9240 | ssl_conf_remove_psk( conf ); |
| 9241 | |
| 9242 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9243 | } |
| 9244 | |
| 9245 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 9246 | psa_key_handle_t psk_slot ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9247 | { |
| 9248 | if( psk_slot == 0 || ssl->handshake == NULL ) |
| 9249 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9250 | |
| 9251 | ssl_remove_psk( ssl ); |
| 9252 | ssl->handshake->psk_opaque = psk_slot; |
| 9253 | return( 0 ); |
| 9254 | } |
| 9255 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9256 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9257 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9258 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9259 | size_t), |
| 9260 | void *p_psk ) |
| 9261 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9262 | conf->f_psk = f_psk; |
| 9263 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 9264 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9265 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 9266 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 9267 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 9268 | |
| 9269 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9270 | 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] | 9271 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 9272 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9273 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9274 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || |
| 9275 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) |
| 9276 | { |
| 9277 | mbedtls_mpi_free( &conf->dhm_P ); |
| 9278 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9279 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9280 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9281 | |
| 9282 | return( 0 ); |
| 9283 | } |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 9284 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9285 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 9286 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 9287 | const unsigned char *dhm_P, size_t P_len, |
| 9288 | const unsigned char *dhm_G, size_t G_len ) |
| 9289 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 9290 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 9291 | |
| 9292 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 9293 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 9294 | { |
| 9295 | mbedtls_mpi_free( &conf->dhm_P ); |
| 9296 | mbedtls_mpi_free( &conf->dhm_G ); |
| 9297 | return( ret ); |
| 9298 | } |
| 9299 | |
| 9300 | return( 0 ); |
| 9301 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9302 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9303 | 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] | 9304 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 9305 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9306 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9307 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || |
| 9308 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) |
| 9309 | { |
| 9310 | mbedtls_mpi_free( &conf->dhm_P ); |
| 9311 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9312 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9313 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9314 | |
| 9315 | return( 0 ); |
| 9316 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 9317 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9318 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 9319 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 9320 | /* |
| 9321 | * Set the minimum length for Diffie-Hellman parameters |
| 9322 | */ |
| 9323 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 9324 | unsigned int bitlen ) |
| 9325 | { |
| 9326 | conf->dhm_min_bitlen = bitlen; |
| 9327 | } |
| 9328 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 9329 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9330 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 9331 | /* |
| 9332 | * Set allowed/preferred hashes for handshake signatures |
| 9333 | */ |
| 9334 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 9335 | const int *hashes ) |
| 9336 | { |
| 9337 | conf->sig_hashes = hashes; |
| 9338 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9339 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 9340 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 9341 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9342 | /* |
| 9343 | * Set the allowed elliptic curves |
| 9344 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9345 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9346 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9347 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9348 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9349 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9350 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9351 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9352 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9353 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9354 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9355 | /* Initialize to suppress unnecessary compiler warning */ |
| 9356 | size_t hostname_len = 0; |
| 9357 | |
| 9358 | /* Check if new hostname is valid before |
| 9359 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9360 | if( hostname != NULL ) |
| 9361 | { |
| 9362 | hostname_len = strlen( hostname ); |
| 9363 | |
| 9364 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 9365 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9366 | } |
| 9367 | |
| 9368 | /* Now it's clear that we will overwrite the old hostname, |
| 9369 | * so we can free it safely */ |
| 9370 | |
| 9371 | if( ssl->hostname != NULL ) |
| 9372 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9373 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9374 | mbedtls_free( ssl->hostname ); |
| 9375 | } |
| 9376 | |
| 9377 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 9378 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9379 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9380 | { |
| 9381 | ssl->hostname = NULL; |
| 9382 | } |
| 9383 | else |
| 9384 | { |
| 9385 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9386 | if( ssl->hostname == NULL ) |
| 9387 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 9388 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9389 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 9390 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9391 | ssl->hostname[hostname_len] = '\0'; |
| 9392 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9393 | |
| 9394 | return( 0 ); |
| 9395 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 9396 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9397 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9398 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9399 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9400 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9401 | const unsigned char *, size_t), |
| 9402 | void *p_sni ) |
| 9403 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9404 | conf->f_sni = f_sni; |
| 9405 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9406 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9407 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9408 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9409 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9410 | 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] | 9411 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9412 | size_t cur_len, tot_len; |
| 9413 | const char **p; |
| 9414 | |
| 9415 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 9416 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 9417 | * MUST NOT be truncated." |
| 9418 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9419 | */ |
| 9420 | tot_len = 0; |
| 9421 | for( p = protos; *p != NULL; p++ ) |
| 9422 | { |
| 9423 | cur_len = strlen( *p ); |
| 9424 | tot_len += cur_len; |
| 9425 | |
| 9426 | if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9427 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9428 | } |
| 9429 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9430 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9431 | |
| 9432 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9433 | } |
| 9434 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9435 | 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] | 9436 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9437 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9438 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9439 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9440 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9441 | void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor ) |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9442 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9443 | conf->max_major_ver = major; |
| 9444 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9445 | } |
| 9446 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9447 | void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9448 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9449 | conf->min_major_ver = major; |
| 9450 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9451 | } |
| 9452 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9453 | #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] | 9454 | 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] | 9455 | { |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 9456 | conf->fallback = fallback; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 9457 | } |
| 9458 | #endif |
| 9459 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 9460 | #if defined(MBEDTLS_SSL_SRV_C) |
| 9461 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 9462 | char cert_req_ca_list ) |
| 9463 | { |
| 9464 | conf->cert_req_ca_list = cert_req_ca_list; |
| 9465 | } |
| 9466 | #endif |
| 9467 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9468 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9469 | 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] | 9470 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9471 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 9472 | } |
| 9473 | #endif |
| 9474 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9475 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9476 | 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] | 9477 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9478 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 9479 | } |
| 9480 | #endif |
| 9481 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9482 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9483 | 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] | 9484 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9485 | conf->arc4_disabled = arc4; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9486 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9487 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9488 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9489 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9490 | 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] | 9491 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9492 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9493 | 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] | 9494 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9495 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9496 | } |
| 9497 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 9498 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9499 | |
| 9500 | return( 0 ); |
| 9501 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9502 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9503 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9504 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9505 | 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] | 9506 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9507 | conf->trunc_hmac = truncate; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9508 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9509 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9511 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9512 | 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] | 9513 | { |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 9514 | conf->cbc_record_splitting = split; |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 9515 | } |
| 9516 | #endif |
| 9517 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9518 | 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] | 9519 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9520 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9521 | } |
| 9522 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9523 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9524 | 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] | 9525 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9526 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 9527 | } |
| 9528 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9529 | 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] | 9530 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9531 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9532 | } |
| 9533 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9534 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9535 | const unsigned char period[8] ) |
| 9536 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9537 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9538 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9539 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9540 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9541 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9542 | #if defined(MBEDTLS_SSL_CLI_C) |
| 9543 | 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] | 9544 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 9545 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9546 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9547 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9548 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9549 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9550 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 9551 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 9552 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 9553 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9554 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9555 | conf->f_ticket_write = f_ticket_write; |
| 9556 | conf->f_ticket_parse = f_ticket_parse; |
| 9557 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9558 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9559 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9560 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9561 | |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 9562 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 9563 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, |
| 9564 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 9565 | void *p_export_keys ) |
| 9566 | { |
| 9567 | conf->f_export_keys = f_export_keys; |
| 9568 | conf->p_export_keys = p_export_keys; |
| 9569 | } |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 9570 | |
| 9571 | void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf, |
| 9572 | mbedtls_ssl_export_keys_ext_t *f_export_keys_ext, |
| 9573 | void *p_export_keys ) |
| 9574 | { |
| 9575 | conf->f_export_keys_ext = f_export_keys_ext; |
| 9576 | conf->p_export_keys = p_export_keys; |
| 9577 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 9578 | #endif |
| 9579 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9580 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9581 | void mbedtls_ssl_conf_async_private_cb( |
| 9582 | mbedtls_ssl_config *conf, |
| 9583 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 9584 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 9585 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 9586 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9587 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9588 | { |
| 9589 | conf->f_async_sign_start = f_async_sign; |
| 9590 | conf->f_async_decrypt_start = f_async_decrypt; |
| 9591 | conf->f_async_resume = f_async_resume; |
| 9592 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9593 | conf->p_async_config_data = async_config_data; |
| 9594 | } |
| 9595 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 9596 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 9597 | { |
| 9598 | return( conf->p_async_config_data ); |
| 9599 | } |
| 9600 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9601 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9602 | { |
| 9603 | if( ssl->handshake == NULL ) |
| 9604 | return( NULL ); |
| 9605 | else |
| 9606 | return( ssl->handshake->user_async_ctx ); |
| 9607 | } |
| 9608 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9609 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9610 | void *ctx ) |
| 9611 | { |
| 9612 | if( ssl->handshake != NULL ) |
| 9613 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9614 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9615 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9616 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9617 | /* |
| 9618 | * SSL get accessors |
| 9619 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9620 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9621 | { |
| 9622 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); |
| 9623 | } |
| 9624 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9625 | int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl ) |
| 9626 | { |
| 9627 | /* |
| 9628 | * Case A: We're currently holding back |
| 9629 | * a message for further processing. |
| 9630 | */ |
| 9631 | |
| 9632 | if( ssl->keep_current_message == 1 ) |
| 9633 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9634 | 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] | 9635 | return( 1 ); |
| 9636 | } |
| 9637 | |
| 9638 | /* |
| 9639 | * Case B: Further records are pending in the current datagram. |
| 9640 | */ |
| 9641 | |
| 9642 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9643 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 9644 | ssl->in_left > ssl->next_record_offset ) |
| 9645 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9646 | 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] | 9647 | return( 1 ); |
| 9648 | } |
| 9649 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9650 | |
| 9651 | /* |
| 9652 | * Case C: A handshake message is being processed. |
| 9653 | */ |
| 9654 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9655 | if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen ) |
| 9656 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9657 | 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] | 9658 | return( 1 ); |
| 9659 | } |
| 9660 | |
| 9661 | /* |
| 9662 | * Case D: An application data message is being processed |
| 9663 | */ |
| 9664 | if( ssl->in_offt != NULL ) |
| 9665 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9666 | 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] | 9667 | return( 1 ); |
| 9668 | } |
| 9669 | |
| 9670 | /* |
| 9671 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 9672 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9673 | * we implement support for multiple alerts in single records. |
| 9674 | */ |
| 9675 | |
| 9676 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) ); |
| 9677 | return( 0 ); |
| 9678 | } |
| 9679 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 9680 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9681 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 9682 | if( ssl->session != NULL ) |
| 9683 | return( ssl->session->verify_result ); |
| 9684 | |
| 9685 | if( ssl->session_negotiate != NULL ) |
| 9686 | return( ssl->session_negotiate->verify_result ); |
| 9687 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 9688 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9689 | } |
| 9690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9691 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9692 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9693 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9694 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9696 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9697 | } |
| 9698 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9699 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9700 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9701 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9702 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9703 | { |
| 9704 | switch( ssl->minor_ver ) |
| 9705 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9706 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9707 | return( "DTLSv1.0" ); |
| 9708 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9709 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9710 | return( "DTLSv1.2" ); |
| 9711 | |
| 9712 | default: |
| 9713 | return( "unknown (DTLS)" ); |
| 9714 | } |
| 9715 | } |
| 9716 | #endif |
| 9717 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9718 | switch( ssl->minor_ver ) |
| 9719 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9720 | case MBEDTLS_SSL_MINOR_VERSION_0: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9721 | return( "SSLv3.0" ); |
| 9722 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9723 | case MBEDTLS_SSL_MINOR_VERSION_1: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9724 | return( "TLSv1.0" ); |
| 9725 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9726 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9727 | return( "TLSv1.1" ); |
| 9728 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9729 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 9730 | return( "TLSv1.2" ); |
| 9731 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9732 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9733 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9734 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9735 | } |
| 9736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9737 | 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] | 9738 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9739 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9740 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9741 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9742 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9743 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl ); |
| 9744 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9745 | if( transform == NULL ) |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9746 | return( (int) out_hdr_len ); |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9747 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9748 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 9749 | if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL ) |
| 9750 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9751 | #endif |
| 9752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9753 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9754 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9755 | case MBEDTLS_MODE_GCM: |
| 9756 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9757 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9758 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9759 | transform_expansion = transform->minlen; |
| 9760 | break; |
| 9761 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9762 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9763 | |
| 9764 | block_size = mbedtls_cipher_get_block_size( |
| 9765 | &transform->cipher_ctx_enc ); |
| 9766 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9767 | /* Expansion due to the addition of the MAC. */ |
| 9768 | transform_expansion += transform->maclen; |
| 9769 | |
| 9770 | /* Expansion due to the addition of CBC padding; |
| 9771 | * Theoretically up to 256 bytes, but we never use |
| 9772 | * more than the block size of the underlying cipher. */ |
| 9773 | transform_expansion += block_size; |
| 9774 | |
| 9775 | /* For TLS 1.1 or higher, an explicit IV is added |
| 9776 | * after the record header. */ |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9777 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9778 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9779 | transform_expansion += block_size; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9780 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9781 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9782 | break; |
| 9783 | |
| 9784 | default: |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 9785 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9786 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9787 | } |
| 9788 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9789 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9790 | if( transform->out_cid_len != 0 ) |
| 9791 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9792 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9793 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9794 | return( (int)( out_hdr_len + transform_expansion ) ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9795 | } |
| 9796 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9797 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9798 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 9799 | { |
| 9800 | size_t max_len; |
| 9801 | |
| 9802 | /* |
| 9803 | * Assume mfl_code is correct since it was checked when set |
| 9804 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9805 | 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] | 9806 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9807 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9808 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9809 | 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] | 9810 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9811 | 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] | 9812 | } |
| 9813 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9814 | /* During a handshake, use the value being negotiated */ |
| 9815 | if( ssl->session_negotiate != NULL && |
| 9816 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 9817 | { |
| 9818 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 9819 | } |
| 9820 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9821 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9822 | } |
| 9823 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 9824 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9825 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9826 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) |
| 9827 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9828 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 9829 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 9830 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 9831 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 9832 | return ( 0 ); |
| 9833 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9834 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 9835 | return( ssl->mtu ); |
| 9836 | |
| 9837 | if( ssl->mtu == 0 ) |
| 9838 | return( ssl->handshake->mtu ); |
| 9839 | |
| 9840 | return( ssl->mtu < ssl->handshake->mtu ? |
| 9841 | ssl->mtu : ssl->handshake->mtu ); |
| 9842 | } |
| 9843 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9844 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9845 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 9846 | { |
| 9847 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 9848 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 9849 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9850 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9851 | (void) ssl; |
| 9852 | #endif |
| 9853 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9854 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9855 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 9856 | |
| 9857 | if( max_len > mfl ) |
| 9858 | max_len = mfl; |
| 9859 | #endif |
| 9860 | |
| 9861 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9862 | if( ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9863 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9864 | const size_t mtu = ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9865 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 9866 | const size_t overhead = (size_t) ret; |
| 9867 | |
| 9868 | if( ret < 0 ) |
| 9869 | return( ret ); |
| 9870 | |
| 9871 | if( mtu <= overhead ) |
| 9872 | { |
| 9873 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 9874 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 9875 | } |
| 9876 | |
| 9877 | if( max_len > mtu - overhead ) |
| 9878 | max_len = mtu - overhead; |
| 9879 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9880 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9881 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 9882 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9883 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9884 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9885 | #endif |
| 9886 | |
| 9887 | return( (int) max_len ); |
| 9888 | } |
| 9889 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9890 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9891 | 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] | 9892 | { |
| 9893 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9894 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9895 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9896 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9897 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9898 | #else |
| 9899 | return( NULL ); |
| 9900 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9901 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9902 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9903 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9904 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 9905 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 9906 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9907 | { |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9908 | if( ssl == NULL || |
| 9909 | dst == NULL || |
| 9910 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9911 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9912 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9913 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9914 | } |
| 9915 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 9916 | return( mbedtls_ssl_session_copy( dst, ssl->session ) ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9917 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9918 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9919 | |
Manuel Pégourié-Gonnard | b5e4e0a | 2019-05-20 11:12:28 +0200 | [diff] [blame] | 9920 | const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl ) |
| 9921 | { |
| 9922 | if( ssl == NULL ) |
| 9923 | return( NULL ); |
| 9924 | |
| 9925 | return( ssl->session ); |
| 9926 | } |
| 9927 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9928 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9929 | * Define ticket header determining Mbed TLS version |
| 9930 | * and structure of the ticket. |
| 9931 | */ |
| 9932 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9933 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9934 | * Define bitflag determining compile-time settings influencing |
| 9935 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9936 | */ |
| 9937 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9938 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9939 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9940 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9941 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9942 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9943 | |
| 9944 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9945 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9946 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9947 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9948 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9949 | |
| 9950 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9951 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9952 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9953 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9954 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 9955 | |
| 9956 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9957 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9958 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9959 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9960 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 9961 | |
| 9962 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9963 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9964 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9965 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9966 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 9967 | |
| 9968 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9969 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9970 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9971 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9972 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 9973 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9974 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 9975 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 9976 | #else |
| 9977 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 9978 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 9979 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9980 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 9981 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 9982 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 9983 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
| 9984 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4 |
| 9985 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5 |
| 9986 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9987 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9988 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9989 | ( (uint16_t) ( \ |
| 9990 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 9991 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 9992 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 9993 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
| 9994 | ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \ |
| 9995 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 9996 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9997 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 9998 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9999 | MBEDTLS_VERSION_MAJOR, |
| 10000 | MBEDTLS_VERSION_MINOR, |
| 10001 | MBEDTLS_VERSION_PATCH, |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10002 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 10003 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 10004 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10005 | |
| 10006 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10007 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10008 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10009 | * |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10010 | * opaque mbedtls_version[3]; // major, minor, patch |
| 10011 | * opaque session_format[2]; // version-specific 16-bit field determining |
| 10012 | * // the format of the remaining |
| 10013 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 10014 | * |
| 10015 | * Note: When updating the format, remember to keep |
| 10016 | * these version+format bytes. |
| 10017 | * |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 10018 | * // In this version, `session_format` determines |
| 10019 | * // the setting of those compile-time |
| 10020 | * // configuration options which influence |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10021 | * // the structure of mbedtls_ssl_session. |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10022 | * uint64 start_time; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10023 | * uint8 ciphersuite[2]; // defined by the standard |
| 10024 | * uint8 compression; // 0 or 1 |
| 10025 | * uint8 session_id_len; // at most 32 |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10026 | * opaque session_id[32]; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10027 | * opaque master[48]; // fixed length in the standard |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10028 | * uint32 verify_result; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10029 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 10030 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10031 | * uint32 ticket_lifetime; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10032 | * uint8 mfl_code; // up to 255 according to standard |
| 10033 | * uint8 trunc_hmac; // 0 or 1 |
| 10034 | * uint8 encrypt_then_mac; // 0 or 1 |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10035 | * |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10036 | * The order is the same as in the definition of the structure, except |
| 10037 | * verify_result is put before peer_cert so that all mandatory fields come |
| 10038 | * together in one block. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10039 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10040 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 10041 | unsigned char omit_header, |
| 10042 | unsigned char *buf, |
| 10043 | size_t buf_len, |
| 10044 | size_t *olen ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10045 | { |
| 10046 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10047 | size_t used = 0; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10048 | #if defined(MBEDTLS_HAVE_TIME) |
| 10049 | uint64_t start; |
| 10050 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10051 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10052 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10053 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10054 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10055 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 10056 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10057 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10058 | if( !omit_header ) |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10059 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10060 | /* |
| 10061 | * Add version identifier |
| 10062 | */ |
| 10063 | |
| 10064 | used += sizeof( ssl_serialized_session_header ); |
| 10065 | |
| 10066 | if( used <= buf_len ) |
| 10067 | { |
| 10068 | memcpy( p, ssl_serialized_session_header, |
| 10069 | sizeof( ssl_serialized_session_header ) ); |
| 10070 | p += sizeof( ssl_serialized_session_header ); |
| 10071 | } |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10072 | } |
| 10073 | |
| 10074 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10075 | * Time |
| 10076 | */ |
| 10077 | #if defined(MBEDTLS_HAVE_TIME) |
| 10078 | used += 8; |
| 10079 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10080 | if( used <= buf_len ) |
| 10081 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10082 | start = (uint64_t) session->start; |
| 10083 | |
| 10084 | *p++ = (unsigned char)( ( start >> 56 ) & 0xFF ); |
| 10085 | *p++ = (unsigned char)( ( start >> 48 ) & 0xFF ); |
| 10086 | *p++ = (unsigned char)( ( start >> 40 ) & 0xFF ); |
| 10087 | *p++ = (unsigned char)( ( start >> 32 ) & 0xFF ); |
| 10088 | *p++ = (unsigned char)( ( start >> 24 ) & 0xFF ); |
| 10089 | *p++ = (unsigned char)( ( start >> 16 ) & 0xFF ); |
| 10090 | *p++ = (unsigned char)( ( start >> 8 ) & 0xFF ); |
| 10091 | *p++ = (unsigned char)( ( start ) & 0xFF ); |
| 10092 | } |
| 10093 | #endif /* MBEDTLS_HAVE_TIME */ |
| 10094 | |
| 10095 | /* |
| 10096 | * Basic mandatory fields |
| 10097 | */ |
| 10098 | used += 2 /* ciphersuite */ |
| 10099 | + 1 /* compression */ |
| 10100 | + 1 /* id_len */ |
| 10101 | + sizeof( session->id ) |
| 10102 | + sizeof( session->master ) |
| 10103 | + 4; /* verify_result */ |
| 10104 | |
| 10105 | if( used <= buf_len ) |
| 10106 | { |
| 10107 | *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF ); |
| 10108 | *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF ); |
| 10109 | |
| 10110 | *p++ = (unsigned char)( session->compression & 0xFF ); |
| 10111 | |
| 10112 | *p++ = (unsigned char)( session->id_len & 0xFF ); |
| 10113 | memcpy( p, session->id, 32 ); |
| 10114 | p += 32; |
| 10115 | |
| 10116 | memcpy( p, session->master, 48 ); |
| 10117 | p += 48; |
| 10118 | |
| 10119 | *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF ); |
| 10120 | *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF ); |
| 10121 | *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF ); |
| 10122 | *p++ = (unsigned char)( ( session->verify_result ) & 0xFF ); |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10123 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10124 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10125 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10126 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10127 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10128 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10129 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10130 | if( session->peer_cert == NULL ) |
| 10131 | cert_len = 0; |
| 10132 | else |
| 10133 | cert_len = session->peer_cert->raw.len; |
| 10134 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10135 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10136 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10137 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10138 | { |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10139 | *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF ); |
| 10140 | *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF ); |
| 10141 | *p++ = (unsigned char)( ( cert_len ) & 0xFF ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10142 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10143 | if( session->peer_cert != NULL ) |
| 10144 | { |
| 10145 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 10146 | p += cert_len; |
| 10147 | } |
| 10148 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10149 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10150 | if( session->peer_cert_digest != NULL ) |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10151 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10152 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 10153 | if( used <= buf_len ) |
| 10154 | { |
| 10155 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 10156 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 10157 | memcpy( p, session->peer_cert_digest, |
| 10158 | session->peer_cert_digest_len ); |
| 10159 | p += session->peer_cert_digest_len; |
| 10160 | } |
| 10161 | } |
| 10162 | else |
| 10163 | { |
| 10164 | used += 2; |
| 10165 | if( used <= buf_len ) |
| 10166 | { |
| 10167 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 10168 | *p++ = 0; |
| 10169 | } |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10170 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10171 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10172 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 10173 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10174 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10175 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10176 | */ |
| 10177 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10178 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10179 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10180 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10181 | { |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10182 | *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF ); |
| 10183 | *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF ); |
| 10184 | *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF ); |
| 10185 | |
| 10186 | if( session->ticket != NULL ) |
| 10187 | { |
| 10188 | memcpy( p, session->ticket, session->ticket_len ); |
| 10189 | p += session->ticket_len; |
| 10190 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10191 | |
| 10192 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF ); |
| 10193 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF ); |
| 10194 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF ); |
| 10195 | *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF ); |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10196 | } |
| 10197 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 10198 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10199 | /* |
| 10200 | * Misc extension-related info |
| 10201 | */ |
| 10202 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 10203 | used += 1; |
| 10204 | |
| 10205 | if( used <= buf_len ) |
| 10206 | *p++ = session->mfl_code; |
| 10207 | #endif |
| 10208 | |
| 10209 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 10210 | used += 1; |
| 10211 | |
| 10212 | if( used <= buf_len ) |
| 10213 | *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF ); |
| 10214 | #endif |
| 10215 | |
| 10216 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 10217 | used += 1; |
| 10218 | |
| 10219 | if( used <= buf_len ) |
| 10220 | *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF ); |
| 10221 | #endif |
| 10222 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10223 | /* Done */ |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10224 | *olen = used; |
| 10225 | |
| 10226 | if( used > buf_len ) |
| 10227 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10228 | |
| 10229 | return( 0 ); |
| 10230 | } |
| 10231 | |
| 10232 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10233 | * Public wrapper for ssl_session_save() |
| 10234 | */ |
| 10235 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 10236 | unsigned char *buf, |
| 10237 | size_t buf_len, |
| 10238 | size_t *olen ) |
| 10239 | { |
| 10240 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 10241 | } |
| 10242 | |
| 10243 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 10244 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10245 | * |
| 10246 | * This internal version is wrapped by a public function that cleans up in |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10247 | * case of error, and has an extra option omit_header. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10248 | */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10249 | static int ssl_session_load( mbedtls_ssl_session *session, |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10250 | unsigned char omit_header, |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10251 | const unsigned char *buf, |
| 10252 | size_t len ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10253 | { |
| 10254 | const unsigned char *p = buf; |
| 10255 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10256 | #if defined(MBEDTLS_HAVE_TIME) |
| 10257 | uint64_t start; |
| 10258 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10259 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10260 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10261 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10262 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10263 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 10264 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10265 | if( !omit_header ) |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10266 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10267 | /* |
| 10268 | * Check version identifier |
| 10269 | */ |
| 10270 | |
| 10271 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 10272 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10273 | |
| 10274 | if( memcmp( p, ssl_serialized_session_header, |
| 10275 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 10276 | { |
| 10277 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 10278 | } |
| 10279 | p += sizeof( ssl_serialized_session_header ); |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10280 | } |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10281 | |
| 10282 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10283 | * Time |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10284 | */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10285 | #if defined(MBEDTLS_HAVE_TIME) |
| 10286 | if( 8 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10287 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10288 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10289 | start = ( (uint64_t) p[0] << 56 ) | |
| 10290 | ( (uint64_t) p[1] << 48 ) | |
| 10291 | ( (uint64_t) p[2] << 40 ) | |
| 10292 | ( (uint64_t) p[3] << 32 ) | |
| 10293 | ( (uint64_t) p[4] << 24 ) | |
| 10294 | ( (uint64_t) p[5] << 16 ) | |
| 10295 | ( (uint64_t) p[6] << 8 ) | |
| 10296 | ( (uint64_t) p[7] ); |
| 10297 | p += 8; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10298 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10299 | session->start = (time_t) start; |
| 10300 | #endif /* MBEDTLS_HAVE_TIME */ |
| 10301 | |
| 10302 | /* |
| 10303 | * Basic mandatory fields |
| 10304 | */ |
| 10305 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 10306 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10307 | |
| 10308 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 10309 | p += 2; |
| 10310 | |
| 10311 | session->compression = *p++; |
| 10312 | |
| 10313 | session->id_len = *p++; |
| 10314 | memcpy( session->id, p, 32 ); |
| 10315 | p += 32; |
| 10316 | |
| 10317 | memcpy( session->master, p, 48 ); |
| 10318 | p += 48; |
| 10319 | |
| 10320 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 10321 | ( (uint32_t) p[1] << 16 ) | |
| 10322 | ( (uint32_t) p[2] << 8 ) | |
| 10323 | ( (uint32_t) p[3] ); |
| 10324 | p += 4; |
| 10325 | |
| 10326 | /* Immediately clear invalid pointer values that have been read, in case |
| 10327 | * we exit early before we replaced them with valid ones. */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10328 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10329 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10330 | session->peer_cert = NULL; |
| 10331 | #else |
| 10332 | session->peer_cert_digest = NULL; |
| 10333 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10334 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 10335 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 10336 | session->ticket = NULL; |
| 10337 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 10338 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10339 | /* |
| 10340 | * Peer certificate |
| 10341 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10342 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10343 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10344 | /* Deserialize CRT from the end of the ticket. */ |
| 10345 | if( 3 > (size_t)( end - p ) ) |
| 10346 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10347 | |
| 10348 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 10349 | p += 3; |
| 10350 | |
| 10351 | if( cert_len != 0 ) |
| 10352 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 10353 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10354 | |
| 10355 | if( cert_len > (size_t)( end - p ) ) |
| 10356 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10357 | |
| 10358 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 10359 | |
| 10360 | if( session->peer_cert == NULL ) |
| 10361 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 10362 | |
| 10363 | mbedtls_x509_crt_init( session->peer_cert ); |
| 10364 | |
| 10365 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 10366 | p, cert_len ) ) != 0 ) |
| 10367 | { |
| 10368 | mbedtls_x509_crt_free( session->peer_cert ); |
| 10369 | mbedtls_free( session->peer_cert ); |
| 10370 | session->peer_cert = NULL; |
| 10371 | return( ret ); |
| 10372 | } |
| 10373 | |
| 10374 | p += cert_len; |
| 10375 | } |
| 10376 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10377 | /* Deserialize CRT digest from the end of the ticket. */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10378 | if( 2 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10379 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10380 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10381 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 10382 | session->peer_cert_digest_len = (size_t) *p++; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10383 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10384 | if( session->peer_cert_digest_len != 0 ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10385 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10386 | const mbedtls_md_info_t *md_info = |
| 10387 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 10388 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10389 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10390 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 10391 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10392 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10393 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 10394 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10395 | |
| 10396 | session->peer_cert_digest = |
| 10397 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10398 | if( session->peer_cert_digest == NULL ) |
| 10399 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 10400 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10401 | memcpy( session->peer_cert_digest, p, |
| 10402 | session->peer_cert_digest_len ); |
| 10403 | p += session->peer_cert_digest_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10404 | } |
| 10405 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10406 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 10407 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10408 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10409 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10410 | */ |
| 10411 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 10412 | if( 3 > (size_t)( end - p ) ) |
| 10413 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10414 | |
| 10415 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 10416 | p += 3; |
| 10417 | |
| 10418 | if( session->ticket_len != 0 ) |
| 10419 | { |
| 10420 | if( session->ticket_len > (size_t)( end - p ) ) |
| 10421 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10422 | |
| 10423 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 10424 | if( session->ticket == NULL ) |
| 10425 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 10426 | |
| 10427 | memcpy( session->ticket, p, session->ticket_len ); |
| 10428 | p += session->ticket_len; |
| 10429 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10430 | |
| 10431 | if( 4 > (size_t)( end - p ) ) |
| 10432 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10433 | |
| 10434 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 10435 | ( (uint32_t) p[1] << 16 ) | |
| 10436 | ( (uint32_t) p[2] << 8 ) | |
| 10437 | ( (uint32_t) p[3] ); |
| 10438 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10439 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 10440 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10441 | /* |
| 10442 | * Misc extension-related info |
| 10443 | */ |
| 10444 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 10445 | if( 1 > (size_t)( end - p ) ) |
| 10446 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10447 | |
| 10448 | session->mfl_code = *p++; |
| 10449 | #endif |
| 10450 | |
| 10451 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 10452 | if( 1 > (size_t)( end - p ) ) |
| 10453 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10454 | |
| 10455 | session->trunc_hmac = *p++; |
| 10456 | #endif |
| 10457 | |
| 10458 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 10459 | if( 1 > (size_t)( end - p ) ) |
| 10460 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10461 | |
| 10462 | session->encrypt_then_mac = *p++; |
| 10463 | #endif |
| 10464 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10465 | /* Done, should have consumed entire buffer */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10466 | if( p != end ) |
| 10467 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10468 | |
| 10469 | return( 0 ); |
| 10470 | } |
| 10471 | |
| 10472 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 10473 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10474 | */ |
| 10475 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 10476 | const unsigned char *buf, |
| 10477 | size_t len ) |
| 10478 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10479 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10480 | |
| 10481 | if( ret != 0 ) |
| 10482 | mbedtls_ssl_session_free( session ); |
| 10483 | |
| 10484 | return( ret ); |
| 10485 | } |
| 10486 | |
| 10487 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10488 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10489 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10490 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10491 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10492 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10493 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10494 | if( ssl == NULL || ssl->conf == NULL ) |
| 10495 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10497 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10498 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10499 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10500 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10501 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10502 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10503 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10504 | #endif |
| 10505 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10506 | return( ret ); |
| 10507 | } |
| 10508 | |
| 10509 | /* |
| 10510 | * Perform the SSL handshake |
| 10511 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10512 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10513 | { |
| 10514 | int ret = 0; |
| 10515 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10516 | if( ssl == NULL || ssl->conf == NULL ) |
| 10517 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10519 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10520 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10521 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10522 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10523 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10524 | |
| 10525 | if( ret != 0 ) |
| 10526 | break; |
| 10527 | } |
| 10528 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10529 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10530 | |
| 10531 | return( ret ); |
| 10532 | } |
| 10533 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10534 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 10535 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10536 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10537 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10538 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10539 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10540 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 10541 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10543 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10544 | |
| 10545 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10546 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 10547 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10548 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 10549 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10550 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 10551 | 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] | 10552 | return( ret ); |
| 10553 | } |
| 10554 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10555 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10556 | |
| 10557 | return( 0 ); |
| 10558 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10559 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10560 | |
| 10561 | /* |
| 10562 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10563 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 10564 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 10565 | * - 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] | 10566 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10567 | * 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] | 10568 | * 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] | 10569 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10570 | static int ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10571 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 10572 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10573 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10574 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10575 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10576 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 10577 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10578 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10579 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 10580 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10581 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10582 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10583 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10584 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10585 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 10586 | ssl->handshake->out_msg_seq = 1; |
| 10587 | else |
| 10588 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10589 | } |
| 10590 | #endif |
| 10591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10592 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 10593 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10594 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10595 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10596 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10597 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10598 | return( ret ); |
| 10599 | } |
| 10600 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10601 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10602 | |
| 10603 | return( 0 ); |
| 10604 | } |
| 10605 | |
| 10606 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10607 | * Renegotiate current connection on client, |
| 10608 | * or request renegotiation on server |
| 10609 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10610 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10611 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10612 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10613 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10614 | if( ssl == NULL || ssl->conf == NULL ) |
| 10615 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10616 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10617 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10618 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10619 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10620 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10621 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 10622 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10623 | |
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 | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 10625 | |
| 10626 | /* Did we already try/start sending HelloRequest? */ |
| 10627 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10628 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 10629 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10630 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10631 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10632 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10633 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10634 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10635 | /* |
| 10636 | * On client, either start the renegotiation process or, |
| 10637 | * if already in progress, continue the handshake |
| 10638 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10639 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10640 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10641 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 10642 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10643 | |
| 10644 | if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 ) |
| 10645 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10646 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10647 | return( ret ); |
| 10648 | } |
| 10649 | } |
| 10650 | else |
| 10651 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10652 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10653 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10654 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10655 | return( ret ); |
| 10656 | } |
| 10657 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10658 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10659 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 10660 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10661 | } |
| 10662 | |
| 10663 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10664 | * Check record counters and renegotiate if they're above the limit. |
| 10665 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10666 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10667 | { |
Hanno Becker | dd77229 | 2020-02-05 10:38:31 +0000 | [diff] [blame] | 10668 | size_t ep_len = mbedtls_ssl_ep_len( ssl ); |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10669 | int in_ctr_cmp; |
| 10670 | int out_ctr_cmp; |
| 10671 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10672 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
| 10673 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10674 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10675 | { |
| 10676 | return( 0 ); |
| 10677 | } |
| 10678 | |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10679 | in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, |
| 10680 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 10681 | out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len, |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10682 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
| 10683 | |
| 10684 | if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10685 | { |
| 10686 | return( 0 ); |
| 10687 | } |
| 10688 | |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 10689 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10690 | return( mbedtls_ssl_renegotiate( ssl ) ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10691 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10692 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10693 | |
| 10694 | /* |
| 10695 | * Receive application data decrypted from the SSL layer |
| 10696 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10697 | 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] | 10698 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 10699 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 10700 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10701 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10702 | if( ssl == NULL || ssl->conf == NULL ) |
| 10703 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10704 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10705 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10706 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10707 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10708 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10709 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10710 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10711 | return( ret ); |
| 10712 | |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10713 | if( ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10714 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10715 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 10716 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10717 | return( ret ); |
| 10718 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10719 | } |
| 10720 | #endif |
| 10721 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10722 | /* |
| 10723 | * Check if renegotiation is necessary and/or handshake is |
| 10724 | * in process. If yes, perform/continue, and fall through |
| 10725 | * if an unexpected packet is received while the client |
| 10726 | * is waiting for the ServerHello. |
| 10727 | * |
| 10728 | * (There is no equivalent to the last condition on |
| 10729 | * the server-side as it is not treated as within |
| 10730 | * a handshake while waiting for the ClientHello |
| 10731 | * after a renegotiation request.) |
| 10732 | */ |
| 10733 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10734 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10735 | ret = ssl_check_ctr_renegotiate( ssl ); |
| 10736 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10737 | ret != 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10738 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10739 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10740 | return( ret ); |
| 10741 | } |
| 10742 | #endif |
| 10743 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10744 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10745 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10746 | ret = mbedtls_ssl_handshake( ssl ); |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10747 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10748 | ret != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10749 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10750 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10751 | return( ret ); |
| 10752 | } |
| 10753 | } |
| 10754 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 10755 | /* Loop as long as no application data record is available */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10756 | while( ssl->in_offt == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10757 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10758 | /* Start timer if not already running */ |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10759 | if( ssl->f_get_timer != NULL && |
| 10760 | ssl->f_get_timer( ssl->p_timer ) == -1 ) |
| 10761 | { |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 10762 | mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout ); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10763 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10764 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10765 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10766 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10767 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
| 10768 | return( 0 ); |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10769 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10770 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 10771 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10772 | } |
| 10773 | |
| 10774 | if( ssl->in_msglen == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10775 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10776 | { |
| 10777 | /* |
| 10778 | * OpenSSL sends empty messages to randomize the IV |
| 10779 | */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10780 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10781 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10782 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10783 | return( 0 ); |
| 10784 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10785 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10786 | return( ret ); |
| 10787 | } |
| 10788 | } |
| 10789 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10790 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10791 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10792 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10793 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10794 | /* |
| 10795 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 10796 | * - For server-side, expect CLIENT_HELLO. |
| 10797 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 10798 | */ |
| 10799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10800 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10801 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10802 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10803 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10804 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10805 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10806 | |
| 10807 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10808 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10809 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10810 | { |
| 10811 | continue; |
| 10812 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10813 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10814 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10815 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10816 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10817 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10818 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10819 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10820 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10821 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10822 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10823 | |
| 10824 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10825 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10826 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10827 | { |
| 10828 | continue; |
| 10829 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10830 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10831 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10832 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10833 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 10834 | |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10835 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10836 | /* Determine whether renegotiation attempt should be accepted */ |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10837 | if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 10838 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 10839 | ssl->conf->allow_legacy_renegotiation == |
| 10840 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) ) |
| 10841 | { |
| 10842 | /* |
| 10843 | * Accept renegotiation request |
| 10844 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10845 | |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10846 | /* DTLS clients need to know renego is server-initiated */ |
| 10847 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10848 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 10849 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 10850 | { |
| 10851 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 10852 | } |
| 10853 | #endif |
| 10854 | ret = ssl_start_renegotiation( ssl ); |
| 10855 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10856 | ret != 0 ) |
| 10857 | { |
| 10858 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
| 10859 | return( ret ); |
| 10860 | } |
| 10861 | } |
| 10862 | else |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10863 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10864 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10865 | /* |
| 10866 | * Refuse renegotiation |
| 10867 | */ |
| 10868 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10869 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10870 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10871 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 10872 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10873 | { |
Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 10874 | /* SSLv3 does not have a "no_renegotiation" warning, so |
| 10875 | we send a fatal alert and abort the connection. */ |
| 10876 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 10877 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 10878 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10879 | } |
| 10880 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10881 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 10882 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 10883 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10884 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10885 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10886 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 10887 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 10888 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10889 | { |
| 10890 | return( ret ); |
| 10891 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10892 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 10893 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10894 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || |
| 10895 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 10896 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10897 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 10898 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 10899 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10900 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10901 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10902 | /* At this point, we don't know whether the renegotiation has been |
| 10903 | * completed or not. The cases to consider are the following: |
| 10904 | * 1) The renegotiation is complete. In this case, no new record |
| 10905 | * has been read yet. |
| 10906 | * 2) The renegotiation is incomplete because the client received |
| 10907 | * an application data record while awaiting the ServerHello. |
| 10908 | * 3) The renegotiation is incomplete because the client received |
| 10909 | * a non-handshake, non-application data message while awaiting |
| 10910 | * the ServerHello. |
| 10911 | * In each of these case, looping will be the proper action: |
| 10912 | * - For 1), the next iteration will read a new record and check |
| 10913 | * if it's application data. |
| 10914 | * - For 2), the loop condition isn't satisfied as application data |
| 10915 | * is present, hence continue is the same as break |
| 10916 | * - For 3), the loop condition is satisfied and read_record |
| 10917 | * will re-deliver the message that was held back by the client |
| 10918 | * when expecting the ServerHello. |
| 10919 | */ |
| 10920 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10921 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10922 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10923 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 10924 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10925 | if( ssl->conf->renego_max_records >= 0 ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 10926 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10927 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10928 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10929 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10930 | "but not honored by client" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10931 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10932 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 10933 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 10934 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10935 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10936 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10937 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
| 10938 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10939 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10940 | 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] | 10941 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10942 | } |
| 10943 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10944 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10945 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10946 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); |
| 10947 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10948 | } |
| 10949 | |
| 10950 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10951 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 10952 | /* We're going to return something now, cancel timer, |
| 10953 | * except if handshake (renegotiation) is in progress */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10954 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 10955 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10956 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 10957 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10958 | /* If we requested renego but received AppData, resend HelloRequest. |
| 10959 | * Do it now, after setting in_offt, to avoid taking this branch |
| 10960 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10961 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10962 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10963 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10964 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10965 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10966 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10967 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10968 | return( ret ); |
| 10969 | } |
| 10970 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10971 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10972 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10973 | } |
| 10974 | |
| 10975 | n = ( len < ssl->in_msglen ) |
| 10976 | ? len : ssl->in_msglen; |
| 10977 | |
| 10978 | memcpy( buf, ssl->in_offt, n ); |
| 10979 | ssl->in_msglen -= n; |
| 10980 | |
| 10981 | if( ssl->in_msglen == 0 ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10982 | { |
| 10983 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10984 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 10985 | ssl->keep_current_message = 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10986 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10987 | else |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10988 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10989 | /* more data available */ |
| 10990 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10991 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10992 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10993 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10994 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 10995 | return( (int) n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10996 | } |
| 10997 | |
| 10998 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10999 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 11000 | * fragment length and buffer size. |
| 11001 | * |
| 11002 | * According to RFC 5246 Section 6.2.1: |
| 11003 | * |
| 11004 | * Zero-length fragments of Application data MAY be sent as they are |
| 11005 | * potentially useful as a traffic analysis countermeasure. |
| 11006 | * |
| 11007 | * Therefore, it is possible that the input message length is 0 and the |
| 11008 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11009 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11010 | static int ssl_write_real( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11011 | const unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11012 | { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 11013 | int ret = mbedtls_ssl_get_max_out_record_payload( ssl ); |
| 11014 | const size_t max_len = (size_t) ret; |
| 11015 | |
| 11016 | if( ret < 0 ) |
| 11017 | { |
| 11018 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret ); |
| 11019 | return( ret ); |
| 11020 | } |
| 11021 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11022 | if( len > max_len ) |
| 11023 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11024 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 11025 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11026 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11027 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11028 | "maximum fragment length: %d > %d", |
| 11029 | len, max_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11030 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11031 | } |
| 11032 | else |
| 11033 | #endif |
| 11034 | len = max_len; |
| 11035 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 11036 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11037 | if( ssl->out_left != 0 ) |
| 11038 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 11039 | /* |
| 11040 | * The user has previously tried to send the data and |
| 11041 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 11042 | * written. In this case, we expect the high-level write function |
| 11043 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 11044 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11045 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11046 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11047 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11048 | return( ret ); |
| 11049 | } |
| 11050 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 11051 | else |
Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 11052 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 11053 | /* |
| 11054 | * The user is trying to send a message the first time, so we need to |
| 11055 | * copy the data into the internal buffers and setup the data structure |
| 11056 | * to keep track of partial writes |
| 11057 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11058 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11059 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11060 | memcpy( ssl->out_msg, buf, len ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 11061 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 11062 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 11063 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11064 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 11065 | return( ret ); |
| 11066 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11067 | } |
| 11068 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11069 | return( (int) len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11070 | } |
| 11071 | |
| 11072 | /* |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11073 | * Write application data, doing 1/n-1 splitting if necessary. |
| 11074 | * |
| 11075 | * 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] | 11076 | * then the caller will call us again with the same arguments, so |
Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 11077 | * remember whether we already did the split or not. |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11078 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11079 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11080 | static int ssl_write_split( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11081 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11082 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 11083 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11084 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 11085 | if( ssl->conf->cbc_record_splitting == |
| 11086 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 11087 | len <= 1 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11088 | ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 || |
| 11089 | mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) |
| 11090 | != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11091 | { |
| 11092 | return( ssl_write_real( ssl, buf, len ) ); |
| 11093 | } |
| 11094 | |
| 11095 | if( ssl->split_done == 0 ) |
| 11096 | { |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 11097 | if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11098 | return( ret ); |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 11099 | ssl->split_done = 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11100 | } |
| 11101 | |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 11102 | if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) |
| 11103 | return( ret ); |
| 11104 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11105 | |
| 11106 | return( ret + 1 ); |
| 11107 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11108 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11109 | |
| 11110 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11111 | * Write application data (public-facing wrapper) |
| 11112 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11113 | 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] | 11114 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 11115 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11116 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11117 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11118 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 11119 | if( ssl == NULL || ssl->conf == NULL ) |
| 11120 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11121 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11122 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11123 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) |
| 11124 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11125 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11126 | return( ret ); |
| 11127 | } |
| 11128 | #endif |
| 11129 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11130 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11131 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11132 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11133 | { |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 11134 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11135 | return( ret ); |
| 11136 | } |
| 11137 | } |
| 11138 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11139 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11140 | ret = ssl_write_split( ssl, buf, len ); |
| 11141 | #else |
| 11142 | ret = ssl_write_real( ssl, buf, len ); |
| 11143 | #endif |
| 11144 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11145 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11146 | |
| 11147 | return( ret ); |
| 11148 | } |
| 11149 | |
| 11150 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11151 | * Notify the peer that the connection is being closed |
| 11152 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11153 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11154 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 11155 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11156 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 11157 | if( ssl == NULL || ssl->conf == NULL ) |
| 11158 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11159 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11160 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11161 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 11162 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11163 | return( mbedtls_ssl_flush_output( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11164 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11165 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11166 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11167 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 11168 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 11169 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11170 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11171 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11172 | return( ret ); |
| 11173 | } |
| 11174 | } |
| 11175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11176 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11177 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 11178 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11179 | } |
| 11180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11181 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11182 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11183 | if( transform == NULL ) |
| 11184 | return; |
| 11185 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11186 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11187 | deflateEnd( &transform->ctx_deflate ); |
| 11188 | inflateEnd( &transform->ctx_inflate ); |
| 11189 | #endif |
| 11190 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11191 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); |
| 11192 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 11193 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 11194 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11195 | mbedtls_md_free( &transform->md_ctx_enc ); |
| 11196 | mbedtls_md_free( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 11197 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 11198 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11199 | mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11200 | } |
| 11201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11202 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 11203 | 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] | 11204 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11205 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 11206 | |
| 11207 | while( cur != NULL ) |
| 11208 | { |
| 11209 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11210 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 11211 | cur = next; |
| 11212 | } |
| 11213 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11214 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 11215 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11216 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11217 | |
| 11218 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ) |
| 11219 | { |
| 11220 | unsigned offset; |
| 11221 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 11222 | |
| 11223 | if( hs == NULL ) |
| 11224 | return; |
| 11225 | |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 11226 | ssl_free_buffered_record( ssl ); |
| 11227 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11228 | for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 11229 | ssl_buffering_free_slot( ssl, offset ); |
| 11230 | } |
| 11231 | |
| 11232 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 11233 | uint8_t slot ) |
| 11234 | { |
| 11235 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 11236 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 11237 | |
| 11238 | if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 11239 | return; |
| 11240 | |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 11241 | if( hs_buf->is_valid == 1 ) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11242 | { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 11243 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Hanno Becker | 805f2e1 | 2018-10-12 16:31:41 +0100 | [diff] [blame] | 11244 | mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len ); |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 11245 | mbedtls_free( hs_buf->data ); |
| 11246 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11247 | } |
| 11248 | } |
| 11249 | |
| 11250 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11251 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 11252 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11253 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 11254 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 11255 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11256 | if( handshake == NULL ) |
| 11257 | return; |
| 11258 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 11259 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 11260 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 11261 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 11262 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 11263 | handshake->async_in_progress = 0; |
| 11264 | } |
| 11265 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 11266 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 11267 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 11268 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 11269 | mbedtls_md5_free( &handshake->fin_md5 ); |
| 11270 | mbedtls_sha1_free( &handshake->fin_sha1 ); |
| 11271 | #endif |
| 11272 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 11273 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 11274 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 11275 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 11276 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 11277 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 11278 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 11279 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 11280 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 11281 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 11282 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 11283 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 11284 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 11285 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 11286 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 11287 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 11288 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11289 | #if defined(MBEDTLS_DHM_C) |
| 11290 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11291 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11292 | #if defined(MBEDTLS_ECDH_C) |
| 11293 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 11294 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 11295 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 11296 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 11297 | #if defined(MBEDTLS_SSL_CLI_C) |
| 11298 | mbedtls_free( handshake->ecjpake_cache ); |
| 11299 | handshake->ecjpake_cache = NULL; |
| 11300 | handshake->ecjpake_cache_len = 0; |
| 11301 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 11302 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 11303 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 11304 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 11305 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 11306 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11307 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 11308 | #endif |
| 11309 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 11310 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 11311 | if( handshake->psk != NULL ) |
| 11312 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11313 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 11314 | mbedtls_free( handshake->psk ); |
| 11315 | } |
| 11316 | #endif |
| 11317 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11318 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 11319 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 11320 | /* |
| 11321 | * Free only the linked list wrapper, not the keys themselves |
| 11322 | * since the belong to the SNI callback |
| 11323 | */ |
| 11324 | if( handshake->sni_key_cert != NULL ) |
| 11325 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11326 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 11327 | |
| 11328 | while( cur != NULL ) |
| 11329 | { |
| 11330 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11331 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 11332 | cur = next; |
| 11333 | } |
| 11334 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11335 | #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] | 11336 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 11337 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 11338 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 11339 | if( handshake->ecrs_peer_cert != NULL ) |
| 11340 | { |
| 11341 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 11342 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 11343 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 11344 | #endif |
| 11345 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 11346 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 11347 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 11348 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 11349 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 11350 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11351 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11352 | mbedtls_free( handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 11353 | ssl_flight_free( handshake->flight ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11354 | ssl_buffering_free( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 11355 | #endif |
| 11356 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 11357 | #if defined(MBEDTLS_ECDH_C) && \ |
| 11358 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 11359 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 11360 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 11361 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11362 | mbedtls_platform_zeroize( handshake, |
| 11363 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11364 | } |
| 11365 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11366 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11367 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11368 | if( session == NULL ) |
| 11369 | return; |
| 11370 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11371 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 11372 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 11373 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 11374 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 11375 | #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] | 11376 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 11377 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 11378 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11379 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11380 | } |
| 11381 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 11382 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 11383 | |
| 11384 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 11385 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 11386 | #else |
| 11387 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 11388 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 11389 | |
| 11390 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 11391 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
| 11392 | #else |
| 11393 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u |
| 11394 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 11395 | |
| 11396 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 11397 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 11398 | #else |
| 11399 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 11400 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 11401 | |
| 11402 | #if defined(MBEDTLS_SSL_ALPN) |
| 11403 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 11404 | #else |
| 11405 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 11406 | #endif /* MBEDTLS_SSL_ALPN */ |
| 11407 | |
| 11408 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 11409 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 11410 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 11411 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 11412 | |
| 11413 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 11414 | ( (uint32_t) ( \ |
| 11415 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 11416 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 11417 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 11418 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 11419 | 0u ) ) |
| 11420 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11421 | static unsigned char ssl_serialized_context_header[] = { |
| 11422 | MBEDTLS_VERSION_MAJOR, |
| 11423 | MBEDTLS_VERSION_MINOR, |
| 11424 | MBEDTLS_VERSION_PATCH, |
| 11425 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 11426 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 11427 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF, |
| 11428 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 11429 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11430 | }; |
| 11431 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11432 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11433 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 11434 | * |
| 11435 | * The format of the serialized data is: |
| 11436 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 11437 | * |
| 11438 | * // header |
| 11439 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11440 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 11441 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11442 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 11443 | * Note: When updating the format, remember to keep these |
| 11444 | * version+format bytes. (We may make their size part of the API.) |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 11445 | * |
| 11446 | * // session sub-structure |
| 11447 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 11448 | * // transform sub-structure |
| 11449 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 11450 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 11451 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 11452 | * // fields from ssl_context |
| 11453 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 11454 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 11455 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 11456 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 11457 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 11458 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 11459 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 11460 | * |
| 11461 | * Note that many fields of the ssl_context or sub-structures are not |
| 11462 | * serialized, as they fall in one of the following categories: |
| 11463 | * |
| 11464 | * 1. forced value (eg in_left must be 0) |
| 11465 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 11466 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 11467 | * 4. value was temporary (eg content of input buffer) |
| 11468 | * 5. value will be provided by the user again (eg I/O callbacks and context) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11469 | */ |
| 11470 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 11471 | unsigned char *buf, |
| 11472 | size_t buf_len, |
| 11473 | size_t *olen ) |
| 11474 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11475 | unsigned char *p = buf; |
| 11476 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11477 | size_t session_len; |
| 11478 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11479 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 11480 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11481 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 11482 | * this function's documentation. |
| 11483 | * |
| 11484 | * These are due to assumptions/limitations in the implementation. Some of |
| 11485 | * them are likely to stay (no handshake in progress) some might go away |
| 11486 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 11487 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11488 | /* The initial handshake must be over */ |
| 11489 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11490 | { |
| 11491 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 11492 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11493 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11494 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11495 | { |
| 11496 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11497 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11498 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11499 | /* Double-check that sub-structures are indeed ready */ |
| 11500 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11501 | { |
| 11502 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11503 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11504 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11505 | /* There must be no pending incoming or outgoing data */ |
| 11506 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11507 | { |
| 11508 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11509 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11510 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11511 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11512 | { |
| 11513 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11514 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11515 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11516 | /* Protocol must be DLTS, not TLS */ |
| 11517 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11518 | { |
| 11519 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11520 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11521 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11522 | /* Version must be 1.2 */ |
| 11523 | if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11524 | { |
| 11525 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11526 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11527 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11528 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11529 | { |
| 11530 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11531 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11532 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11533 | /* We must be using an AEAD ciphersuite */ |
| 11534 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11535 | { |
| 11536 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11537 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11538 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11539 | /* Renegotiation must not be enabled */ |
| 11540 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 11541 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11542 | { |
| 11543 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11544 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 11545 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11546 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11547 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11548 | /* |
| 11549 | * Version and format identifier |
| 11550 | */ |
| 11551 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11552 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11553 | if( used <= buf_len ) |
| 11554 | { |
| 11555 | memcpy( p, ssl_serialized_context_header, |
| 11556 | sizeof( ssl_serialized_context_header ) ); |
| 11557 | p += sizeof( ssl_serialized_context_header ); |
| 11558 | } |
| 11559 | |
| 11560 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11561 | * Session (length + data) |
| 11562 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11563 | ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11564 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 11565 | return( ret ); |
| 11566 | |
| 11567 | used += 4 + session_len; |
| 11568 | if( used <= buf_len ) |
| 11569 | { |
| 11570 | *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF ); |
| 11571 | *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF ); |
| 11572 | *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF ); |
| 11573 | *p++ = (unsigned char)( ( session_len ) & 0xFF ); |
| 11574 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11575 | ret = ssl_session_save( ssl->session, 1, |
| 11576 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11577 | if( ret != 0 ) |
| 11578 | return( ret ); |
| 11579 | |
| 11580 | p += session_len; |
| 11581 | } |
| 11582 | |
| 11583 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11584 | * Transform |
| 11585 | */ |
| 11586 | used += sizeof( ssl->transform->randbytes ); |
| 11587 | if( used <= buf_len ) |
| 11588 | { |
| 11589 | memcpy( p, ssl->transform->randbytes, |
| 11590 | sizeof( ssl->transform->randbytes ) ); |
| 11591 | p += sizeof( ssl->transform->randbytes ); |
| 11592 | } |
| 11593 | |
| 11594 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 11595 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 11596 | if( used <= buf_len ) |
| 11597 | { |
| 11598 | *p++ = ssl->transform->in_cid_len; |
| 11599 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 11600 | p += ssl->transform->in_cid_len; |
| 11601 | |
| 11602 | *p++ = ssl->transform->out_cid_len; |
| 11603 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 11604 | p += ssl->transform->out_cid_len; |
| 11605 | } |
| 11606 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 11607 | |
| 11608 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11609 | * Saved fields from top-level ssl_context structure |
| 11610 | */ |
| 11611 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 11612 | used += 4; |
| 11613 | if( used <= buf_len ) |
| 11614 | { |
| 11615 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF ); |
| 11616 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF ); |
| 11617 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF ); |
| 11618 | *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF ); |
| 11619 | } |
| 11620 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 11621 | |
| 11622 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 11623 | used += 16; |
| 11624 | if( used <= buf_len ) |
| 11625 | { |
| 11626 | *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF ); |
| 11627 | *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF ); |
| 11628 | *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF ); |
| 11629 | *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF ); |
| 11630 | *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF ); |
| 11631 | *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF ); |
| 11632 | *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF ); |
| 11633 | *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF ); |
| 11634 | |
| 11635 | *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF ); |
| 11636 | *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF ); |
| 11637 | *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF ); |
| 11638 | *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF ); |
| 11639 | *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF ); |
| 11640 | *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF ); |
| 11641 | *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF ); |
| 11642 | *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF ); |
| 11643 | } |
| 11644 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 11645 | |
| 11646 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11647 | used += 1; |
| 11648 | if( used <= buf_len ) |
| 11649 | { |
| 11650 | *p++ = ssl->disable_datagram_packing; |
| 11651 | } |
| 11652 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11653 | |
| 11654 | used += 8; |
| 11655 | if( used <= buf_len ) |
| 11656 | { |
| 11657 | memcpy( p, ssl->cur_out_ctr, 8 ); |
| 11658 | p += 8; |
| 11659 | } |
| 11660 | |
| 11661 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11662 | used += 2; |
| 11663 | if( used <= buf_len ) |
| 11664 | { |
| 11665 | *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF ); |
| 11666 | *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF ); |
| 11667 | } |
| 11668 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11669 | |
| 11670 | #if defined(MBEDTLS_SSL_ALPN) |
| 11671 | { |
| 11672 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 11673 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11674 | : 0; |
| 11675 | |
| 11676 | used += 1 + alpn_len; |
| 11677 | if( used <= buf_len ) |
| 11678 | { |
| 11679 | *p++ = alpn_len; |
| 11680 | |
| 11681 | if( ssl->alpn_chosen != NULL ) |
| 11682 | { |
| 11683 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 11684 | p += alpn_len; |
| 11685 | } |
| 11686 | } |
| 11687 | } |
| 11688 | #endif /* MBEDTLS_SSL_ALPN */ |
| 11689 | |
| 11690 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11691 | * Done |
| 11692 | */ |
| 11693 | *olen = used; |
| 11694 | |
| 11695 | if( used > buf_len ) |
| 11696 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11697 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11698 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 11699 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame^] | 11700 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11701 | } |
| 11702 | |
| 11703 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11704 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 11705 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 11706 | */ |
| 11707 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 11708 | const char *label, |
| 11709 | const unsigned char *random, size_t rlen, |
| 11710 | unsigned char *dstbuf, size_t dlen ); |
| 11711 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 11712 | { |
Jarno Lamsa | b7b486c | 2019-08-21 15:30:44 +0300 | [diff] [blame] | 11713 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11714 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 11715 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 11716 | |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 11717 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 11718 | return( tls_prf_sha384 ); |
Jarno Lamsa | b7b486c | 2019-08-21 15:30:44 +0300 | [diff] [blame] | 11719 | #else |
| 11720 | (void) ciphersuite_id; |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 11721 | #endif |
| 11722 | return( tls_prf_sha256 ); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11723 | } |
| 11724 | |
| 11725 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 11726 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11727 | * |
| 11728 | * This internal version is wrapped by a public function that cleans up in |
| 11729 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11730 | */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11731 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 11732 | const unsigned char *buf, |
| 11733 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11734 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11735 | const unsigned char *p = buf; |
| 11736 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11737 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 11738 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11739 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 11740 | /* |
| 11741 | * The context should have been freshly setup or reset. |
| 11742 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 11743 | * (Checking session is useful because it won't be NULL if we're |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 11744 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 11745 | */ |
| 11746 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 11747 | ssl->session != NULL ) |
| 11748 | { |
| 11749 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11750 | } |
| 11751 | |
| 11752 | /* |
| 11753 | * We can't check that the config matches the initial one, but we can at |
| 11754 | * least check it matches the requirements for serializing. |
| 11755 | */ |
| 11756 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 11757 | ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 11758 | ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 11759 | ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 || |
| 11760 | ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 || |
| 11761 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 11762 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 11763 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 11764 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 11765 | { |
| 11766 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11767 | } |
| 11768 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11769 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 11770 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11771 | /* |
| 11772 | * Check version identifier |
| 11773 | */ |
| 11774 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 11775 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11776 | |
| 11777 | if( memcmp( p, ssl_serialized_context_header, |
| 11778 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 11779 | { |
| 11780 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 11781 | } |
| 11782 | p += sizeof( ssl_serialized_context_header ); |
| 11783 | |
| 11784 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11785 | * Session |
| 11786 | */ |
| 11787 | if( (size_t)( end - p ) < 4 ) |
| 11788 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11789 | |
| 11790 | session_len = ( (size_t) p[0] << 24 ) | |
| 11791 | ( (size_t) p[1] << 16 ) | |
| 11792 | ( (size_t) p[2] << 8 ) | |
| 11793 | ( (size_t) p[3] ); |
| 11794 | p += 4; |
| 11795 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11796 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame^] | 11797 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11798 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11799 | ssl->session_in = ssl->session; |
| 11800 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11801 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11802 | |
| 11803 | if( (size_t)( end - p ) < session_len ) |
| 11804 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11805 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11806 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11807 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11808 | { |
| 11809 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11810 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11811 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11812 | |
| 11813 | p += session_len; |
| 11814 | |
| 11815 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11816 | * Transform |
| 11817 | */ |
| 11818 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11819 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame^] | 11820 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11821 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11822 | ssl->transform_in = ssl->transform; |
| 11823 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11824 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11825 | |
| 11826 | /* Read random bytes and populate structure */ |
| 11827 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 11828 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11829 | |
| 11830 | ret = ssl_populate_transform( ssl->transform, |
| 11831 | ssl->session->ciphersuite, |
| 11832 | ssl->session->master, |
| 11833 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 11834 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 11835 | ssl->session->encrypt_then_mac, |
| 11836 | #endif |
| 11837 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 11838 | ssl->session->trunc_hmac, |
| 11839 | #endif |
| 11840 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 11841 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 11842 | ssl->session->compression, |
| 11843 | #endif |
| 11844 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
| 11845 | p, /* currently pointing to randbytes */ |
| 11846 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ |
| 11847 | ssl->conf->endpoint, |
| 11848 | ssl ); |
| 11849 | if( ret != 0 ) |
| 11850 | return( ret ); |
| 11851 | |
| 11852 | p += sizeof( ssl->transform->randbytes ); |
| 11853 | |
| 11854 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 11855 | /* Read connection IDs and store them */ |
| 11856 | if( (size_t)( end - p ) < 1 ) |
| 11857 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11858 | |
| 11859 | ssl->transform->in_cid_len = *p++; |
| 11860 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 11861 | if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u ) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11862 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11863 | |
| 11864 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 11865 | p += ssl->transform->in_cid_len; |
| 11866 | |
| 11867 | ssl->transform->out_cid_len = *p++; |
| 11868 | |
| 11869 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 11870 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11871 | |
| 11872 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 11873 | p += ssl->transform->out_cid_len; |
| 11874 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 11875 | |
| 11876 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11877 | * Saved fields from top-level ssl_context structure |
| 11878 | */ |
| 11879 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 11880 | if( (size_t)( end - p ) < 4 ) |
| 11881 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11882 | |
| 11883 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 11884 | ( (uint32_t) p[1] << 16 ) | |
| 11885 | ( (uint32_t) p[2] << 8 ) | |
| 11886 | ( (uint32_t) p[3] ); |
| 11887 | p += 4; |
| 11888 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 11889 | |
| 11890 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 11891 | if( (size_t)( end - p ) < 16 ) |
| 11892 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11893 | |
| 11894 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 11895 | ( (uint64_t) p[1] << 48 ) | |
| 11896 | ( (uint64_t) p[2] << 40 ) | |
| 11897 | ( (uint64_t) p[3] << 32 ) | |
| 11898 | ( (uint64_t) p[4] << 24 ) | |
| 11899 | ( (uint64_t) p[5] << 16 ) | |
| 11900 | ( (uint64_t) p[6] << 8 ) | |
| 11901 | ( (uint64_t) p[7] ); |
| 11902 | p += 8; |
| 11903 | |
| 11904 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 11905 | ( (uint64_t) p[1] << 48 ) | |
| 11906 | ( (uint64_t) p[2] << 40 ) | |
| 11907 | ( (uint64_t) p[3] << 32 ) | |
| 11908 | ( (uint64_t) p[4] << 24 ) | |
| 11909 | ( (uint64_t) p[5] << 16 ) | |
| 11910 | ( (uint64_t) p[6] << 8 ) | |
| 11911 | ( (uint64_t) p[7] ); |
| 11912 | p += 8; |
| 11913 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 11914 | |
| 11915 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11916 | if( (size_t)( end - p ) < 1 ) |
| 11917 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11918 | |
| 11919 | ssl->disable_datagram_packing = *p++; |
| 11920 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11921 | |
| 11922 | if( (size_t)( end - p ) < 8 ) |
| 11923 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11924 | |
| 11925 | memcpy( ssl->cur_out_ctr, p, 8 ); |
| 11926 | p += 8; |
| 11927 | |
| 11928 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11929 | if( (size_t)( end - p ) < 2 ) |
| 11930 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11931 | |
| 11932 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 11933 | p += 2; |
| 11934 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11935 | |
| 11936 | #if defined(MBEDTLS_SSL_ALPN) |
| 11937 | { |
| 11938 | uint8_t alpn_len; |
| 11939 | const char **cur; |
| 11940 | |
| 11941 | if( (size_t)( end - p ) < 1 ) |
| 11942 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11943 | |
| 11944 | alpn_len = *p++; |
| 11945 | |
| 11946 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 11947 | { |
| 11948 | /* alpn_chosen should point to an item in the configured list */ |
| 11949 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 11950 | { |
| 11951 | if( strlen( *cur ) == alpn_len && |
| 11952 | memcmp( p, cur, alpn_len ) == 0 ) |
| 11953 | { |
| 11954 | ssl->alpn_chosen = *cur; |
| 11955 | break; |
| 11956 | } |
| 11957 | } |
| 11958 | } |
| 11959 | |
| 11960 | /* can only happen on conf mismatch */ |
| 11961 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 11962 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11963 | |
| 11964 | p += alpn_len; |
| 11965 | } |
| 11966 | #endif /* MBEDTLS_SSL_ALPN */ |
| 11967 | |
| 11968 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 11969 | * Forced fields from top-level ssl_context structure |
| 11970 | * |
| 11971 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 11972 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 11973 | */ |
| 11974 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
| 11975 | |
| 11976 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 11977 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 11978 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 11979 | /* Adjust pointers for header fields of outgoing records to |
| 11980 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 11981 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 11982 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 11983 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11984 | ssl->in_epoch = 1; |
| 11985 | #endif |
| 11986 | |
| 11987 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 11988 | * which we don't want - otherwise we'd end up freeing the wrong transform |
| 11989 | * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */ |
| 11990 | if( ssl->handshake != NULL ) |
| 11991 | { |
| 11992 | mbedtls_ssl_handshake_free( ssl ); |
| 11993 | mbedtls_free( ssl->handshake ); |
| 11994 | ssl->handshake = NULL; |
| 11995 | } |
| 11996 | |
| 11997 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11998 | * Done - should have consumed entire buffer |
| 11999 | */ |
| 12000 | if( p != end ) |
| 12001 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 12002 | |
| 12003 | return( 0 ); |
| 12004 | } |
| 12005 | |
| 12006 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 12007 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 12008 | */ |
| 12009 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 12010 | const unsigned char *buf, |
| 12011 | size_t len ) |
| 12012 | { |
| 12013 | int ret = ssl_context_load( context, buf, len ); |
| 12014 | |
| 12015 | if( ret != 0 ) |
| 12016 | mbedtls_ssl_free( context ); |
| 12017 | |
| 12018 | return( ret ); |
| 12019 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 12020 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 12021 | |
| 12022 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12023 | * Free an SSL context |
| 12024 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12025 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12026 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 12027 | if( ssl == NULL ) |
| 12028 | return; |
| 12029 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12030 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12031 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 12032 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12033 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 12034 | 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] | 12035 | mbedtls_free( ssl->out_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12036 | } |
| 12037 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 12038 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12039 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 12040 | 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] | 12041 | mbedtls_free( ssl->in_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12042 | } |
| 12043 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12044 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 12045 | if( ssl->compress_buf != NULL ) |
| 12046 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 12047 | 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] | 12048 | mbedtls_free( ssl->compress_buf ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 12049 | } |
| 12050 | #endif |
| 12051 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 12052 | if( ssl->transform ) |
| 12053 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12054 | mbedtls_ssl_transform_free( ssl->transform ); |
| 12055 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 12056 | } |
| 12057 | |
| 12058 | if( ssl->handshake ) |
| 12059 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 12060 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12061 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 12062 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 12063 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12064 | mbedtls_free( ssl->handshake ); |
| 12065 | mbedtls_free( ssl->transform_negotiate ); |
| 12066 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 12067 | } |
| 12068 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 12069 | if( ssl->session ) |
| 12070 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12071 | mbedtls_ssl_session_free( ssl->session ); |
| 12072 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 12073 | } |
| 12074 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 12075 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 12076 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12077 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12078 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12079 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12080 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 12081 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12082 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12083 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 12084 | if( mbedtls_ssl_hw_record_finish != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 12085 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12086 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); |
| 12087 | mbedtls_ssl_hw_record_finish( ssl ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 12088 | } |
| 12089 | #endif |
| 12090 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 12091 | #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] | 12092 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 12093 | #endif |
| 12094 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12095 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 12096 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 12097 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12098 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12099 | } |
| 12100 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12101 | /* |
| 12102 | * Initialze mbedtls_ssl_config |
| 12103 | */ |
| 12104 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 12105 | { |
| 12106 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 12107 | } |
| 12108 | |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 12109 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 12110 | static int ssl_preset_default_hashes[] = { |
| 12111 | #if defined(MBEDTLS_SHA512_C) |
| 12112 | MBEDTLS_MD_SHA512, |
| 12113 | MBEDTLS_MD_SHA384, |
| 12114 | #endif |
| 12115 | #if defined(MBEDTLS_SHA256_C) |
| 12116 | MBEDTLS_MD_SHA256, |
| 12117 | MBEDTLS_MD_SHA224, |
| 12118 | #endif |
Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 12119 | #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] | 12120 | MBEDTLS_MD_SHA1, |
| 12121 | #endif |
| 12122 | MBEDTLS_MD_NONE |
| 12123 | }; |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 12124 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 12125 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12126 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 12127 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 12128 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 12129 | 0 |
| 12130 | }; |
| 12131 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12132 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12133 | static int ssl_preset_suiteb_hashes[] = { |
| 12134 | MBEDTLS_MD_SHA256, |
| 12135 | MBEDTLS_MD_SHA384, |
| 12136 | MBEDTLS_MD_NONE |
| 12137 | }; |
| 12138 | #endif |
| 12139 | |
| 12140 | #if defined(MBEDTLS_ECP_C) |
| 12141 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 12142 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12143 | MBEDTLS_ECP_DP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 12144 | #endif |
| 12145 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12146 | MBEDTLS_ECP_DP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 12147 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12148 | MBEDTLS_ECP_DP_NONE |
| 12149 | }; |
| 12150 | #endif |
| 12151 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12152 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 12153 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12154 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 12155 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12156 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12157 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 12158 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 12159 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 12160 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12161 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 12162 | /* Use the functions here so that they are covered in tests, |
| 12163 | * but otherwise access member directly for efficiency */ |
| 12164 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 12165 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12166 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12167 | /* |
| 12168 | * Things that are common to all presets |
| 12169 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 12170 | #if defined(MBEDTLS_SSL_CLI_C) |
| 12171 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 12172 | { |
| 12173 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 12174 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 12175 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 12176 | #endif |
| 12177 | } |
| 12178 | #endif |
| 12179 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 12180 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12181 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 12182 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12183 | |
| 12184 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 12185 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 12186 | #endif |
| 12187 | |
| 12188 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 12189 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 12190 | #endif |
| 12191 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 12192 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 12193 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; |
| 12194 | #endif |
| 12195 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 12196 | #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] | 12197 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 12198 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 12199 | #endif |
| 12200 | |
| 12201 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 12202 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 12203 | #endif |
| 12204 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 12205 | #if defined(MBEDTLS_SSL_SRV_C) |
| 12206 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
| 12207 | #endif |
| 12208 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12209 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 12210 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 12211 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 12212 | #endif |
| 12213 | |
| 12214 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 12215 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 12216 | memset( conf->renego_period, 0x00, 2 ); |
| 12217 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12218 | #endif |
| 12219 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12220 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 12221 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 12222 | { |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 12223 | const unsigned char dhm_p[] = |
| 12224 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 12225 | const unsigned char dhm_g[] = |
| 12226 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
| 12227 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 12228 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 12229 | dhm_p, sizeof( dhm_p ), |
| 12230 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12231 | { |
| 12232 | return( ret ); |
| 12233 | } |
| 12234 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 12235 | #endif |
| 12236 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12237 | /* |
| 12238 | * Preset-specific defaults |
| 12239 | */ |
| 12240 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12241 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12242 | /* |
| 12243 | * NSA Suite B |
| 12244 | */ |
| 12245 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 12246 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 12247 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 12248 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 12249 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 12250 | |
| 12251 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 12252 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 12253 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 12254 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 12255 | ssl_preset_suiteb_ciphersuites; |
| 12256 | |
| 12257 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 12258 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12259 | #endif |
| 12260 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12261 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12262 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
| 12263 | #endif |
| 12264 | |
| 12265 | #if defined(MBEDTLS_ECP_C) |
| 12266 | conf->curve_list = ssl_preset_suiteb_curves; |
| 12267 | #endif |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 12268 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12269 | |
| 12270 | /* |
| 12271 | * Default |
| 12272 | */ |
| 12273 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 12274 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 12275 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 12276 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 12277 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 12278 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 12279 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 12280 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 12281 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12282 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 12283 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 12284 | |
| 12285 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 12286 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 12287 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 12288 | #endif |
| 12289 | |
| 12290 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 12291 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 12292 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 12293 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 12294 | mbedtls_ssl_list_ciphersuites(); |
| 12295 | |
| 12296 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 12297 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 12298 | #endif |
| 12299 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12300 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 12301 | conf->sig_hashes = ssl_preset_default_hashes; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12302 | #endif |
| 12303 | |
| 12304 | #if defined(MBEDTLS_ECP_C) |
| 12305 | conf->curve_list = mbedtls_ecp_grp_id_list(); |
| 12306 | #endif |
| 12307 | |
| 12308 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 12309 | conf->dhm_min_bitlen = 1024; |
| 12310 | #endif |
| 12311 | } |
| 12312 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12313 | return( 0 ); |
| 12314 | } |
| 12315 | |
| 12316 | /* |
| 12317 | * Free mbedtls_ssl_config |
| 12318 | */ |
| 12319 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 12320 | { |
| 12321 | #if defined(MBEDTLS_DHM_C) |
| 12322 | mbedtls_mpi_free( &conf->dhm_P ); |
| 12323 | mbedtls_mpi_free( &conf->dhm_G ); |
| 12324 | #endif |
| 12325 | |
| 12326 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 12327 | if( conf->psk != NULL ) |
| 12328 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12329 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12330 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 12331 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12332 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 12333 | } |
| 12334 | |
| 12335 | if( conf->psk_identity != NULL ) |
| 12336 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12337 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 12338 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 12339 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12340 | conf->psk_identity_len = 0; |
| 12341 | } |
| 12342 | #endif |
| 12343 | |
| 12344 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 12345 | ssl_key_cert_free( conf->key_cert ); |
| 12346 | #endif |
| 12347 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12348 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12349 | } |
| 12350 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 12351 | #if defined(MBEDTLS_PK_C) && \ |
| 12352 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12353 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12354 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12355 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12356 | 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] | 12357 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12358 | #if defined(MBEDTLS_RSA_C) |
| 12359 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 12360 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12361 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12362 | #if defined(MBEDTLS_ECDSA_C) |
| 12363 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 12364 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12365 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12366 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12367 | } |
| 12368 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 12369 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 12370 | { |
| 12371 | switch( type ) { |
| 12372 | case MBEDTLS_PK_RSA: |
| 12373 | return( MBEDTLS_SSL_SIG_RSA ); |
| 12374 | case MBEDTLS_PK_ECDSA: |
| 12375 | case MBEDTLS_PK_ECKEY: |
| 12376 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 12377 | default: |
| 12378 | return( MBEDTLS_SSL_SIG_ANON ); |
| 12379 | } |
| 12380 | } |
| 12381 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12382 | 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] | 12383 | { |
| 12384 | switch( sig ) |
| 12385 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12386 | #if defined(MBEDTLS_RSA_C) |
| 12387 | case MBEDTLS_SSL_SIG_RSA: |
| 12388 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12389 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12390 | #if defined(MBEDTLS_ECDSA_C) |
| 12391 | case MBEDTLS_SSL_SIG_ECDSA: |
| 12392 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12393 | #endif |
| 12394 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12395 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12396 | } |
| 12397 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 12398 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12399 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 12400 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 12401 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 12402 | |
| 12403 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 12404 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 12405 | mbedtls_pk_type_t sig_alg ) |
| 12406 | { |
| 12407 | switch( sig_alg ) |
| 12408 | { |
| 12409 | case MBEDTLS_PK_RSA: |
| 12410 | return( set->rsa ); |
| 12411 | case MBEDTLS_PK_ECDSA: |
| 12412 | return( set->ecdsa ); |
| 12413 | default: |
| 12414 | return( MBEDTLS_MD_NONE ); |
| 12415 | } |
| 12416 | } |
| 12417 | |
| 12418 | /* Add a signature-hash-pair to a signature-hash set */ |
| 12419 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 12420 | mbedtls_pk_type_t sig_alg, |
| 12421 | mbedtls_md_type_t md_alg ) |
| 12422 | { |
| 12423 | switch( sig_alg ) |
| 12424 | { |
| 12425 | case MBEDTLS_PK_RSA: |
| 12426 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 12427 | set->rsa = md_alg; |
| 12428 | break; |
| 12429 | |
| 12430 | case MBEDTLS_PK_ECDSA: |
| 12431 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 12432 | set->ecdsa = md_alg; |
| 12433 | break; |
| 12434 | |
| 12435 | default: |
| 12436 | break; |
| 12437 | } |
| 12438 | } |
| 12439 | |
| 12440 | /* Allow exactly one hash algorithm for each signature. */ |
| 12441 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 12442 | mbedtls_md_type_t md_alg ) |
| 12443 | { |
| 12444 | set->rsa = md_alg; |
| 12445 | set->ecdsa = md_alg; |
| 12446 | } |
| 12447 | |
| 12448 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
| 12449 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 12450 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 12451 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12452 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 12453 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12454 | 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] | 12455 | { |
| 12456 | switch( hash ) |
| 12457 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12458 | #if defined(MBEDTLS_MD5_C) |
| 12459 | case MBEDTLS_SSL_HASH_MD5: |
| 12460 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12461 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12462 | #if defined(MBEDTLS_SHA1_C) |
| 12463 | case MBEDTLS_SSL_HASH_SHA1: |
| 12464 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12465 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12466 | #if defined(MBEDTLS_SHA256_C) |
| 12467 | case MBEDTLS_SSL_HASH_SHA224: |
| 12468 | return( MBEDTLS_MD_SHA224 ); |
| 12469 | case MBEDTLS_SSL_HASH_SHA256: |
| 12470 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12471 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12472 | #if defined(MBEDTLS_SHA512_C) |
| 12473 | case MBEDTLS_SSL_HASH_SHA384: |
| 12474 | return( MBEDTLS_MD_SHA384 ); |
| 12475 | case MBEDTLS_SSL_HASH_SHA512: |
| 12476 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12477 | #endif |
| 12478 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12479 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12480 | } |
| 12481 | } |
| 12482 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12483 | /* |
| 12484 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 12485 | */ |
| 12486 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 12487 | { |
| 12488 | switch( md ) |
| 12489 | { |
| 12490 | #if defined(MBEDTLS_MD5_C) |
| 12491 | case MBEDTLS_MD_MD5: |
| 12492 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 12493 | #endif |
| 12494 | #if defined(MBEDTLS_SHA1_C) |
| 12495 | case MBEDTLS_MD_SHA1: |
| 12496 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 12497 | #endif |
| 12498 | #if defined(MBEDTLS_SHA256_C) |
| 12499 | case MBEDTLS_MD_SHA224: |
| 12500 | return( MBEDTLS_SSL_HASH_SHA224 ); |
| 12501 | case MBEDTLS_MD_SHA256: |
| 12502 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 12503 | #endif |
| 12504 | #if defined(MBEDTLS_SHA512_C) |
| 12505 | case MBEDTLS_MD_SHA384: |
| 12506 | return( MBEDTLS_SSL_HASH_SHA384 ); |
| 12507 | case MBEDTLS_MD_SHA512: |
| 12508 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 12509 | #endif |
| 12510 | default: |
| 12511 | return( MBEDTLS_SSL_HASH_NONE ); |
| 12512 | } |
| 12513 | } |
| 12514 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 12515 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12516 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12517 | * 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] | 12518 | * 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] | 12519 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 12520 | 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] | 12521 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12522 | const mbedtls_ecp_group_id *gid; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12523 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 12524 | if( ssl->conf->curve_list == NULL ) |
| 12525 | return( -1 ); |
| 12526 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 12527 | for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12528 | if( *gid == grp_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 12529 | return( 0 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12530 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 12531 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12532 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 12533 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12534 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12535 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12536 | /* |
| 12537 | * Check if a hash proposed by the peer is in our list. |
| 12538 | * Return 0 if we're willing to use it, -1 otherwise. |
| 12539 | */ |
| 12540 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 12541 | mbedtls_md_type_t md ) |
| 12542 | { |
| 12543 | const int *cur; |
| 12544 | |
| 12545 | if( ssl->conf->sig_hashes == NULL ) |
| 12546 | return( -1 ); |
| 12547 | |
| 12548 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) |
| 12549 | if( *cur == (int) md ) |
| 12550 | return( 0 ); |
| 12551 | |
| 12552 | return( -1 ); |
| 12553 | } |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12554 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12555 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12556 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 12557 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 12558 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12559 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 12560 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12561 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12562 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12563 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12564 | int usage = 0; |
| 12565 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12566 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12567 | const char *ext_oid; |
| 12568 | size_t ext_len; |
| 12569 | #endif |
| 12570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12571 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \ |
| 12572 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12573 | ((void) cert); |
| 12574 | ((void) cert_endpoint); |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12575 | ((void) flags); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12576 | #endif |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12577 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12578 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 12579 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12580 | { |
| 12581 | /* Server part of the key exchange */ |
| 12582 | switch( ciphersuite->key_exchange ) |
| 12583 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12584 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 12585 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12586 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12587 | break; |
| 12588 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12589 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 12590 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 12591 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 12592 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12593 | break; |
| 12594 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12595 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 12596 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12597 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12598 | break; |
| 12599 | |
| 12600 | /* 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] | 12601 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 12602 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 12603 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 12604 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 12605 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12606 | usage = 0; |
| 12607 | } |
| 12608 | } |
| 12609 | else |
| 12610 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12611 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 12612 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12613 | } |
| 12614 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12615 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12616 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12617 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12618 | ret = -1; |
| 12619 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12620 | #else |
| 12621 | ((void) ciphersuite); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12622 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12623 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12624 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 12625 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12626 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12627 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 12628 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12629 | } |
| 12630 | else |
| 12631 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12632 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 12633 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12634 | } |
| 12635 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12636 | 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] | 12637 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12638 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12639 | ret = -1; |
| 12640 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12641 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12642 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12643 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12644 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12645 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 12646 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12647 | /* |
| 12648 | * Convert version numbers to/from wire format |
| 12649 | * and, for DTLS, to/from TLS equivalent. |
| 12650 | * |
| 12651 | * For TLS this is the identity. |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 12652 | * For DTLS, use 1's complement (v -> 255 - v, and then map as follows: |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12653 | * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1) |
| 12654 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
| 12655 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12656 | void mbedtls_ssl_write_version( int major, int minor, int transport, |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12657 | unsigned char ver[2] ) |
| 12658 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12659 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 12660 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12661 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12662 | if( minor == MBEDTLS_SSL_MINOR_VERSION_2 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12663 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 12664 | |
| 12665 | ver[0] = (unsigned char)( 255 - ( major - 2 ) ); |
| 12666 | ver[1] = (unsigned char)( 255 - ( minor - 1 ) ); |
| 12667 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 12668 | else |
| 12669 | #else |
| 12670 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12671 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 12672 | { |
| 12673 | ver[0] = (unsigned char) major; |
| 12674 | ver[1] = (unsigned char) minor; |
| 12675 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12676 | } |
| 12677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12678 | void mbedtls_ssl_read_version( int *major, int *minor, int transport, |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12679 | const unsigned char ver[2] ) |
| 12680 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12681 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 12682 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12683 | { |
| 12684 | *major = 255 - ver[0] + 2; |
| 12685 | *minor = 255 - ver[1] + 1; |
| 12686 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12687 | if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12688 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 12689 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 12690 | else |
| 12691 | #else |
| 12692 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12693 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 12694 | { |
| 12695 | *major = ver[0]; |
| 12696 | *minor = ver[1]; |
| 12697 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12698 | } |
| 12699 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12700 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 12701 | { |
| 12702 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 12703 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 12704 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 12705 | |
| 12706 | switch( md ) |
| 12707 | { |
| 12708 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 12709 | #if defined(MBEDTLS_MD5_C) |
| 12710 | case MBEDTLS_SSL_HASH_MD5: |
Janos Follath | 182013f | 2016-10-25 10:50:22 +0100 | [diff] [blame] | 12711 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12712 | #endif |
| 12713 | #if defined(MBEDTLS_SHA1_C) |
| 12714 | case MBEDTLS_SSL_HASH_SHA1: |
| 12715 | ssl->handshake->calc_verify = ssl_calc_verify_tls; |
| 12716 | break; |
| 12717 | #endif |
| 12718 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 12719 | #if defined(MBEDTLS_SHA512_C) |
| 12720 | case MBEDTLS_SSL_HASH_SHA384: |
| 12721 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 12722 | break; |
| 12723 | #endif |
| 12724 | #if defined(MBEDTLS_SHA256_C) |
| 12725 | case MBEDTLS_SSL_HASH_SHA256: |
| 12726 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 12727 | break; |
| 12728 | #endif |
| 12729 | default: |
| 12730 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 12731 | } |
| 12732 | |
| 12733 | return 0; |
| 12734 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 12735 | (void) ssl; |
| 12736 | (void) md; |
| 12737 | |
| 12738 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 12739 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 12740 | } |
| 12741 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12742 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 12743 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 12744 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, |
| 12745 | unsigned char *output, |
| 12746 | unsigned char *data, size_t data_len ) |
| 12747 | { |
| 12748 | int ret = 0; |
| 12749 | mbedtls_md5_context mbedtls_md5; |
| 12750 | mbedtls_sha1_context mbedtls_sha1; |
| 12751 | |
| 12752 | mbedtls_md5_init( &mbedtls_md5 ); |
| 12753 | mbedtls_sha1_init( &mbedtls_sha1 ); |
| 12754 | |
| 12755 | /* |
| 12756 | * digitally-signed struct { |
| 12757 | * opaque md5_hash[16]; |
| 12758 | * opaque sha_hash[20]; |
| 12759 | * }; |
| 12760 | * |
| 12761 | * md5_hash |
| 12762 | * MD5(ClientHello.random + ServerHello.random |
| 12763 | * + ServerParams); |
| 12764 | * sha_hash |
| 12765 | * SHA(ClientHello.random + ServerHello.random |
| 12766 | * + ServerParams); |
| 12767 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12768 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12769 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12770 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12771 | goto exit; |
| 12772 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12773 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12774 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 12775 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12776 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12777 | goto exit; |
| 12778 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12779 | 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] | 12780 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12781 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12782 | goto exit; |
| 12783 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12784 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12785 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12786 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12787 | goto exit; |
| 12788 | } |
| 12789 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12790 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12791 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12792 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12793 | goto exit; |
| 12794 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12795 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12796 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 12797 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12798 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12799 | goto exit; |
| 12800 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12801 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12802 | data_len ) ) != 0 ) |
| 12803 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12804 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12805 | goto exit; |
| 12806 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12807 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12808 | output + 16 ) ) != 0 ) |
| 12809 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12810 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12811 | goto exit; |
| 12812 | } |
| 12813 | |
| 12814 | exit: |
| 12815 | mbedtls_md5_free( &mbedtls_md5 ); |
| 12816 | mbedtls_sha1_free( &mbedtls_sha1 ); |
| 12817 | |
| 12818 | if( ret != 0 ) |
| 12819 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 12820 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 12821 | |
| 12822 | return( ret ); |
| 12823 | |
| 12824 | } |
| 12825 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 12826 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 12827 | |
| 12828 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 12829 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12830 | |
| 12831 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 12832 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 12833 | unsigned char *hash, size_t *hashlen, |
| 12834 | unsigned char *data, size_t data_len, |
| 12835 | mbedtls_md_type_t md_alg ) |
| 12836 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12837 | psa_status_t status; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 12838 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12839 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 12840 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 12841 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12842 | |
| 12843 | if( ( status = psa_hash_setup( &hash_operation, |
| 12844 | hash_alg ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12845 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12846 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12847 | goto exit; |
| 12848 | } |
| 12849 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12850 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 12851 | 64 ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12852 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12853 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12854 | goto exit; |
| 12855 | } |
| 12856 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12857 | if( ( status = psa_hash_update( &hash_operation, |
| 12858 | data, data_len ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12859 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12860 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12861 | goto exit; |
| 12862 | } |
| 12863 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12864 | if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE, |
| 12865 | hashlen ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12866 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12867 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12868 | goto exit; |
| 12869 | } |
| 12870 | |
| 12871 | exit: |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12872 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12873 | { |
| 12874 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 12875 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12876 | switch( status ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12877 | { |
| 12878 | case PSA_ERROR_NOT_SUPPORTED: |
| 12879 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12880 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12881 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 12882 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 12883 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 12884 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 12885 | default: |
| 12886 | return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED ); |
| 12887 | } |
| 12888 | } |
| 12889 | return( 0 ); |
| 12890 | } |
| 12891 | |
| 12892 | #else |
| 12893 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12894 | 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] | 12895 | unsigned char *hash, size_t *hashlen, |
| 12896 | unsigned char *data, size_t data_len, |
| 12897 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12898 | { |
| 12899 | int ret = 0; |
| 12900 | mbedtls_md_context_t ctx; |
| 12901 | 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] | 12902 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12903 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 12904 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12905 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12906 | mbedtls_md_init( &ctx ); |
| 12907 | |
| 12908 | /* |
| 12909 | * digitally-signed struct { |
| 12910 | * opaque client_random[32]; |
| 12911 | * opaque server_random[32]; |
| 12912 | * ServerDHParams params; |
| 12913 | * }; |
| 12914 | */ |
| 12915 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 12916 | { |
| 12917 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 12918 | goto exit; |
| 12919 | } |
| 12920 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 12921 | { |
| 12922 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 12923 | goto exit; |
| 12924 | } |
| 12925 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 12926 | { |
| 12927 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 12928 | goto exit; |
| 12929 | } |
| 12930 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 12931 | { |
| 12932 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 12933 | goto exit; |
| 12934 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 12935 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12936 | { |
| 12937 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 12938 | goto exit; |
| 12939 | } |
| 12940 | |
| 12941 | exit: |
| 12942 | mbedtls_md_free( &ctx ); |
| 12943 | |
| 12944 | if( ret != 0 ) |
| 12945 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 12946 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 12947 | |
| 12948 | return( ret ); |
| 12949 | } |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12950 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 12951 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12952 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 12953 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 12954 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12955 | #endif /* MBEDTLS_SSL_TLS_C */ |