Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 shared functions |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | /* |
| 22 | * The SSL 3.0 specification was drafted by Netscape in 1996, |
| 23 | * and became an IETF standard in 1999. |
| 24 | * |
| 25 | * http://wp.netscape.com/eng/ssl3/ |
| 26 | * http://www.ietf.org/rfc/rfc2246.txt |
| 27 | * http://www.ietf.org/rfc/rfc4346.txt |
| 28 | */ |
| 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | #if defined(MBEDTLS_PLATFORM_C) |
| 39 | #include "mbedtls/platform.h" |
| 40 | #else |
| 41 | #include <stdlib.h> |
| 42 | #define mbedtls_calloc calloc |
| 43 | #define mbedtls_free free |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 44 | #endif |
| 45 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/debug.h" |
| 47 | #include "mbedtls/ssl.h" |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 48 | #include "mbedtls/ssl_internal.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 49 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 50 | #include "mbedtls/version.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 51 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 52 | #include <string.h> |
| 53 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 54 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 55 | #include "mbedtls/psa_util.h" |
| 56 | #include "psa/crypto.h" |
| 57 | #endif |
| 58 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 59 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 60 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 61 | #endif |
| 62 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 63 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ); |
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 | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 66 | /* Length of the "epoch" field in the record header */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 68 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 69 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 70 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 71 | return( 2 ); |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 72 | #else |
| 73 | ((void) ssl); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 74 | #endif |
| 75 | return( 0 ); |
| 76 | } |
| 77 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 78 | /* |
| 79 | * Start a timer. |
| 80 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 81 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 83 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 84 | if( ssl->f_set_timer == NULL ) |
| 85 | return; |
| 86 | |
| 87 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); |
| 88 | ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Return -1 is timer is expired, 0 if it isn't. |
| 93 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | static int ssl_check_timer( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 95 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 96 | if( ssl->f_get_timer == NULL ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 97 | return( 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 98 | |
| 99 | if( ssl->f_get_timer( ssl->p_timer ) == 2 ) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 100 | { |
| 101 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 102 | return( -1 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 103 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 104 | |
| 105 | return( 0 ); |
| 106 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 107 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 108 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 109 | mbedtls_ssl_transform *transform ); |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 110 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 111 | |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 112 | #if defined(MBEDTLS_SSL_RECORD_CHECKING) |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 113 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, |
| 114 | unsigned char *buf, |
| 115 | size_t len, |
| 116 | mbedtls_record *rec ); |
| 117 | |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 118 | int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl, |
| 119 | unsigned char *buf, |
| 120 | size_t buflen ) |
| 121 | { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 122 | int ret = 0; |
| 123 | mbedtls_record rec; |
| 124 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) ); |
| 125 | MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen ); |
| 126 | |
| 127 | /* We don't support record checking in TLS because |
| 128 | * (a) there doesn't seem to be a usecase for it, and |
| 129 | * (b) In SSLv3 and TLS 1.0, CBC record decryption has state |
| 130 | * and we'd need to backup the transform here. |
| 131 | */ |
| 132 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM ) |
| 133 | { |
| 134 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 135 | goto exit; |
| 136 | } |
| 137 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 138 | else |
| 139 | { |
| 140 | ret = ssl_parse_record_header( ssl, buf, buflen, &rec ); |
| 141 | if( ret != 0 ) |
| 142 | { |
| 143 | MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret ); |
| 144 | goto exit; |
| 145 | } |
| 146 | |
| 147 | if( ssl->transform_in != NULL ) |
| 148 | { |
| 149 | ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec ); |
| 150 | if( ret != 0 ) |
| 151 | { |
| 152 | MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret ); |
| 153 | goto exit; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 158 | |
| 159 | exit: |
| 160 | /* On success, we have decrypted the buffer in-place, so make |
| 161 | * sure we don't leak any plaintext data. */ |
| 162 | mbedtls_platform_zeroize( buf, buflen ); |
| 163 | |
| 164 | /* For the purpose of this API, treat messages with unexpected CID |
| 165 | * as well as such from future epochs as unexpected. */ |
| 166 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID || |
| 167 | ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 168 | { |
| 169 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 170 | } |
| 171 | |
| 172 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) ); |
| 173 | return( ret ); |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 174 | } |
| 175 | #endif /* MBEDTLS_SSL_RECORD_CHECKING */ |
| 176 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 177 | #define SSL_DONT_FORCE_FLUSH 0 |
| 178 | #define SSL_FORCE_FLUSH 1 |
| 179 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 180 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 181 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 182 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 183 | /* Top-level Connection ID API */ |
| 184 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 185 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 186 | size_t len, |
| 187 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 188 | { |
| 189 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 190 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 191 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 192 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 193 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 194 | { |
| 195 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 196 | } |
| 197 | |
| 198 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 199 | conf->cid_len = len; |
| 200 | return( 0 ); |
| 201 | } |
| 202 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 203 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 204 | int enable, |
| 205 | unsigned char const *own_cid, |
| 206 | size_t own_cid_len ) |
| 207 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 208 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 209 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 210 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 211 | ssl->negotiate_cid = enable; |
| 212 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 213 | { |
| 214 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 215 | return( 0 ); |
| 216 | } |
| 217 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 218 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 219 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 220 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 221 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 222 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 223 | (unsigned) own_cid_len, |
| 224 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 225 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 226 | } |
| 227 | |
| 228 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 229 | /* Truncation is not an issue here because |
| 230 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 231 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 232 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 233 | return( 0 ); |
| 234 | } |
| 235 | |
| 236 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 237 | int *enabled, |
| 238 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 239 | size_t *peer_cid_len ) |
| 240 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 241 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 242 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 243 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 244 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 245 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 246 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 247 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 248 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 249 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 250 | * were used, but client and server requested the empty CID. |
| 251 | * This is indistinguishable from not using the CID extension |
| 252 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 253 | if( ssl->transform_in->in_cid_len == 0 && |
| 254 | ssl->transform_in->out_cid_len == 0 ) |
| 255 | { |
| 256 | return( 0 ); |
| 257 | } |
| 258 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 259 | if( peer_cid_len != NULL ) |
| 260 | { |
| 261 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 262 | if( peer_cid != NULL ) |
| 263 | { |
| 264 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 265 | ssl->transform_in->out_cid_len ); |
| 266 | } |
| 267 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 268 | |
| 269 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 270 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 271 | return( 0 ); |
| 272 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 273 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 274 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 275 | /* Forward declarations for functions related to message buffering. */ |
| 276 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ); |
| 277 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 278 | uint8_t slot ); |
| 279 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ); |
| 280 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ); |
| 281 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ); |
| 282 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 283 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, |
| 284 | mbedtls_record const *rec ); |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 285 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 286 | |
Hanno Becker | a67dee2 | 2018-08-22 10:05:20 +0100 | [diff] [blame] | 287 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ); |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 288 | 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] | 289 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 290 | size_t mtu = ssl_get_current_mtu( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 291 | |
| 292 | if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN ) |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 293 | return( mtu ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 294 | |
| 295 | return( MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 296 | } |
| 297 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 298 | static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl ) |
| 299 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 300 | size_t const bytes_written = ssl->out_left; |
| 301 | size_t const mtu = ssl_get_maximum_datagram_size( ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 302 | |
| 303 | /* Double-check that the write-index hasn't gone |
| 304 | * past what we can transmit in a single datagram. */ |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 305 | if( bytes_written > mtu ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 306 | { |
| 307 | /* Should never happen... */ |
| 308 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 309 | } |
| 310 | |
| 311 | return( (int) ( mtu - bytes_written ) ); |
| 312 | } |
| 313 | |
| 314 | static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl ) |
| 315 | { |
| 316 | int ret; |
| 317 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 318 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 319 | |
| 320 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 321 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 322 | |
| 323 | if( max_len > mfl ) |
| 324 | max_len = mfl; |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 325 | |
| 326 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 327 | * only limits the maximum record payload size, so in theory |
| 328 | * we would be allowed to pack multiple records of payload size |
| 329 | * MFL into a single datagram. However, this would mean that there's |
| 330 | * no way to explicitly communicate MTU restrictions to the peer. |
| 331 | * |
| 332 | * The following reduction of max_len makes sure that we never |
| 333 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 334 | */ |
| 335 | if( max_len <= ssl->out_left ) |
| 336 | return( 0 ); |
| 337 | |
| 338 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 339 | #endif |
| 340 | |
| 341 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
| 342 | if( ret < 0 ) |
| 343 | return( ret ); |
| 344 | remaining = (size_t) ret; |
| 345 | |
| 346 | ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 347 | if( ret < 0 ) |
| 348 | return( ret ); |
| 349 | expansion = (size_t) ret; |
| 350 | |
| 351 | if( remaining <= expansion ) |
| 352 | return( 0 ); |
| 353 | |
| 354 | remaining -= expansion; |
| 355 | if( remaining >= max_len ) |
| 356 | remaining = max_len; |
| 357 | |
| 358 | return( (int) remaining ); |
| 359 | } |
| 360 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 361 | /* |
| 362 | * Double the retransmit timeout value, within the allowed range, |
| 363 | * returning -1 if the maximum value has already been reached. |
| 364 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 365 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 366 | { |
| 367 | uint32_t new_timeout; |
| 368 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 369 | if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 370 | return( -1 ); |
| 371 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 372 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 373 | * in the following way: after the initial transmission and a first |
| 374 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 375 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 376 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 377 | * on most non-IP stacks too. */ |
| 378 | if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min ) |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 379 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 380 | ssl->handshake->mtu = 508; |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 381 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) ); |
| 382 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 383 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 384 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 385 | |
| 386 | /* Avoid arithmetic overflow and range overflow */ |
| 387 | if( new_timeout < ssl->handshake->retransmit_timeout || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 388 | new_timeout > ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 389 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 390 | new_timeout = ssl->conf->hs_timeout_max; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | ssl->handshake->retransmit_timeout = new_timeout; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 394 | 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] | 395 | ssl->handshake->retransmit_timeout ) ); |
| 396 | |
| 397 | return( 0 ); |
| 398 | } |
| 399 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 400 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 401 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 402 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 403 | 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] | 404 | ssl->handshake->retransmit_timeout ) ); |
| 405 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 406 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 407 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 408 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 409 | /* |
| 410 | * Convert max_fragment_length codes to length. |
| 411 | * RFC 6066 says: |
| 412 | * enum{ |
| 413 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 414 | * } MaxFragmentLength; |
| 415 | * and we add 0 -> extension unused |
| 416 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 417 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 418 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 419 | switch( mfl ) |
| 420 | { |
| 421 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 422 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 423 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 424 | return 512; |
| 425 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 426 | return 1024; |
| 427 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 428 | return 2048; |
| 429 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 430 | return 4096; |
| 431 | default: |
| 432 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 433 | } |
| 434 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 435 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 436 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 437 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 438 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 439 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 440 | mbedtls_ssl_session_free( dst ); |
| 441 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 442 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 443 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 444 | |
| 445 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 446 | if( src->peer_cert != NULL ) |
| 447 | { |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 448 | int ret; |
| 449 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 450 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 451 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 452 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 453 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 454 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 455 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 456 | 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] | 457 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 458 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 460 | dst->peer_cert = NULL; |
| 461 | return( ret ); |
| 462 | } |
| 463 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 464 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 465 | if( src->peer_cert_digest != NULL ) |
| 466 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 467 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 468 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 469 | if( dst->peer_cert_digest == NULL ) |
| 470 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 471 | |
| 472 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 473 | src->peer_cert_digest_len ); |
| 474 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 475 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 476 | } |
| 477 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 479 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 480 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 481 | #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] | 482 | if( src->ticket != NULL ) |
| 483 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 484 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 485 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 486 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 487 | |
| 488 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 489 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 490 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 491 | |
| 492 | return( 0 ); |
| 493 | } |
| 494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 495 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 496 | int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl, |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 497 | const unsigned char *key_enc, const unsigned char *key_dec, |
| 498 | size_t keylen, |
| 499 | const unsigned char *iv_enc, const unsigned char *iv_dec, |
| 500 | size_t ivlen, |
| 501 | const unsigned char *mac_enc, const unsigned char *mac_dec, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 502 | size_t maclen ) = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 503 | int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL; |
| 504 | int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL; |
| 505 | int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL; |
| 506 | int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL; |
| 507 | int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL; |
| 508 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 509 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 510 | /* |
| 511 | * Key material generation |
| 512 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 513 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 514 | static int ssl3_prf( const unsigned char *secret, size_t slen, |
| 515 | const char *label, |
| 516 | const unsigned char *random, size_t rlen, |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 517 | unsigned char *dstbuf, size_t dlen ) |
| 518 | { |
Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 519 | int ret = 0; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 520 | size_t i; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 521 | mbedtls_md5_context md5; |
| 522 | mbedtls_sha1_context sha1; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 523 | unsigned char padding[16]; |
| 524 | unsigned char sha1sum[20]; |
| 525 | ((void)label); |
| 526 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 527 | mbedtls_md5_init( &md5 ); |
| 528 | mbedtls_sha1_init( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 529 | |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 530 | /* |
| 531 | * SSLv3: |
| 532 | * block = |
| 533 | * MD5( secret + SHA1( 'A' + secret + random ) ) + |
| 534 | * MD5( secret + SHA1( 'BB' + secret + random ) ) + |
| 535 | * MD5( secret + SHA1( 'CCC' + secret + random ) ) + |
| 536 | * ... |
| 537 | */ |
| 538 | for( i = 0; i < dlen / 16; i++ ) |
| 539 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 540 | memset( padding, (unsigned char) ('A' + i), 1 + i ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 541 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 542 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 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_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 545 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 546 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 547 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 548 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 549 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 550 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 551 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 552 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 553 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 554 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 555 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 556 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 557 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 558 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 559 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 560 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 563 | exit: |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 564 | mbedtls_md5_free( &md5 ); |
| 565 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 566 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 567 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); |
| 568 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 569 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 570 | return( ret ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 571 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 572 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 573 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 574 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 575 | static int tls1_prf( const unsigned char *secret, size_t slen, |
| 576 | const char *label, |
| 577 | const unsigned char *random, size_t rlen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 578 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 579 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 580 | size_t nb, hs; |
| 581 | size_t i, j, k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 582 | const unsigned char *S1, *S2; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 583 | unsigned char *tmp; |
| 584 | size_t tmp_len = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 585 | unsigned char h_i[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 586 | const mbedtls_md_info_t *md_info; |
| 587 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 588 | int ret; |
| 589 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 590 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 591 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 592 | tmp_len = 20 + strlen( label ) + rlen; |
| 593 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 594 | if( tmp == NULL ) |
| 595 | { |
| 596 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 597 | goto exit; |
| 598 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 599 | |
| 600 | hs = ( slen + 1 ) / 2; |
| 601 | S1 = secret; |
| 602 | S2 = secret + slen - hs; |
| 603 | |
| 604 | nb = strlen( label ); |
| 605 | memcpy( tmp + 20, label, nb ); |
| 606 | memcpy( tmp + 20 + nb, random, rlen ); |
| 607 | nb += rlen; |
| 608 | |
| 609 | /* |
| 610 | * First compute P_md5(secret,label+random)[0..dlen] |
| 611 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 612 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 613 | { |
| 614 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 615 | goto exit; |
| 616 | } |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 618 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 619 | { |
| 620 | goto exit; |
| 621 | } |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); |
| 624 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 625 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 626 | |
| 627 | for( i = 0; i < dlen; i += 16 ) |
| 628 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 629 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 630 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); |
| 631 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 633 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 634 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); |
| 635 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 636 | |
| 637 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; |
| 638 | |
| 639 | for( j = 0; j < k; j++ ) |
| 640 | dstbuf[i + j] = h_i[j]; |
| 641 | } |
| 642 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 643 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 644 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 645 | /* |
| 646 | * XOR out with P_sha1(secret,label+random)[0..dlen] |
| 647 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 648 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 649 | { |
| 650 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 651 | goto exit; |
| 652 | } |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 655 | { |
| 656 | goto exit; |
| 657 | } |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 658 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 659 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); |
| 660 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 661 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 662 | |
| 663 | for( i = 0; i < dlen; i += 20 ) |
| 664 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 666 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); |
| 667 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 669 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 670 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); |
| 671 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 672 | |
| 673 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; |
| 674 | |
| 675 | for( j = 0; j < k; j++ ) |
| 676 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); |
| 677 | } |
| 678 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 679 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 680 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 681 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 682 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 683 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 684 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 685 | mbedtls_free( tmp ); |
| 686 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 687 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 689 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 690 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 691 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 692 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 693 | const unsigned char *secret, size_t slen, |
| 694 | const char *label, |
| 695 | const unsigned char *random, size_t rlen, |
| 696 | unsigned char *dstbuf, size_t dlen ) |
| 697 | { |
| 698 | psa_status_t status; |
| 699 | psa_algorithm_t alg; |
Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 700 | psa_key_attributes_t key_attributes; |
Andrzej Kurek | ac5dc34 | 2019-01-23 06:57:34 -0500 | [diff] [blame] | 701 | psa_key_handle_t master_slot; |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 702 | psa_key_derivation_operation_t derivation = |
Janos Follath | 8dee877 | 2019-07-30 12:53:32 +0100 | [diff] [blame] | 703 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 704 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 705 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 706 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 707 | else |
| 708 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 709 | |
Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 710 | key_attributes = psa_key_attributes_init(); |
| 711 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 712 | psa_set_key_algorithm( &key_attributes, alg ); |
| 713 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 714 | |
Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 715 | status = psa_import_key( &key_attributes, secret, slen, &master_slot ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 716 | if( status != PSA_SUCCESS ) |
| 717 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 718 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 719 | status = psa_key_derivation( &derivation, |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 720 | master_slot, alg, |
| 721 | random, rlen, |
| 722 | (unsigned char const *) label, |
| 723 | (size_t) strlen( label ), |
| 724 | dlen ); |
| 725 | if( status != PSA_SUCCESS ) |
| 726 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 727 | psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 728 | psa_destroy_key( master_slot ); |
| 729 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 730 | } |
| 731 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 732 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 733 | if( status != PSA_SUCCESS ) |
| 734 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 735 | psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 736 | psa_destroy_key( master_slot ); |
| 737 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 738 | } |
| 739 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 740 | status = psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 741 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 742 | { |
| 743 | psa_destroy_key( master_slot ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 744 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 745 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 746 | |
| 747 | status = psa_destroy_key( master_slot ); |
| 748 | if( status != PSA_SUCCESS ) |
| 749 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 750 | |
Andrzej Kurek | 3317126 | 2019-01-15 03:25:18 -0500 | [diff] [blame] | 751 | return( 0 ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 755 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 756 | 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] | 757 | const unsigned char *secret, size_t slen, |
| 758 | const char *label, |
| 759 | const unsigned char *random, size_t rlen, |
| 760 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 761 | { |
| 762 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 763 | size_t i, j, k, md_len; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 764 | unsigned char *tmp; |
| 765 | size_t tmp_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 766 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 767 | const mbedtls_md_info_t *md_info; |
| 768 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 769 | int ret; |
| 770 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 771 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 772 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 773 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 774 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 775 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 776 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 777 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 778 | tmp_len = md_len + strlen( label ) + rlen; |
| 779 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 780 | if( tmp == NULL ) |
| 781 | { |
| 782 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 783 | goto exit; |
| 784 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 785 | |
| 786 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 787 | memcpy( tmp + md_len, label, nb ); |
| 788 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 789 | nb += rlen; |
| 790 | |
| 791 | /* |
| 792 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 793 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 794 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 795 | goto exit; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 796 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 797 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 798 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 799 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 800 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 801 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 802 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 803 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 804 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 805 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 807 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 808 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 809 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 810 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 811 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 812 | |
| 813 | for( j = 0; j < k; j++ ) |
| 814 | dstbuf[i + j] = h_i[j]; |
| 815 | } |
| 816 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 817 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 818 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 819 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 820 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 821 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 822 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 823 | mbedtls_free( tmp ); |
| 824 | |
| 825 | return( ret ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 826 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 827 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 828 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 829 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 830 | const char *label, |
| 831 | const unsigned char *random, size_t rlen, |
| 832 | unsigned char *dstbuf, size_t dlen ) |
| 833 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 834 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 835 | label, random, rlen, dstbuf, dlen ) ); |
| 836 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 837 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 838 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 839 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 840 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 841 | const char *label, |
| 842 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 843 | unsigned char *dstbuf, size_t dlen ) |
| 844 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 845 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 846 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 847 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 848 | #endif /* MBEDTLS_SHA512_C */ |
| 849 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 850 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 851 | 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] | 852 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 853 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 854 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 855 | 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] | 856 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 857 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 858 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 859 | 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] | 860 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 861 | #endif |
| 862 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 863 | #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] | 864 | 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] | 865 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 866 | #endif |
| 867 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 868 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 869 | #if defined(MBEDTLS_SHA256_C) |
| 870 | 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] | 871 | 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] | 872 | 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] | 873 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 874 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 875 | #if defined(MBEDTLS_SHA512_C) |
| 876 | 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] | 877 | 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] | 878 | 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] | 879 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 880 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 881 | |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 882 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 883 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 884 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) |
| 885 | { |
| 886 | if( ssl->conf->f_psk != NULL ) |
| 887 | { |
| 888 | /* If we've used a callback to select the PSK, |
| 889 | * the static configuration is irrelevant. */ |
| 890 | if( ssl->handshake->psk_opaque != 0 ) |
| 891 | return( 1 ); |
| 892 | |
| 893 | return( 0 ); |
| 894 | } |
| 895 | |
| 896 | if( ssl->conf->psk_opaque != 0 ) |
| 897 | return( 1 ); |
| 898 | |
| 899 | return( 0 ); |
| 900 | } |
| 901 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 902 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 903 | |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 904 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 905 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 906 | { |
| 907 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 908 | if( tls_prf == ssl3_prf ) |
| 909 | { |
Ron Eldor | 0810f0b | 2019-05-15 12:32:32 +0300 | [diff] [blame] | 910 | return( MBEDTLS_SSL_TLS_PRF_SSL3 ); |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 911 | } |
| 912 | else |
| 913 | #endif |
| 914 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 915 | if( tls_prf == tls1_prf ) |
| 916 | { |
| 917 | return( MBEDTLS_SSL_TLS_PRF_TLS1 ); |
| 918 | } |
| 919 | else |
| 920 | #endif |
| 921 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 922 | #if defined(MBEDTLS_SHA512_C) |
| 923 | if( tls_prf == tls_prf_sha384 ) |
| 924 | { |
| 925 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 926 | } |
| 927 | else |
| 928 | #endif |
| 929 | #if defined(MBEDTLS_SHA256_C) |
| 930 | if( tls_prf == tls_prf_sha256 ) |
| 931 | { |
| 932 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 933 | } |
| 934 | else |
| 935 | #endif |
| 936 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 937 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 938 | } |
| 939 | #endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
| 940 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 941 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 942 | const unsigned char *secret, size_t slen, |
| 943 | const char *label, |
| 944 | const unsigned char *random, size_t rlen, |
| 945 | unsigned char *dstbuf, size_t dlen ) |
| 946 | { |
| 947 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 948 | |
| 949 | switch( prf ) |
| 950 | { |
| 951 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 952 | case MBEDTLS_SSL_TLS_PRF_SSL3: |
| 953 | tls_prf = ssl3_prf; |
| 954 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 955 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 956 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 957 | case MBEDTLS_SSL_TLS_PRF_TLS1: |
| 958 | tls_prf = tls1_prf; |
| 959 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 960 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 961 | |
| 962 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 963 | #if defined(MBEDTLS_SHA512_C) |
| 964 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 965 | tls_prf = tls_prf_sha384; |
| 966 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 967 | #endif /* MBEDTLS_SHA512_C */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 968 | #if defined(MBEDTLS_SHA256_C) |
| 969 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 970 | tls_prf = tls_prf_sha256; |
| 971 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 972 | #endif /* MBEDTLS_SHA256_C */ |
| 973 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 974 | default: |
| 975 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 976 | } |
| 977 | |
| 978 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 979 | } |
| 980 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 981 | /* Type for the TLS PRF */ |
| 982 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 983 | const unsigned char *, size_t, |
| 984 | unsigned char *, size_t); |
| 985 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 986 | /* |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 987 | * Populate a transform structure with session keys and all the other |
| 988 | * necessary information. |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 989 | * |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 990 | * Parameters: |
| 991 | * - [in/out]: transform: structure to populate |
| 992 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 993 | * [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] | 994 | * - [in] ciphersuite |
| 995 | * - [in] master |
| 996 | * - [in] encrypt_then_mac |
| 997 | * - [in] trunc_hmac |
| 998 | * - [in] compression |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 999 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 1000 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1001 | * - [in] minor_ver: SSL/TLS minor version |
| 1002 | * - [in] endpoint: client or server |
| 1003 | * - [in] ssl: optionally used for: |
| 1004 | * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context |
| 1005 | * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys |
| 1006 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1007 | */ |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1008 | static int ssl_populate_transform( mbedtls_ssl_transform *transform, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1009 | int ciphersuite, |
| 1010 | const unsigned char master[48], |
| 1011 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1012 | int encrypt_then_mac, |
| 1013 | #endif |
| 1014 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1015 | int trunc_hmac, |
| 1016 | #endif |
| 1017 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1018 | int compression, |
| 1019 | #endif |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1020 | ssl_tls_prf_t tls_prf, |
| 1021 | const unsigned char randbytes[64], |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1022 | int minor_ver, |
| 1023 | unsigned endpoint, |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1024 | const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1025 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1026 | int ret = 0; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1027 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1028 | int psa_fallthrough; |
| 1029 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1030 | unsigned char keyblk[256]; |
| 1031 | unsigned char *key1; |
| 1032 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1033 | unsigned char *mac_enc; |
| 1034 | unsigned char *mac_dec; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1035 | size_t mac_key_len; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1036 | size_t iv_copy_len; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1037 | unsigned keylen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1038 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1039 | const mbedtls_cipher_info_t *cipher_info; |
| 1040 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1041 | |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1042 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \ |
| 1043 | !defined(MBEDTLS_SSL_EXPORT_KEYS) && \ |
| 1044 | !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1045 | 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] | 1046 | (void) ssl; |
| 1047 | #endif |
| 1048 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 1049 | /* |
| 1050 | * Some data just needs copying into the structure |
| 1051 | */ |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1052 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 1053 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1054 | transform->encrypt_then_mac = encrypt_then_mac; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1055 | #endif |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1056 | transform->minor_ver = minor_ver; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1057 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 1058 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 1059 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 1060 | #endif |
| 1061 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1062 | /* |
| 1063 | * Get various info structures |
| 1064 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1065 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1066 | if( ciphersuite_info == NULL ) |
| 1067 | { |
| 1068 | 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] | 1069 | ciphersuite ) ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1070 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1071 | } |
| 1072 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1073 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1074 | if( cipher_info == NULL ) |
| 1075 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1076 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1077 | ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1078 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1079 | } |
| 1080 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1081 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1082 | if( md_info == NULL ) |
| 1083 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1084 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1085 | ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1086 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1089 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1090 | /* Copy own and peer's CID if the use of the CID |
| 1091 | * extension has been negotiated. */ |
| 1092 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 1093 | { |
| 1094 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | 8a7f972 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 1095 | |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 1096 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 1097 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 1c1f046 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 1098 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1099 | transform->in_cid_len ); |
Hanno Becker | d1f2035 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 1100 | |
| 1101 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 1102 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 1103 | ssl->handshake->peer_cid_len ); |
| 1104 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 1105 | transform->out_cid_len ); |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1106 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1107 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1108 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1109 | /* |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1110 | * Compute key block using the PRF |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1111 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1112 | 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] | 1113 | if( ret != 0 ) |
| 1114 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1115 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1116 | return( ret ); |
| 1117 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1118 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1119 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1120 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1121 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1122 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1123 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1124 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1125 | /* |
| 1126 | * Determine the appropriate key, IV and MAC length. |
| 1127 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1128 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1129 | keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1130 | |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1131 | #if defined(MBEDTLS_GCM_C) || \ |
| 1132 | defined(MBEDTLS_CCM_C) || \ |
| 1133 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1134 | if( cipher_info->mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1135 | cipher_info->mode == MBEDTLS_MODE_CCM || |
| 1136 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1137 | { |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1138 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1139 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1140 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1141 | mac_key_len = 0; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1142 | transform->taglen = |
| 1143 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1144 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1145 | /* All modes haves 96-bit IVs; |
| 1146 | * GCM and CCM has 4 implicit and 8 explicit bytes |
| 1147 | * ChachaPoly has all 12 bytes implicit |
| 1148 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1149 | transform->ivlen = 12; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1150 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1151 | transform->fixed_ivlen = 12; |
| 1152 | else |
| 1153 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1154 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1155 | /* Minimum length of encrypted record */ |
| 1156 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1157 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1158 | } |
| 1159 | else |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1160 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 1161 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 1162 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 1163 | cipher_info->mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1164 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1165 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1166 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 1167 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1168 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1169 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1170 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1171 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1172 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1173 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1174 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 1175 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1176 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1177 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1178 | /* |
| 1179 | * If HMAC is to be truncated, we shall keep the leftmost bytes, |
| 1180 | * (rfc 6066 page 13 or rfc 2104 section 4), |
| 1181 | * so we only need to adjust the length here. |
| 1182 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1183 | if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1184 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1185 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1186 | |
| 1187 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) |
| 1188 | /* Fall back to old, non-compliant version of the truncated |
Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 1189 | * HMAC implementation which also truncates the key |
| 1190 | * (Mbed TLS versions from 1.3 to 2.6.0) */ |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1191 | mac_key_len = transform->maclen; |
| 1192 | #endif |
| 1193 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1194 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1195 | |
| 1196 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1197 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1198 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1199 | /* Minimum length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1200 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1201 | transform->minlen = transform->maclen; |
| 1202 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1203 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1204 | /* |
| 1205 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1206 | * 1. if EtM is in use: one block plus MAC |
| 1207 | * otherwise: * first multiple of blocklen greater than maclen |
| 1208 | * 2. IV except for SSL3 and TLS 1.0 |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1209 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1210 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1211 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1212 | { |
| 1213 | transform->minlen = transform->maclen |
| 1214 | + cipher_info->block_size; |
| 1215 | } |
| 1216 | else |
| 1217 | #endif |
| 1218 | { |
| 1219 | transform->minlen = transform->maclen |
| 1220 | + cipher_info->block_size |
| 1221 | - transform->maclen % cipher_info->block_size; |
| 1222 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1223 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1224 | #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] | 1225 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 1226 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1227 | ; /* No need to adjust minlen */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1228 | else |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1229 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1230 | #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] | 1231 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || |
| 1232 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1233 | { |
| 1234 | transform->minlen += transform->ivlen; |
| 1235 | } |
| 1236 | else |
| 1237 | #endif |
| 1238 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1239 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1240 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1241 | goto end; |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1242 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1243 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1244 | } |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1245 | else |
| 1246 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1247 | { |
| 1248 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1249 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1250 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1251 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1252 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 1253 | (unsigned) keylen, |
| 1254 | (unsigned) transform->minlen, |
| 1255 | (unsigned) transform->ivlen, |
| 1256 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1257 | |
| 1258 | /* |
| 1259 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 1260 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1261 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1262 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1263 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1264 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1265 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1266 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1267 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1268 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1269 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1270 | /* |
| 1271 | * This is not used in TLS v1.1. |
| 1272 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1273 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1274 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1275 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 1276 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1277 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1278 | } |
| 1279 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1280 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1281 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1282 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1283 | { |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1284 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1285 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1286 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1287 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1288 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1289 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1290 | /* |
| 1291 | * This is not used in TLS v1.1. |
| 1292 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1293 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1294 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1295 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 1296 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1297 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1298 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1299 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1300 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1301 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1302 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1303 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1304 | goto end; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1305 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1306 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1307 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1308 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1309 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1310 | { |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1311 | if( mac_key_len > sizeof( transform->mac_enc ) ) |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1312 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1313 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1314 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1315 | goto end; |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1316 | } |
| 1317 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1318 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); |
| 1319 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1320 | } |
| 1321 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1322 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1323 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1324 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1325 | if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1326 | { |
Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1327 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 1328 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 1329 | if( mac_key_len != 0 ) |
| 1330 | { |
| 1331 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 1332 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 1333 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1334 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1335 | else |
| 1336 | #endif |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1337 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1338 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1339 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1340 | goto end; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1341 | } |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1342 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1344 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 1345 | if( mbedtls_ssl_hw_record_init != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1346 | { |
| 1347 | int ret = 0; |
| 1348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1349 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1350 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1351 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1352 | transform->iv_enc, transform->iv_dec, |
| 1353 | iv_copy_len, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1354 | mac_enc, mac_dec, |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1355 | mac_key_len ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1356 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1357 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1358 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1359 | goto end; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1360 | } |
| 1361 | } |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1362 | #else |
| 1363 | ((void) mac_dec); |
| 1364 | ((void) mac_enc); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1365 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1366 | |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1367 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 1368 | if( ssl->conf->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1369 | { |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1370 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1371 | master, keyblk, |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1372 | mac_key_len, keylen, |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1373 | iv_copy_len ); |
| 1374 | } |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1375 | |
| 1376 | if( ssl->conf->f_export_keys_ext != NULL ) |
| 1377 | { |
| 1378 | 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] | 1379 | master, keyblk, |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1380 | mac_key_len, keylen, |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1381 | iv_copy_len, |
Manuel Pégourié-Gonnard | 344460c | 2019-07-25 13:17:38 +0200 | [diff] [blame] | 1382 | /* work around bug in exporter type */ |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1383 | (unsigned char *) randbytes + 32, |
| 1384 | (unsigned char *) randbytes, |
| 1385 | tls_prf_get_type( tls_prf ) ); |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1386 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1387 | #endif |
| 1388 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1389 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1390 | |
| 1391 | /* Only use PSA-based ciphers for TLS-1.2. |
| 1392 | * That's relevant at least for TLS-1.0, where |
| 1393 | * we assume that mbedtls_cipher_crypt() updates |
| 1394 | * the structure field for the IV, which the PSA-based |
| 1395 | * implementation currently doesn't. */ |
| 1396 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1397 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1398 | { |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1399 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc, |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1400 | cipher_info, transform->taglen ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1401 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 1402 | { |
| 1403 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1404 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | if( ret == 0 ) |
| 1408 | { |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1409 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1410 | psa_fallthrough = 0; |
| 1411 | } |
| 1412 | else |
| 1413 | { |
| 1414 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) ); |
| 1415 | psa_fallthrough = 1; |
| 1416 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1417 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1418 | else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1419 | psa_fallthrough = 1; |
| 1420 | #else |
| 1421 | psa_fallthrough = 1; |
| 1422 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1423 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1424 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1425 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1426 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1427 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1428 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1429 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1430 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1431 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1432 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1433 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1434 | /* Only use PSA-based ciphers for TLS-1.2. |
| 1435 | * That's relevant at least for TLS-1.0, where |
| 1436 | * we assume that mbedtls_cipher_crypt() updates |
| 1437 | * the structure field for the IV, which the PSA-based |
| 1438 | * implementation currently doesn't. */ |
| 1439 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1440 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1441 | { |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1442 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec, |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1443 | cipher_info, transform->taglen ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1444 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 1445 | { |
| 1446 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1447 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | if( ret == 0 ) |
| 1451 | { |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1452 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1453 | psa_fallthrough = 0; |
| 1454 | } |
| 1455 | else |
| 1456 | { |
| 1457 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) ); |
| 1458 | psa_fallthrough = 1; |
| 1459 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1460 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1461 | else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1462 | psa_fallthrough = 1; |
| 1463 | #else |
| 1464 | psa_fallthrough = 1; |
| 1465 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1466 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1467 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1468 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1469 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1470 | cipher_info ) ) != 0 ) |
| 1471 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1472 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1473 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1474 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1475 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1476 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1477 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1478 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1479 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1480 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", 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 | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1483 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1484 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1485 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1486 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1487 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1488 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1489 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1490 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1491 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1492 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1493 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1494 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1495 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1496 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1497 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1498 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1499 | goto end; |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1500 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1501 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1502 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1503 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1504 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1505 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1506 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1507 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1508 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1509 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1510 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1511 | |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1512 | /* Initialize Zlib contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1513 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1514 | if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1515 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1516 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1517 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1518 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); |
| 1519 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1520 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1521 | if( deflateInit( &transform->ctx_deflate, |
| 1522 | Z_DEFAULT_COMPRESSION ) != Z_OK || |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1523 | inflateInit( &transform->ctx_inflate ) != Z_OK ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1524 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1525 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1526 | ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED; |
| 1527 | goto end; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1528 | } |
| 1529 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1530 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1531 | |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1532 | end: |
Ron Eldor | a9f9a73 | 2019-05-07 18:29:02 +0300 | [diff] [blame] | 1533 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1534 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1537 | /* |
Manuel Pégourié-Gonnard | 47e33e1 | 2019-05-20 10:10:17 +0200 | [diff] [blame] | 1538 | * 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] | 1539 | * |
| 1540 | * Inputs: |
| 1541 | * - SSL/TLS minor version |
| 1542 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1543 | * |
Manuel Pégourié-Gonnard | 31d3ef1 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1544 | * Outputs: |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1545 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1546 | */ |
| 1547 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1548 | int minor_ver, |
| 1549 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1550 | { |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1551 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) |
| 1552 | (void) hash; |
| 1553 | #endif |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1554 | |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1555 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1556 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1557 | { |
| 1558 | handshake->tls_prf = ssl3_prf; |
| 1559 | handshake->calc_verify = ssl_calc_verify_ssl; |
| 1560 | handshake->calc_finished = ssl_calc_finished_ssl; |
| 1561 | } |
| 1562 | else |
| 1563 | #endif |
| 1564 | #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] | 1565 | if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1566 | { |
| 1567 | handshake->tls_prf = tls1_prf; |
| 1568 | handshake->calc_verify = ssl_calc_verify_tls; |
| 1569 | handshake->calc_finished = ssl_calc_finished_tls; |
| 1570 | } |
| 1571 | else |
| 1572 | #endif |
| 1573 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1574 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1575 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1576 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1577 | { |
| 1578 | handshake->tls_prf = tls_prf_sha384; |
| 1579 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1580 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1581 | } |
| 1582 | else |
| 1583 | #endif |
| 1584 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1585 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1586 | { |
| 1587 | handshake->tls_prf = tls_prf_sha256; |
| 1588 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1589 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1590 | } |
| 1591 | else |
| 1592 | #endif |
| 1593 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1594 | { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1595 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1596 | } |
| 1597 | |
| 1598 | return( 0 ); |
| 1599 | } |
| 1600 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1601 | /* |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1602 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1603 | * |
| 1604 | * Parameters: |
| 1605 | * [in/out] handshake |
| 1606 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1607 | * (PSA-PSK) ciphersuite_info, psk_opaque |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1608 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1609 | * [out] master |
| 1610 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 1611 | * debug: conf->f_dbg, conf->p_dbg |
| 1612 | * EMS: passed to calc_verify (debug + (SSL3) session_negotiate) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1613 | * PSA-PSA: minor_ver, conf |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1614 | */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1615 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1616 | unsigned char *master, |
Manuel Pégourié-Gonnard | 0d56aaa | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1617 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1618 | { |
| 1619 | int ret; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1620 | |
| 1621 | /* cf. RFC 5246, Section 8.1: |
| 1622 | * "The master secret is always exactly 48 bytes in length." */ |
| 1623 | size_t const master_secret_len = 48; |
| 1624 | |
| 1625 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1626 | unsigned char session_hash[48]; |
| 1627 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 1628 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1629 | /* The label for the KDF used for key expansion. |
| 1630 | * This is either "master secret" or "extended master secret" |
| 1631 | * depending on whether the Extended Master Secret extension |
| 1632 | * is used. */ |
| 1633 | char const *lbl = "master secret"; |
| 1634 | |
| 1635 | /* The salt for the KDF used for key expansion. |
| 1636 | * - If the Extended Master Secret extension is not used, |
| 1637 | * this is ClientHello.Random + ServerHello.Random |
| 1638 | * (see Sect. 8.1 in RFC 5246). |
| 1639 | * - If the Extended Master Secret extension is used, |
| 1640 | * this is the transcript of the handshake so far. |
| 1641 | * (see Sect. 4 in RFC 7627). */ |
| 1642 | unsigned char const *salt = handshake->randbytes; |
| 1643 | size_t salt_len = 64; |
| 1644 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1645 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 1646 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 1647 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1648 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1649 | 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] | 1650 | (void) ssl; |
| 1651 | #endif |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1652 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1653 | if( handshake->resume != 0 ) |
| 1654 | { |
| 1655 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1656 | return( 0 ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1657 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1658 | |
| 1659 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1660 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1661 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1662 | lbl = "extended master secret"; |
| 1663 | salt = session_hash; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1664 | handshake->calc_verify( ssl, session_hash, &salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1665 | |
Manuel Pégourié-Gonnard | 8faa70e | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1666 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 1667 | session_hash, salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1668 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1669 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
| 1670 | |
| 1671 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1672 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1673 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1674 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1675 | ssl_use_opaque_psk( ssl ) == 1 ) |
| 1676 | { |
| 1677 | /* Perform PSK-to-MS expansion in a single step. */ |
| 1678 | psa_status_t status; |
| 1679 | psa_algorithm_t alg; |
| 1680 | psa_key_handle_t psk; |
| 1681 | psa_key_derivation_operation_t derivation = |
| 1682 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1683 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1684 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1685 | 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] | 1686 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1687 | psk = ssl->conf->psk_opaque; |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1688 | if( handshake->psk_opaque != 0 ) |
| 1689 | psk = handshake->psk_opaque; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1690 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1691 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1692 | 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] | 1693 | else |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1694 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1695 | |
| 1696 | status = psa_key_derivation( &derivation, psk, alg, |
| 1697 | salt, salt_len, |
| 1698 | (unsigned char const *) lbl, |
| 1699 | (size_t) strlen( lbl ), |
| 1700 | master_secret_len ); |
| 1701 | if( status != PSA_SUCCESS ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1702 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1703 | psa_key_derivation_abort( &derivation ); |
| 1704 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1705 | } |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1706 | |
| 1707 | status = psa_key_derivation_output_bytes( &derivation, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1708 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1709 | master_secret_len ); |
| 1710 | if( status != PSA_SUCCESS ) |
| 1711 | { |
| 1712 | psa_key_derivation_abort( &derivation ); |
| 1713 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1714 | } |
| 1715 | |
| 1716 | status = psa_key_derivation_abort( &derivation ); |
| 1717 | if( status != PSA_SUCCESS ) |
| 1718 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1719 | } |
| 1720 | else |
| 1721 | #endif |
| 1722 | { |
| 1723 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1724 | lbl, salt, salt_len, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1725 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1726 | master_secret_len ); |
| 1727 | if( ret != 0 ) |
| 1728 | { |
| 1729 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1730 | return( ret ); |
| 1731 | } |
| 1732 | |
| 1733 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 1734 | handshake->premaster, |
| 1735 | handshake->pmslen ); |
| 1736 | |
| 1737 | mbedtls_platform_zeroize( handshake->premaster, |
| 1738 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | return( 0 ); |
| 1742 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1743 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1744 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1745 | { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1746 | int ret; |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1747 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 1748 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1749 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1750 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1751 | |
| 1752 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1753 | ret = ssl_set_handshake_prfs( ssl->handshake, |
| 1754 | ssl->minor_ver, |
| 1755 | ciphersuite_info->mac ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1756 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1757 | { |
| 1758 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1759 | return( ret ); |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1760 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1761 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1762 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1763 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1764 | ssl->session_negotiate->master, |
| 1765 | ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1766 | if( ret != 0 ) |
| 1767 | { |
| 1768 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1769 | return( ret ); |
| 1770 | } |
| 1771 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1772 | /* Swap the client and server random values: |
| 1773 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1774 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1775 | { |
| 1776 | unsigned char tmp[64]; |
| 1777 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1778 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1779 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1780 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1781 | } |
| 1782 | |
| 1783 | /* Populate transform structure */ |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1784 | ret = ssl_populate_transform( ssl->transform_negotiate, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1785 | ssl->session_negotiate->ciphersuite, |
| 1786 | ssl->session_negotiate->master, |
| 1787 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1788 | ssl->session_negotiate->encrypt_then_mac, |
| 1789 | #endif |
| 1790 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1791 | ssl->session_negotiate->trunc_hmac, |
| 1792 | #endif |
| 1793 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1794 | ssl->session_negotiate->compression, |
| 1795 | #endif |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1796 | ssl->handshake->tls_prf, |
| 1797 | ssl->handshake->randbytes, |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1798 | ssl->minor_ver, |
| 1799 | ssl->conf->endpoint, |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1800 | ssl ); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1801 | if( ret != 0 ) |
| 1802 | { |
| 1803 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret ); |
| 1804 | return( ret ); |
| 1805 | } |
| 1806 | |
| 1807 | /* We no longer need Server/ClientHello.random values */ |
| 1808 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1809 | sizeof( ssl->handshake->randbytes ) ); |
| 1810 | |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1811 | /* Allocate compression buffer */ |
| 1812 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1813 | if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE && |
| 1814 | ssl->compress_buf == NULL ) |
| 1815 | { |
| 1816 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); |
| 1817 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
| 1818 | if( ssl->compress_buf == NULL ) |
| 1819 | { |
| 1820 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1821 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1822 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1823 | } |
| 1824 | } |
| 1825 | #endif |
| 1826 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1827 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1828 | |
| 1829 | return( 0 ); |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1830 | } |
| 1831 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1832 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1833 | void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl, |
| 1834 | unsigned char hash[36], |
| 1835 | size_t *hlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1836 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1837 | mbedtls_md5_context md5; |
| 1838 | mbedtls_sha1_context sha1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1839 | unsigned char pad_1[48]; |
| 1840 | unsigned char pad_2[48]; |
| 1841 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1842 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1843 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1844 | mbedtls_md5_init( &md5 ); |
| 1845 | mbedtls_sha1_init( &sha1 ); |
| 1846 | |
| 1847 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1848 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1849 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1850 | memset( pad_1, 0x36, 48 ); |
| 1851 | memset( pad_2, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1852 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1853 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1854 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); |
| 1855 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1856 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1857 | mbedtls_md5_starts_ret( &md5 ); |
| 1858 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1859 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); |
| 1860 | mbedtls_md5_update_ret( &md5, hash, 16 ); |
| 1861 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1862 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1863 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1864 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); |
| 1865 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1866 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1867 | mbedtls_sha1_starts_ret( &sha1 ); |
| 1868 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1869 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); |
| 1870 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); |
| 1871 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1872 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1873 | *hlen = 36; |
| 1874 | |
| 1875 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1876 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1877 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1878 | mbedtls_md5_free( &md5 ); |
| 1879 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1880 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1881 | return; |
| 1882 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1883 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1884 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1885 | #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] | 1886 | void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl, |
| 1887 | unsigned char hash[36], |
| 1888 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +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 | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1892 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1893 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1894 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1895 | mbedtls_md5_init( &md5 ); |
| 1896 | mbedtls_sha1_init( &sha1 ); |
| 1897 | |
| 1898 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1899 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1900 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1901 | mbedtls_md5_finish_ret( &md5, hash ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1902 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1903 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1904 | *hlen = 36; |
| 1905 | |
| 1906 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1907 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1908 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1909 | mbedtls_md5_free( &md5 ); |
| 1910 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1911 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1912 | return; |
| 1913 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1914 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1915 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1916 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1917 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1918 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 1919 | unsigned char hash[32], |
| 1920 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1921 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1922 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1923 | size_t hash_size; |
| 1924 | psa_status_t status; |
| 1925 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 1926 | |
| 1927 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 1928 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 1929 | if( status != PSA_SUCCESS ) |
| 1930 | { |
| 1931 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1932 | return; |
| 1933 | } |
| 1934 | |
| 1935 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 1936 | if( status != PSA_SUCCESS ) |
| 1937 | { |
| 1938 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1939 | return; |
| 1940 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1941 | |
| 1942 | *hlen = 32; |
| 1943 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1944 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1945 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1946 | mbedtls_sha256_context sha256; |
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_sha256_init( &sha256 ); |
| 1949 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1950 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1951 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1952 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1953 | mbedtls_sha256_finish_ret( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1954 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1955 | *hlen = 32; |
| 1956 | |
| 1957 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1958 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1959 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1960 | mbedtls_sha256_free( &sha256 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1961 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1962 | return; |
| 1963 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1964 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1965 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1966 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1967 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 1968 | unsigned char hash[48], |
| 1969 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1970 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1971 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1972 | size_t hash_size; |
| 1973 | psa_status_t status; |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1974 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1975 | |
| 1976 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1977 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1978 | if( status != PSA_SUCCESS ) |
| 1979 | { |
| 1980 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1981 | return; |
| 1982 | } |
| 1983 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1984 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1985 | if( status != PSA_SUCCESS ) |
| 1986 | { |
| 1987 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1988 | return; |
| 1989 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1990 | |
| 1991 | *hlen = 48; |
| 1992 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1993 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1994 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1995 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1996 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1997 | mbedtls_sha512_init( &sha512 ); |
| 1998 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1999 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2000 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2001 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2002 | mbedtls_sha512_finish_ret( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2003 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 2004 | *hlen = 48; |
| 2005 | |
| 2006 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2007 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2008 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2009 | mbedtls_sha512_free( &sha512 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2010 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2011 | return; |
| 2012 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2013 | #endif /* MBEDTLS_SHA512_C */ |
| 2014 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2015 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2016 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 2017 | 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] | 2018 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2019 | unsigned char *p = ssl->handshake->premaster; |
| 2020 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2021 | const unsigned char *psk = ssl->conf->psk; |
| 2022 | size_t psk_len = ssl->conf->psk_len; |
| 2023 | |
| 2024 | /* If the psk callback was called, use its result */ |
| 2025 | if( ssl->handshake->psk != NULL ) |
| 2026 | { |
| 2027 | psk = ssl->handshake->psk; |
| 2028 | psk_len = ssl->handshake->psk_len; |
| 2029 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2030 | |
| 2031 | /* |
| 2032 | * PMS = struct { |
| 2033 | * opaque other_secret<0..2^16-1>; |
| 2034 | * opaque psk<0..2^16-1>; |
| 2035 | * }; |
| 2036 | * with "other_secret" depending on the particular key exchange |
| 2037 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2038 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 2039 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2040 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2041 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2042 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2043 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2044 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 2045 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2046 | |
| 2047 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 2048 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2049 | |
| 2050 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2051 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2052 | } |
| 2053 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2054 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 2055 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 2056 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2057 | { |
| 2058 | /* |
| 2059 | * other_secret already set by the ClientKeyExchange message, |
| 2060 | * and is 48 bytes long |
| 2061 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 2062 | if( end - p < 2 ) |
| 2063 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2064 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2065 | *p++ = 0; |
| 2066 | *p++ = 48; |
| 2067 | p += 48; |
| 2068 | } |
| 2069 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2070 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 2071 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 2072 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2073 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 2074 | int ret; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 2075 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2076 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 2077 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2078 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 2079 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2080 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2081 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2082 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2083 | return( ret ); |
| 2084 | } |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 2085 | *(p++) = (unsigned char)( len >> 8 ); |
| 2086 | *(p++) = (unsigned char)( len ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2087 | p += len; |
| 2088 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2089 | 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] | 2090 | } |
| 2091 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2092 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 2093 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 2094 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2095 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 2096 | int ret; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2097 | size_t zlen; |
| 2098 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2099 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2100 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2101 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2102 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2103 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2104 | return( ret ); |
| 2105 | } |
| 2106 | |
| 2107 | *(p++) = (unsigned char)( zlen >> 8 ); |
| 2108 | *(p++) = (unsigned char)( zlen ); |
| 2109 | p += zlen; |
| 2110 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2111 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 2112 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2113 | } |
| 2114 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2115 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2116 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2117 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2118 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2122 | if( end - p < 2 ) |
| 2123 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 2124 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2125 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 2126 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2127 | |
| 2128 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 2129 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2130 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2131 | memcpy( p, psk, psk_len ); |
| 2132 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2133 | |
| 2134 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 2135 | |
| 2136 | return( 0 ); |
| 2137 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2138 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2139 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2140 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2141 | /* |
| 2142 | * SSLv3.0 MAC functions |
| 2143 | */ |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2144 | #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] | 2145 | static void ssl_mac( mbedtls_md_context_t *md_ctx, |
| 2146 | const unsigned char *secret, |
| 2147 | const unsigned char *buf, size_t len, |
| 2148 | const unsigned char *ctr, int type, |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2149 | unsigned char out[SSL_MAC_MAX_BYTES] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2150 | { |
| 2151 | unsigned char header[11]; |
| 2152 | unsigned char padding[48]; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 2153 | int padlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2154 | int md_size = mbedtls_md_get_size( md_ctx->md_info ); |
| 2155 | int md_type = mbedtls_md_get_type( md_ctx->md_info ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2156 | |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 2157 | /* Only MD5 and SHA-1 supported */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2158 | if( md_type == MBEDTLS_MD_MD5 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2159 | padlen = 48; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 2160 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2161 | padlen = 40; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2162 | |
| 2163 | memcpy( header, ctr, 8 ); |
| 2164 | header[ 8] = (unsigned char) type; |
| 2165 | header[ 9] = (unsigned char)( len >> 8 ); |
| 2166 | header[10] = (unsigned char)( len ); |
| 2167 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2168 | memset( padding, 0x36, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2169 | mbedtls_md_starts( md_ctx ); |
| 2170 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 2171 | mbedtls_md_update( md_ctx, padding, padlen ); |
| 2172 | mbedtls_md_update( md_ctx, header, 11 ); |
| 2173 | mbedtls_md_update( md_ctx, buf, len ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 2174 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2175 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2176 | memset( padding, 0x5C, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2177 | mbedtls_md_starts( md_ctx ); |
| 2178 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 2179 | mbedtls_md_update( md_ctx, padding, padlen ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 2180 | mbedtls_md_update( md_ctx, out, md_size ); |
| 2181 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 2182 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2183 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 2184 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2185 | /* 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] | 2186 | * 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] | 2187 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \ |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2188 | ( defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2189 | defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2190 | defined(MBEDTLS_SSL_PROTO_TLS1_2) ) |
| 2191 | /* This function makes sure every byte in the memory region is accessed |
| 2192 | * (in ascending addresses order) */ |
| 2193 | static void ssl_read_memory( unsigned char *p, size_t len ) |
| 2194 | { |
| 2195 | unsigned char acc = 0; |
| 2196 | volatile unsigned char force; |
| 2197 | |
| 2198 | for( ; len != 0; p++, len-- ) |
| 2199 | acc ^= *p; |
| 2200 | |
| 2201 | force = acc; |
| 2202 | (void) force; |
| 2203 | } |
| 2204 | #endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */ |
| 2205 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2206 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2207 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2208 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2209 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2210 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d3f8c79 | 2019-05-20 15:06:12 +0100 | [diff] [blame] | 2211 | /* This functions transforms a DTLS plaintext fragment and a record content |
| 2212 | * type into an instance of the DTLSInnerPlaintext structure: |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2213 | * |
| 2214 | * struct { |
| 2215 | * opaque content[DTLSPlaintext.length]; |
| 2216 | * ContentType real_type; |
| 2217 | * uint8 zeros[length_of_padding]; |
| 2218 | * } DTLSInnerPlaintext; |
| 2219 | * |
| 2220 | * Input: |
| 2221 | * - `content`: The beginning of the buffer holding the |
| 2222 | * plaintext to be wrapped. |
| 2223 | * - `*content_size`: The length of the plaintext in Bytes. |
| 2224 | * - `max_len`: The number of Bytes available starting from |
| 2225 | * `content`. This must be `>= *content_size`. |
| 2226 | * - `rec_type`: The desired record content type. |
| 2227 | * |
| 2228 | * Output: |
| 2229 | * - `content`: The beginning of the resulting DTLSInnerPlaintext structure. |
| 2230 | * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure. |
| 2231 | * |
| 2232 | * Returns: |
| 2233 | * - `0` on success. |
| 2234 | * - A negative error code if `max_len` didn't offer enough space |
| 2235 | * for the expansion. |
| 2236 | */ |
| 2237 | static int ssl_cid_build_inner_plaintext( unsigned char *content, |
| 2238 | size_t *content_size, |
| 2239 | size_t remaining, |
| 2240 | uint8_t rec_type ) |
| 2241 | { |
| 2242 | size_t len = *content_size; |
Hanno Becker | b9ec44f | 2019-05-13 15:31:17 +0100 | [diff] [blame] | 2243 | size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY - |
| 2244 | ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) % |
| 2245 | MBEDTLS_SSL_CID_PADDING_GRANULARITY; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2246 | |
| 2247 | /* Write real content type */ |
| 2248 | if( remaining == 0 ) |
| 2249 | return( -1 ); |
| 2250 | content[ len ] = rec_type; |
| 2251 | len++; |
| 2252 | remaining--; |
| 2253 | |
| 2254 | if( remaining < pad ) |
| 2255 | return( -1 ); |
| 2256 | memset( content + len, 0, pad ); |
| 2257 | len += pad; |
| 2258 | remaining -= pad; |
| 2259 | |
| 2260 | *content_size = len; |
| 2261 | return( 0 ); |
| 2262 | } |
| 2263 | |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2264 | /* This function parses a DTLSInnerPlaintext structure. |
| 2265 | * See ssl_cid_build_inner_plaintext() for details. */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2266 | static int ssl_cid_parse_inner_plaintext( unsigned char const *content, |
| 2267 | size_t *content_size, |
| 2268 | uint8_t *rec_type ) |
| 2269 | { |
| 2270 | size_t remaining = *content_size; |
| 2271 | |
| 2272 | /* Determine length of padding by skipping zeroes from the back. */ |
| 2273 | do |
| 2274 | { |
| 2275 | if( remaining == 0 ) |
| 2276 | return( -1 ); |
| 2277 | remaining--; |
| 2278 | } while( content[ remaining ] == 0 ); |
| 2279 | |
| 2280 | *content_size = remaining; |
| 2281 | *rec_type = content[ remaining ]; |
| 2282 | |
| 2283 | return( 0 ); |
| 2284 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2285 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2286 | |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2287 | /* `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] | 2288 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2289 | static void ssl_extract_add_data_from_record( unsigned char* add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2290 | size_t *add_data_len, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2291 | mbedtls_record *rec ) |
| 2292 | { |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2293 | /* Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2294 | * |
| 2295 | * additional_data = seq_num + TLSCompressed.type + |
| 2296 | * TLSCompressed.version + TLSCompressed.length; |
| 2297 | * |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2298 | * For the CID extension, this is extended as follows |
| 2299 | * (quoting draft-ietf-tls-dtls-connection-id-05, |
| 2300 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2301 | * |
| 2302 | * additional_data = seq_num + DTLSPlaintext.type + |
| 2303 | * DTLSPlaintext.version + |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2304 | * cid + |
| 2305 | * cid_length + |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2306 | * length_of_DTLSInnerPlaintext; |
| 2307 | */ |
| 2308 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2309 | memcpy( add_data, rec->ctr, sizeof( rec->ctr ) ); |
| 2310 | add_data[8] = rec->type; |
Hanno Becker | edb24f8 | 2019-05-20 15:01:46 +0100 | [diff] [blame] | 2311 | memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) ); |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2312 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2313 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2314 | if( rec->cid_len != 0 ) |
| 2315 | { |
| 2316 | memcpy( add_data + 11, rec->cid, rec->cid_len ); |
| 2317 | add_data[11 + rec->cid_len + 0] = rec->cid_len; |
| 2318 | add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF; |
| 2319 | add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF; |
| 2320 | *add_data_len = 13 + 1 + rec->cid_len; |
| 2321 | } |
| 2322 | else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2323 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2324 | { |
| 2325 | add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF; |
| 2326 | add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF; |
| 2327 | *add_data_len = 13; |
| 2328 | } |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2331 | int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl, |
| 2332 | mbedtls_ssl_transform *transform, |
| 2333 | mbedtls_record *rec, |
| 2334 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2335 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2336 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2337 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2338 | int auth_done = 0; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2339 | unsigned char * data; |
Hanno Becker | 92fb4fa | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2340 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2341 | size_t add_data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2342 | size_t post_avail; |
| 2343 | |
| 2344 | /* The SSL context is only used for debugging purposes! */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2345 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 2346 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2347 | ((void) ssl); |
| 2348 | #endif |
| 2349 | |
| 2350 | /* The PRNG is used for dynamic IV generation that's used |
| 2351 | * for CBC transformations in TLS 1.1 and TLS 1.2. */ |
| 2352 | #if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
| 2353 | ( defined(MBEDTLS_AES_C) || \ |
| 2354 | defined(MBEDTLS_ARIA_C) || \ |
| 2355 | defined(MBEDTLS_CAMELLIA_C) ) && \ |
| 2356 | ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) ) |
| 2357 | ((void) f_rng); |
| 2358 | ((void) p_rng); |
| 2359 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2360 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2361 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2362 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2363 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2364 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2365 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) ); |
| 2366 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2367 | } |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2368 | if( rec == NULL |
| 2369 | || rec->buf == NULL |
| 2370 | || rec->buf_len < rec->data_offset |
| 2371 | || rec->buf_len - rec->data_offset < rec->data_len |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2372 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2373 | || rec->cid_len != 0 |
| 2374 | #endif |
| 2375 | ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2376 | { |
| 2377 | 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] | 2378 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2379 | } |
| 2380 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2381 | data = rec->buf + rec->data_offset; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2382 | 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] | 2383 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2384 | data, rec->data_len ); |
| 2385 | |
| 2386 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ); |
| 2387 | |
| 2388 | if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 2389 | { |
| 2390 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d", |
| 2391 | (unsigned) rec->data_len, |
| 2392 | MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 2393 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2394 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 2395 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2396 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2397 | /* |
| 2398 | * Add CID information |
| 2399 | */ |
| 2400 | rec->cid_len = transform->out_cid_len; |
| 2401 | memcpy( rec->cid, transform->out_cid, transform->out_cid_len ); |
| 2402 | MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len ); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2403 | |
| 2404 | if( rec->cid_len != 0 ) |
| 2405 | { |
| 2406 | /* |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2407 | * Wrap plaintext into DTLSInnerPlaintext structure. |
| 2408 | * See ssl_cid_build_inner_plaintext() for more information. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2409 | * |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2410 | * Note that this changes `rec->data_len`, and hence |
| 2411 | * `post_avail` needs to be recalculated afterwards. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2412 | */ |
| 2413 | if( ssl_cid_build_inner_plaintext( data, |
| 2414 | &rec->data_len, |
| 2415 | post_avail, |
| 2416 | rec->type ) != 0 ) |
| 2417 | { |
| 2418 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2419 | } |
| 2420 | |
| 2421 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 2422 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2423 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2424 | |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2425 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); |
| 2426 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2427 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2428 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2429 | */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2430 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2431 | if( mode == MBEDTLS_MODE_STREAM || |
| 2432 | ( mode == MBEDTLS_MODE_CBC |
| 2433 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2434 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2435 | #endif |
| 2436 | ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2437 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2438 | if( post_avail < transform->maclen ) |
| 2439 | { |
| 2440 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2441 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2442 | } |
| 2443 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2444 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2445 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2446 | { |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2447 | unsigned char mac[SSL_MAC_MAX_BYTES]; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2448 | ssl_mac( &transform->md_ctx_enc, transform->mac_enc, |
| 2449 | data, rec->data_len, rec->ctr, rec->type, mac ); |
| 2450 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2451 | } |
| 2452 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2453 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2454 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2455 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2456 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2457 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2458 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2459 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2460 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2461 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2462 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2463 | add_data_len ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2464 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2465 | data, rec->data_len ); |
| 2466 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2467 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
| 2468 | |
| 2469 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2470 | } |
| 2471 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2472 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2473 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2474 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2475 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2476 | } |
| 2477 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2478 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len, |
| 2479 | transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2480 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2481 | rec->data_len += transform->maclen; |
| 2482 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2483 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2484 | } |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2485 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2486 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2487 | /* |
| 2488 | * Encrypt |
| 2489 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2490 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2491 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2492 | { |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2493 | int ret; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2494 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2495 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2496 | "including %d bytes of padding", |
| 2497 | rec->data_len, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2498 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2499 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2500 | transform->iv_enc, transform->ivlen, |
| 2501 | data, rec->data_len, |
| 2502 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2503 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2504 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2505 | return( ret ); |
| 2506 | } |
| 2507 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2508 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2509 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2510 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2511 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2512 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2513 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2514 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2515 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2516 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2517 | #if defined(MBEDTLS_GCM_C) || \ |
| 2518 | defined(MBEDTLS_CCM_C) || \ |
| 2519 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2520 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2521 | mode == MBEDTLS_MODE_CCM || |
| 2522 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2523 | { |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 2524 | int ret; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2525 | unsigned char iv[12]; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2526 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2527 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2528 | /* Check that there's space for both the authentication tag |
| 2529 | * and the explicit IV before and after the record content. */ |
| 2530 | if( post_avail < transform->taglen || |
| 2531 | rec->data_offset < explicit_iv_len ) |
| 2532 | { |
| 2533 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2534 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2535 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2536 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2537 | /* |
| 2538 | * Generate IV |
| 2539 | */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2540 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2541 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2542 | /* GCM and CCM: fixed || explicit (=seqnum) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2543 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2544 | memcpy( iv + transform->fixed_ivlen, rec->ctr, |
| 2545 | explicit_iv_len ); |
| 2546 | /* Prefix record content with explicit IV. */ |
| 2547 | memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2548 | } |
| 2549 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 2550 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2551 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2552 | unsigned char i; |
| 2553 | |
| 2554 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
| 2555 | |
| 2556 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2557 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2558 | } |
| 2559 | else |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2560 | { |
| 2561 | /* 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] | 2562 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2563 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2564 | } |
| 2565 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2566 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2567 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2568 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)", |
| 2569 | iv, transform->ivlen ); |
| 2570 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2571 | data - explicit_iv_len, explicit_iv_len ); |
| 2572 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2573 | add_data, add_data_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2574 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2575 | "including 0 bytes of padding", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2576 | rec->data_len ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2577 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2578 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2579 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2580 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2581 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2582 | if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2583 | iv, transform->ivlen, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2584 | add_data, add_data_len, /* add data */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2585 | data, rec->data_len, /* source */ |
| 2586 | data, &rec->data_len, /* destination */ |
| 2587 | data + rec->data_len, transform->taglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2588 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2589 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2590 | return( ret ); |
| 2591 | } |
| 2592 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2593 | MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", |
| 2594 | data + rec->data_len, transform->taglen ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2595 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2596 | rec->data_len += transform->taglen + explicit_iv_len; |
| 2597 | rec->data_offset -= explicit_iv_len; |
| 2598 | post_avail -= transform->taglen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2599 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2600 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2601 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2602 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2603 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2604 | ( 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] | 2605 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2606 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2607 | int ret; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2608 | size_t padlen, i; |
| 2609 | size_t olen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2610 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2611 | /* Currently we're always using minimal padding |
| 2612 | * (up to 255 bytes would be allowed). */ |
| 2613 | padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen; |
| 2614 | if( padlen == transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2615 | padlen = 0; |
| 2616 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2617 | /* Check there's enough space in the buffer for the padding. */ |
| 2618 | if( post_avail < padlen + 1 ) |
| 2619 | { |
| 2620 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2621 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2622 | } |
| 2623 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2624 | for( i = 0; i <= padlen; i++ ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2625 | data[rec->data_len + i] = (unsigned char) padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2626 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2627 | rec->data_len += padlen + 1; |
| 2628 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2629 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2630 | #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] | 2631 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2632 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per |
| 2633 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2634 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2635 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2636 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2637 | if( f_rng == NULL ) |
| 2638 | { |
| 2639 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) ); |
| 2640 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2641 | } |
| 2642 | |
| 2643 | if( rec->data_offset < transform->ivlen ) |
| 2644 | { |
| 2645 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2646 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2647 | } |
| 2648 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2649 | /* |
| 2650 | * Generate IV |
| 2651 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2652 | ret = f_rng( p_rng, transform->iv_enc, transform->ivlen ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2653 | if( ret != 0 ) |
| 2654 | return( ret ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2655 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2656 | memcpy( data - transform->ivlen, transform->iv_enc, |
| 2657 | transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2658 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2659 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2660 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2661 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2662 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2663 | "including %d bytes of IV and %d bytes of padding", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2664 | rec->data_len, transform->ivlen, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2665 | padlen + 1 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2666 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2667 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2668 | transform->iv_enc, |
| 2669 | transform->ivlen, |
| 2670 | data, rec->data_len, |
| 2671 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2672 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2673 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2674 | return( ret ); |
| 2675 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2676 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2677 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2678 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2679 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2680 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2681 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2682 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2683 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2684 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2685 | { |
| 2686 | /* |
| 2687 | * Save IV in SSL3 and TLS1 |
| 2688 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2689 | memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv, |
| 2690 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2691 | } |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2692 | else |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2693 | #endif |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2694 | { |
| 2695 | data -= transform->ivlen; |
| 2696 | rec->data_offset -= transform->ivlen; |
| 2697 | rec->data_len += transform->ivlen; |
| 2698 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2699 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2700 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2701 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2702 | { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 2703 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2704 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2705 | /* |
| 2706 | * MAC(MAC_write_key, seq_num + |
| 2707 | * TLSCipherText.type + |
| 2708 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2709 | * length_of( (IV +) ENC(...) ) + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2710 | * IV + // except for TLS 1.0 |
| 2711 | * ENC(content + padding + padding_length)); |
| 2712 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2713 | |
| 2714 | if( post_avail < transform->maclen) |
| 2715 | { |
| 2716 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2717 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2718 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2719 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2720 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2721 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2722 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2723 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2724 | add_data_len ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2725 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2726 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2727 | add_data_len ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2728 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2729 | data, rec->data_len ); |
| 2730 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2731 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2732 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2733 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2734 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2735 | rec->data_len += transform->maclen; |
| 2736 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2737 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2738 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2739 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2740 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2741 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2742 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2743 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2744 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2745 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2746 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2747 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2748 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2749 | /* Make extra sure authentication was performed, exactly once */ |
| 2750 | if( auth_done != 1 ) |
| 2751 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2752 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2753 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2754 | } |
| 2755 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2756 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2757 | |
| 2758 | return( 0 ); |
| 2759 | } |
| 2760 | |
Hanno Becker | 605949f | 2019-07-12 08:23:59 +0100 | [diff] [blame] | 2761 | int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl, |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2762 | mbedtls_ssl_transform *transform, |
| 2763 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2764 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2765 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2766 | mbedtls_cipher_mode_t mode; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2767 | int ret, auth_done = 0; |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2768 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2769 | size_t padlen = 0, correct = 1; |
| 2770 | #endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2771 | unsigned char* data; |
Hanno Becker | 92fb4fa | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2772 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2773 | size_t add_data_len; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2774 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2775 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 2776 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2777 | ((void) ssl); |
| 2778 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2779 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2780 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2781 | if( rec == NULL || |
| 2782 | rec->buf == NULL || |
| 2783 | rec->buf_len < rec->data_offset || |
| 2784 | rec->buf_len - rec->data_offset < rec->data_len ) |
| 2785 | { |
| 2786 | 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] | 2787 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2788 | } |
| 2789 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2790 | data = rec->buf + rec->data_offset; |
| 2791 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2792 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2793 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2794 | /* |
| 2795 | * Match record's CID with incoming CID. |
| 2796 | */ |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2797 | if( rec->cid_len != transform->in_cid_len || |
| 2798 | memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) |
| 2799 | { |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 2800 | return( MBEDTLS_ERR_SSL_UNEXPECTED_CID ); |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2801 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2802 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2803 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2804 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2805 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2806 | { |
| 2807 | padlen = 0; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2808 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2809 | transform->iv_dec, |
| 2810 | transform->ivlen, |
| 2811 | data, rec->data_len, |
| 2812 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2813 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2814 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2815 | return( ret ); |
| 2816 | } |
| 2817 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2818 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2819 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2820 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2821 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2822 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2823 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2824 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2825 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2826 | #if defined(MBEDTLS_GCM_C) || \ |
| 2827 | defined(MBEDTLS_CCM_C) || \ |
| 2828 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2829 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2830 | mode == MBEDTLS_MODE_CCM || |
| 2831 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2832 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2833 | unsigned char iv[12]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2834 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2835 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2836 | /* |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2837 | * Prepare IV from explicit and implicit data. |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2838 | */ |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2839 | |
| 2840 | /* Check that there's enough space for the explicit IV |
| 2841 | * (at the beginning of the record) and the MAC (at the |
| 2842 | * end of the record). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2843 | if( rec->data_len < explicit_iv_len + transform->taglen ) |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2844 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2845 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2846 | "+ taglen (%d)", rec->data_len, |
| 2847 | explicit_iv_len, transform->taglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2848 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2849 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2850 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2851 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2852 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2853 | { |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2854 | /* GCM and CCM: fixed || explicit */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2855 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2856 | /* Fixed */ |
| 2857 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2858 | /* Explicit */ |
| 2859 | memcpy( iv + transform->fixed_ivlen, data, 8 ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2860 | } |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2861 | else |
| 2862 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2863 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 2864 | if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2865 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2866 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2867 | unsigned char i; |
| 2868 | |
| 2869 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2870 | |
| 2871 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2872 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2873 | } |
| 2874 | else |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2875 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2876 | { |
| 2877 | /* Reminder if we ever add an AEAD mode with a different size */ |
| 2878 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2879 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2880 | } |
| 2881 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2882 | /* Group changes to data, data_len, and add_data, because |
| 2883 | * add_data depends on data_len. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2884 | data += explicit_iv_len; |
| 2885 | rec->data_offset += explicit_iv_len; |
| 2886 | rec->data_len -= explicit_iv_len + transform->taglen; |
| 2887 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2888 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2889 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2890 | add_data, add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2891 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2892 | /* Because of the check above, we know that there are |
| 2893 | * explicit_iv_len Bytes preceeding data, and taglen |
| 2894 | * bytes following data + data_len. This justifies |
Hanno Becker | 2001665 | 2019-07-10 11:44:13 +0100 | [diff] [blame] | 2895 | * the debug message and the invocation of |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2896 | * mbedtls_cipher_auth_decrypt() below. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2897 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2898 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2899 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len, |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2900 | transform->taglen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2901 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2902 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2903 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2904 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2905 | if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec, |
| 2906 | iv, transform->ivlen, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2907 | add_data, add_data_len, |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2908 | data, rec->data_len, |
| 2909 | data, &olen, |
| 2910 | data + rec->data_len, |
| 2911 | transform->taglen ) ) != 0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2912 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2913 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2914 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2915 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 2916 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2917 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2918 | return( ret ); |
| 2919 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2920 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2921 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2922 | /* Double-check that AEAD decryption doesn't change content length. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2923 | if( olen != rec->data_len ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2924 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2925 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2926 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2927 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2928 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2929 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2930 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2931 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2932 | ( 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] | 2933 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2934 | { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2935 | size_t minlen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2936 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2937 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2938 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2939 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2940 | #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] | 2941 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 2942 | { |
| 2943 | /* The ciphertext is prefixed with the CBC IV. */ |
| 2944 | minlen += transform->ivlen; |
| 2945 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2946 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2947 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2948 | /* Size considerations: |
| 2949 | * |
| 2950 | * - The CBC cipher text must not be empty and hence |
| 2951 | * at least of size transform->ivlen. |
| 2952 | * |
| 2953 | * Together with the potential IV-prefix, this explains |
| 2954 | * the first of the two checks below. |
| 2955 | * |
| 2956 | * - The record must contain a MAC, either in plain or |
| 2957 | * encrypted, depending on whether Encrypt-then-MAC |
| 2958 | * is used or not. |
| 2959 | * - If it is, the message contains the IV-prefix, |
| 2960 | * the CBC ciphertext, and the MAC. |
| 2961 | * - If it is not, the padded plaintext, and hence |
| 2962 | * the CBC ciphertext, has at least length maclen + 1 |
| 2963 | * because there is at least the padding length byte. |
| 2964 | * |
| 2965 | * As the CBC ciphertext is not empty, both cases give the |
| 2966 | * lower bound minlen + maclen + 1 on the record size, which |
| 2967 | * we test for in the second check below. |
| 2968 | */ |
| 2969 | if( rec->data_len < minlen + transform->ivlen || |
| 2970 | rec->data_len < minlen + transform->maclen + 1 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2971 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2972 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2973 | "+ 1 ) ( + expl IV )", rec->data_len, |
| 2974 | transform->ivlen, |
| 2975 | transform->maclen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2976 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2977 | } |
| 2978 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2979 | /* |
| 2980 | * Authenticate before decrypt if enabled |
| 2981 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2982 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2983 | if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2984 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2985 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2986 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2987 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2988 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2989 | /* Update data_len in tandem with add_data. |
| 2990 | * |
| 2991 | * The subtraction is safe because of the previous check |
| 2992 | * data_len >= minlen + maclen + 1. |
| 2993 | * |
| 2994 | * Afterwards, we know that data + data_len is followed by at |
| 2995 | * least maclen Bytes, which justifies the call to |
| 2996 | * mbedtls_ssl_safer_memcmp() below. |
| 2997 | * |
| 2998 | * Further, we still know that data_len > minlen */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2999 | rec->data_len -= transform->maclen; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3000 | 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] | 3001 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3002 | /* Calculate expected MAC. */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3003 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
| 3004 | add_data_len ); |
| 3005 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 3006 | add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3007 | mbedtls_md_hmac_update( &transform->md_ctx_dec, |
| 3008 | data, rec->data_len ); |
| 3009 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
| 3010 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 3011 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3012 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, |
| 3013 | transform->maclen ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 3014 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3015 | transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3016 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3017 | /* Compare expected MAC with MAC at the end of the record. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3018 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 3019 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3020 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3021 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3022 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3023 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3024 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3025 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3026 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3027 | |
| 3028 | /* |
| 3029 | * Check length sanity |
| 3030 | */ |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3031 | |
| 3032 | /* We know from above that data_len > minlen >= 0, |
| 3033 | * so the following check in particular implies that |
| 3034 | * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3035 | if( rec->data_len % transform->ivlen != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3036 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3037 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3038 | rec->data_len, transform->ivlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3039 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3040 | } |
| 3041 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3042 | #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] | 3043 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3044 | * 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] | 3045 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3046 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3047 | { |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3048 | /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3049 | memcpy( transform->iv_dec, data, transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3050 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3051 | data += transform->ivlen; |
| 3052 | rec->data_offset += transform->ivlen; |
| 3053 | rec->data_len -= transform->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3054 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3055 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3056 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3057 | /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */ |
| 3058 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3059 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 3060 | transform->iv_dec, transform->ivlen, |
| 3061 | data, rec->data_len, data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 3062 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3063 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3064 | return( ret ); |
| 3065 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 3066 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3067 | /* Double-check that length hasn't changed during decryption. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3068 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3069 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3070 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3071 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3072 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 3073 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3074 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3075 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3076 | { |
| 3077 | /* |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3078 | * Save IV in SSL3 and TLS1, where CBC decryption of consecutive |
| 3079 | * records is equivalent to CBC decryption of the concatenation |
| 3080 | * of the records; in other words, IVs are maintained across |
| 3081 | * record decryptions. |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3082 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3083 | memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv, |
| 3084 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3085 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3086 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3087 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3088 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 3089 | * subtracted at most minlen and maclen up to this point, |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3090 | * data_len > 0 (because of data_len % ivlen == 0, it's actually |
| 3091 | * >= ivlen ). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3092 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3093 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3094 | if( auth_done == 1 ) |
| 3095 | { |
| 3096 | correct *= ( rec->data_len >= padlen + 1 ); |
| 3097 | padlen *= ( rec->data_len >= padlen + 1 ); |
| 3098 | } |
| 3099 | else |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3100 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3101 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3102 | if( rec->data_len < transform->maclen + padlen + 1 ) |
| 3103 | { |
| 3104 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", |
| 3105 | rec->data_len, |
| 3106 | transform->maclen, |
| 3107 | padlen + 1 ) ); |
| 3108 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 3109 | #endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3110 | |
| 3111 | correct *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
| 3112 | padlen *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3113 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3114 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3115 | padlen++; |
| 3116 | |
| 3117 | /* Regardless of the validity of the padding, |
| 3118 | * we have data_len >= padlen here. */ |
| 3119 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3120 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3121 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3122 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3123 | if( padlen > transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3124 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3125 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 3126 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3127 | "should be no more than %d", |
| 3128 | padlen, transform->ivlen ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 3129 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3130 | correct = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3131 | } |
| 3132 | } |
| 3133 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3134 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 3135 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3136 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3137 | if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3138 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3139 | /* The padding check involves a series of up to 256 |
| 3140 | * consecutive memory reads at the end of the record |
| 3141 | * plaintext buffer. In order to hide the length and |
| 3142 | * validity of the padding, always perform exactly |
| 3143 | * `min(256,plaintext_len)` reads (but take into account |
| 3144 | * only the last `padlen` bytes for the padding check). */ |
| 3145 | size_t pad_count = 0; |
| 3146 | size_t real_count = 0; |
| 3147 | volatile unsigned char* const check = data; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3148 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3149 | /* Index of first padding byte; it has been ensured above |
| 3150 | * that the subtraction is safe. */ |
| 3151 | size_t const padding_idx = rec->data_len - padlen; |
| 3152 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 3153 | size_t const start_idx = rec->data_len - num_checks; |
| 3154 | size_t idx; |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 3155 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3156 | for( idx = start_idx; idx < rec->data_len; idx++ ) |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 3157 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3158 | real_count |= ( idx >= padding_idx ); |
| 3159 | pad_count += real_count * ( check[idx] == padlen - 1 ); |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 3160 | } |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3161 | correct &= ( pad_count == padlen ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3162 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3163 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 3164 | if( padlen > 0 && correct == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3165 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 3166 | #endif |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3167 | padlen &= correct * 0x1FF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3168 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3169 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3170 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3171 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3172 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3173 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3174 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3175 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3176 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3177 | /* If the padding was found to be invalid, padlen == 0 |
| 3178 | * and the subtraction is safe. If the padding was found valid, |
| 3179 | * padlen hasn't been changed and the previous assertion |
| 3180 | * data_len >= padlen still holds. */ |
| 3181 | rec->data_len -= padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3182 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3183 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3184 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 3185 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3186 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3187 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3188 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3189 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3190 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 3191 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3192 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3193 | data, rec->data_len ); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 3194 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3195 | |
| 3196 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3197 | * Authenticate if not done yet. |
| 3198 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3199 | */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 3200 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3201 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3202 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 3203 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 3204 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3205 | /* If the initial value of padlen was such that |
| 3206 | * data_len < maclen + padlen + 1, then padlen |
| 3207 | * got reset to 1, and the initial check |
| 3208 | * data_len >= minlen + maclen + 1 |
| 3209 | * guarantees that at this point we still |
| 3210 | * have at least data_len >= maclen. |
| 3211 | * |
| 3212 | * If the initial value of padlen was such that |
| 3213 | * data_len >= maclen + padlen + 1, then we have |
| 3214 | * subtracted either padlen + 1 (if the padding was correct) |
| 3215 | * or 0 (if the padding was incorrect) since then, |
| 3216 | * hence data_len >= maclen in any case. |
| 3217 | */ |
| 3218 | rec->data_len -= transform->maclen; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3219 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3220 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3221 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3222 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3223 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3224 | ssl_mac( &transform->md_ctx_dec, |
| 3225 | transform->mac_dec, |
| 3226 | data, rec->data_len, |
| 3227 | rec->ctr, rec->type, |
| 3228 | mac_expect ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3229 | } |
| 3230 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3231 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 3232 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3233 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3234 | if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3235 | { |
| 3236 | /* |
| 3237 | * Process MAC and always update for padlen afterwards to make |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3238 | * total time independent of padlen. |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3239 | * |
| 3240 | * Known timing attacks: |
| 3241 | * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf) |
| 3242 | * |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3243 | * To compensate for different timings for the MAC calculation |
| 3244 | * depending on how much padding was removed (which is determined |
| 3245 | * by padlen), process extra_run more blocks through the hash |
| 3246 | * function. |
| 3247 | * |
| 3248 | * The formula in the paper is |
| 3249 | * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 ) |
| 3250 | * where L1 is the size of the header plus the decrypted message |
| 3251 | * plus CBC padding and L2 is the size of the header plus the |
| 3252 | * decrypted message. This is for an underlying hash function |
| 3253 | * with 64-byte blocks. |
| 3254 | * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values |
| 3255 | * correctly. We round down instead of up, so -56 is the correct |
| 3256 | * value for our calculations instead of -55. |
| 3257 | * |
Gilles Peskine | 1bd9d58 | 2018-06-04 11:58:44 +0200 | [diff] [blame] | 3258 | * Repeat the formula rather than defining a block_size variable. |
| 3259 | * This avoids requiring division by a variable at runtime |
| 3260 | * (which would be marginally less efficient and would require |
| 3261 | * linking an extra division function in some builds). |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3262 | */ |
| 3263 | size_t j, extra_run = 0; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3264 | unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE]; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3265 | |
| 3266 | /* |
| 3267 | * The next two sizes are the minimum and maximum values of |
| 3268 | * in_msglen over all padlen values. |
| 3269 | * |
| 3270 | * They're independent of padlen, since we previously did |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3271 | * data_len -= padlen. |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3272 | * |
| 3273 | * Note that max_len + maclen is never more than the buffer |
| 3274 | * length, as we previously did in_msglen -= maclen too. |
| 3275 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3276 | const size_t max_len = rec->data_len + padlen; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3277 | const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0; |
| 3278 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3279 | memset( tmp, 0, sizeof( tmp ) ); |
| 3280 | |
| 3281 | switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) ) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3282 | { |
Gilles Peskine | d0e55a4 | 2018-06-04 12:03:30 +0200 | [diff] [blame] | 3283 | #if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \ |
| 3284 | defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3285 | case MBEDTLS_MD_MD5: |
| 3286 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3287 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3288 | /* 8 bytes of message size, 64-byte compression blocks */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3289 | extra_run = |
| 3290 | ( add_data_len + rec->data_len + padlen + 8 ) / 64 - |
| 3291 | ( add_data_len + rec->data_len + 8 ) / 64; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3292 | break; |
| 3293 | #endif |
Gilles Peskine | a7fe25d | 2018-06-04 12:01:18 +0200 | [diff] [blame] | 3294 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3295 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3296 | /* 16 bytes of message size, 128-byte compression blocks */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3297 | extra_run = |
| 3298 | ( add_data_len + rec->data_len + padlen + 16 ) / 128 - |
| 3299 | ( add_data_len + rec->data_len + 16 ) / 128; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3300 | break; |
| 3301 | #endif |
| 3302 | default: |
Gilles Peskine | 5c38984 | 2018-06-04 12:02:43 +0200 | [diff] [blame] | 3303 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3304 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3305 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3306 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3307 | extra_run &= correct * 0xFF; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3308 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3309 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 3310 | add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3311 | mbedtls_md_hmac_update( &transform->md_ctx_dec, data, |
| 3312 | rec->data_len ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3313 | /* Make sure we access everything even when padlen > 0. This |
| 3314 | * makes the synchronisation requirements for just-in-time |
| 3315 | * Prime+Probe attacks much tighter and hopefully impractical. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3316 | ssl_read_memory( data + rec->data_len, padlen ); |
| 3317 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3318 | |
| 3319 | /* Call mbedtls_md_process at least once due to cache attacks |
| 3320 | * that observe whether md_process() was called of not */ |
Manuel Pégourié-Gonnard | 47fede0 | 2015-04-29 01:35:48 +0200 | [diff] [blame] | 3321 | for( j = 0; j < extra_run + 1; j++ ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3322 | mbedtls_md_process( &transform->md_ctx_dec, tmp ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3323 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3324 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3325 | |
| 3326 | /* Make sure we access all the memory that could contain the MAC, |
| 3327 | * before we check it in the next code block. This makes the |
| 3328 | * synchronisation requirements for just-in-time Prime+Probe |
| 3329 | * attacks much tighter and hopefully impractical. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3330 | ssl_read_memory( data + min_len, |
| 3331 | max_len - min_len + transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3332 | } |
| 3333 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3334 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3335 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3336 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3337 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3338 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3339 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3340 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3341 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3342 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen ); |
| 3343 | 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] | 3344 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3345 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3346 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 3347 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3348 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3349 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 3350 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3351 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3352 | correct = 0; |
| 3353 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3354 | auth_done++; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3355 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 3356 | |
| 3357 | /* |
| 3358 | * Finally check the correct flag |
| 3359 | */ |
| 3360 | if( correct == 0 ) |
| 3361 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 3362 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3363 | |
| 3364 | /* Make extra sure authentication was performed, exactly once */ |
| 3365 | if( auth_done != 1 ) |
| 3366 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3367 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3368 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3369 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3370 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3371 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3372 | if( rec->cid_len != 0 ) |
| 3373 | { |
| 3374 | ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len, |
| 3375 | &rec->type ); |
| 3376 | if( ret != 0 ) |
| 3377 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3378 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3379 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3380 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3381 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3382 | |
| 3383 | return( 0 ); |
| 3384 | } |
| 3385 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 3386 | #undef MAC_NONE |
| 3387 | #undef MAC_PLAINTEXT |
| 3388 | #undef MAC_CIPHERTEXT |
| 3389 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3390 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3391 | /* |
| 3392 | * Compression/decompression functions |
| 3393 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3394 | static int ssl_compress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3395 | { |
| 3396 | int ret; |
| 3397 | unsigned char *msg_post = ssl->out_msg; |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3398 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3399 | size_t len_pre = ssl->out_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3400 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3401 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3402 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3403 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3404 | if( len_pre == 0 ) |
| 3405 | return( 0 ); |
| 3406 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3407 | memcpy( msg_pre, ssl->out_msg, len_pre ); |
| 3408 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3409 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3410 | ssl->out_msglen ) ); |
| 3411 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3412 | MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3413 | ssl->out_msg, ssl->out_msglen ); |
| 3414 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3415 | ssl->transform_out->ctx_deflate.next_in = msg_pre; |
| 3416 | ssl->transform_out->ctx_deflate.avail_in = len_pre; |
| 3417 | ssl->transform_out->ctx_deflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3418 | 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] | 3419 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3420 | ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3421 | if( ret != Z_OK ) |
| 3422 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3423 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) ); |
| 3424 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3427 | ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN - |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3428 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3429 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3430 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3431 | ssl->out_msglen ) ); |
| 3432 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3433 | MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3434 | ssl->out_msg, ssl->out_msglen ); |
| 3435 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3436 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3437 | |
| 3438 | return( 0 ); |
| 3439 | } |
| 3440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3441 | static int ssl_decompress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3442 | { |
| 3443 | int ret; |
| 3444 | unsigned char *msg_post = ssl->in_msg; |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3445 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3446 | size_t len_pre = ssl->in_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3447 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3449 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3450 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3451 | if( len_pre == 0 ) |
| 3452 | return( 0 ); |
| 3453 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3454 | memcpy( msg_pre, ssl->in_msg, len_pre ); |
| 3455 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3456 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3457 | ssl->in_msglen ) ); |
| 3458 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3459 | MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3460 | ssl->in_msg, ssl->in_msglen ); |
| 3461 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3462 | ssl->transform_in->ctx_inflate.next_in = msg_pre; |
| 3463 | ssl->transform_in->ctx_inflate.avail_in = len_pre; |
| 3464 | ssl->transform_in->ctx_inflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3465 | ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3466 | header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3467 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3468 | ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3469 | if( ret != Z_OK ) |
| 3470 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3471 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) ); |
| 3472 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3475 | ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3476 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3477 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3478 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3479 | ssl->in_msglen ) ); |
| 3480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3481 | MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3482 | ssl->in_msg, ssl->in_msglen ); |
| 3483 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3484 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3485 | |
| 3486 | return( 0 ); |
| 3487 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3488 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3490 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3491 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3492 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3493 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3494 | static int ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3495 | { |
| 3496 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 3497 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3498 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3499 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3500 | 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] | 3501 | unsigned char doublings = 1; |
| 3502 | |
| 3503 | while( ratio != 0 ) |
| 3504 | { |
| 3505 | ++doublings; |
| 3506 | ratio >>= 1; |
| 3507 | } |
| 3508 | |
| 3509 | if( ++ssl->renego_records_seen > doublings ) |
| 3510 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 3511 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3512 | return( 0 ); |
| 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | return( ssl_write_hello_request( ssl ) ); |
| 3517 | } |
| 3518 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3519 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3520 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3521 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3522 | * Fill the input message buffer by appending data to it. |
| 3523 | * 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] | 3524 | * |
| 3525 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 3526 | * available (from this read and/or a previous one). Otherwise, an error code |
| 3527 | * is returned (possibly EOF or WANT_READ). |
| 3528 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3529 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 3530 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 3531 | * since we always read a whole datagram at once. |
| 3532 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3533 | * 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] | 3534 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3535 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3536 | 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] | 3537 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3538 | int ret; |
| 3539 | size_t len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3540 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3541 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3542 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3543 | if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL ) |
| 3544 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3545 | 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] | 3546 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3547 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3548 | } |
| 3549 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3550 | 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] | 3551 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3552 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); |
| 3553 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3554 | } |
| 3555 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3556 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3557 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3558 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3559 | uint32_t timeout; |
| 3560 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3561 | /* Just to be sure */ |
| 3562 | if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) |
| 3563 | { |
| 3564 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3565 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3566 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3567 | } |
| 3568 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3569 | /* |
| 3570 | * The point is, we need to always read a full datagram at once, so we |
| 3571 | * sometimes read more then requested, and handle the additional data. |
| 3572 | * It could be the rest of the current record (while fetching the |
| 3573 | * header) and/or some other records in the same datagram. |
| 3574 | */ |
| 3575 | |
| 3576 | /* |
| 3577 | * Move to the next record in the already read datagram if applicable |
| 3578 | */ |
| 3579 | if( ssl->next_record_offset != 0 ) |
| 3580 | { |
| 3581 | if( ssl->in_left < ssl->next_record_offset ) |
| 3582 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3583 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3584 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | ssl->in_left -= ssl->next_record_offset; |
| 3588 | |
| 3589 | if( ssl->in_left != 0 ) |
| 3590 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3591 | 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] | 3592 | ssl->next_record_offset ) ); |
| 3593 | memmove( ssl->in_hdr, |
| 3594 | ssl->in_hdr + ssl->next_record_offset, |
| 3595 | ssl->in_left ); |
| 3596 | } |
| 3597 | |
| 3598 | ssl->next_record_offset = 0; |
| 3599 | } |
| 3600 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3601 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3602 | ssl->in_left, nb_want ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3603 | |
| 3604 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3605 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3606 | */ |
| 3607 | if( nb_want <= ssl->in_left) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3608 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3609 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3610 | return( 0 ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3611 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3612 | |
| 3613 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 3614 | * 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] | 3615 | * are not at the beginning of a new record, the caller did something |
| 3616 | * wrong. |
| 3617 | */ |
| 3618 | if( ssl->in_left != 0 ) |
| 3619 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3620 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3621 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3622 | } |
| 3623 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3624 | /* |
| 3625 | * Don't even try to read if time's out already. |
| 3626 | * This avoids by-passing the timer when repeatedly receiving messages |
| 3627 | * that will end up being dropped. |
| 3628 | */ |
| 3629 | if( ssl_check_timer( ssl ) != 0 ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3630 | { |
| 3631 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3632 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3633 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3634 | else |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3635 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3636 | 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] | 3637 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3638 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3639 | timeout = ssl->handshake->retransmit_timeout; |
| 3640 | else |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3641 | timeout = ssl->conf->read_timeout; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3642 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3643 | 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] | 3644 | |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3645 | if( ssl->f_recv_timeout != NULL ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3646 | ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, |
| 3647 | timeout ); |
| 3648 | else |
| 3649 | ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len ); |
| 3650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3651 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3652 | |
| 3653 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3654 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3655 | } |
| 3656 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3657 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3658 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3659 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3660 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3661 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3662 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3663 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3664 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) |
| 3665 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3666 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3667 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3668 | } |
| 3669 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3670 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3671 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3672 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3673 | return( ret ); |
| 3674 | } |
| 3675 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3676 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3677 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3678 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3679 | else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3680 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3681 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3682 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3683 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3684 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3685 | return( ret ); |
| 3686 | } |
| 3687 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3688 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3689 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3690 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3691 | } |
| 3692 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3693 | if( ret < 0 ) |
| 3694 | return( ret ); |
| 3695 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3696 | ssl->in_left = ret; |
| 3697 | } |
| 3698 | else |
| 3699 | #endif |
| 3700 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3701 | 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] | 3702 | ssl->in_left, nb_want ) ); |
| 3703 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3704 | while( ssl->in_left < nb_want ) |
| 3705 | { |
| 3706 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3707 | |
| 3708 | if( ssl_check_timer( ssl ) != 0 ) |
| 3709 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
| 3710 | else |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3711 | { |
| 3712 | if( ssl->f_recv_timeout != NULL ) |
| 3713 | { |
| 3714 | ret = ssl->f_recv_timeout( ssl->p_bio, |
| 3715 | ssl->in_hdr + ssl->in_left, len, |
| 3716 | ssl->conf->read_timeout ); |
| 3717 | } |
| 3718 | else |
| 3719 | { |
| 3720 | ret = ssl->f_recv( ssl->p_bio, |
| 3721 | ssl->in_hdr + ssl->in_left, len ); |
| 3722 | } |
| 3723 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3724 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3725 | 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] | 3726 | ssl->in_left, nb_want ) ); |
| 3727 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3728 | |
| 3729 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3730 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3731 | |
| 3732 | if( ret < 0 ) |
| 3733 | return( ret ); |
| 3734 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3735 | if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3736 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3737 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3738 | ( "f_recv returned %d bytes but only %lu were requested", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3739 | ret, (unsigned long)len ) ); |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3740 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3741 | } |
| 3742 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3743 | ssl->in_left += ret; |
| 3744 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3745 | } |
| 3746 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3747 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3748 | |
| 3749 | return( 0 ); |
| 3750 | } |
| 3751 | |
| 3752 | /* |
| 3753 | * Flush any data not yet written |
| 3754 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3755 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3756 | { |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 3757 | int ret; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3758 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3759 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3760 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3761 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3762 | if( ssl->f_send == NULL ) |
| 3763 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3764 | 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] | 3765 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3766 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3767 | } |
| 3768 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3769 | /* Avoid incrementing counter if data is flushed */ |
| 3770 | if( ssl->out_left == 0 ) |
| 3771 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3772 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3773 | return( 0 ); |
| 3774 | } |
| 3775 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3776 | while( ssl->out_left > 0 ) |
| 3777 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3778 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d", |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 3779 | mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3780 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3781 | buf = ssl->out_hdr - ssl->out_left; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3782 | ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left ); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 3783 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3784 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3785 | |
| 3786 | if( ret <= 0 ) |
| 3787 | return( ret ); |
| 3788 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3789 | 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] | 3790 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3791 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3792 | ( "f_send returned %d bytes but only %lu bytes were sent", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3793 | ret, (unsigned long)ssl->out_left ) ); |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 3794 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3795 | } |
| 3796 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3797 | ssl->out_left -= ret; |
| 3798 | } |
| 3799 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3800 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3801 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3802 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3803 | ssl->out_hdr = ssl->out_buf; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3804 | } |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3805 | else |
| 3806 | #endif |
| 3807 | { |
| 3808 | ssl->out_hdr = ssl->out_buf + 8; |
| 3809 | } |
| 3810 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3811 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3812 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3813 | |
| 3814 | return( 0 ); |
| 3815 | } |
| 3816 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3817 | /* |
| 3818 | * Functions to handle the DTLS retransmission state machine |
| 3819 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3820 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3821 | /* |
| 3822 | * Append current handshake message to current outgoing flight |
| 3823 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3824 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3825 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3826 | mbedtls_ssl_flight_item *msg; |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3827 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) ); |
| 3828 | MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight", |
| 3829 | ssl->out_msg, ssl->out_msglen ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3830 | |
| 3831 | /* Allocate space for current message */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3832 | 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] | 3833 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3834 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3835 | sizeof( mbedtls_ssl_flight_item ) ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3836 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3837 | } |
| 3838 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3839 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3840 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3841 | 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] | 3842 | mbedtls_free( msg ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3843 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3844 | } |
| 3845 | |
| 3846 | /* Copy current handshake message with headers */ |
| 3847 | memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); |
| 3848 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3849 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3850 | msg->next = NULL; |
| 3851 | |
| 3852 | /* Append to the current flight */ |
| 3853 | if( ssl->handshake->flight == NULL ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3854 | ssl->handshake->flight = msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3855 | else |
| 3856 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3857 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3858 | while( cur->next != NULL ) |
| 3859 | cur = cur->next; |
| 3860 | cur->next = msg; |
| 3861 | } |
| 3862 | |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3863 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3864 | return( 0 ); |
| 3865 | } |
| 3866 | |
| 3867 | /* |
| 3868 | * Free the current flight of handshake messages |
| 3869 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3870 | static void ssl_flight_free( mbedtls_ssl_flight_item *flight ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3871 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3872 | mbedtls_ssl_flight_item *cur = flight; |
| 3873 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3874 | |
| 3875 | while( cur != NULL ) |
| 3876 | { |
| 3877 | next = cur->next; |
| 3878 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3879 | mbedtls_free( cur->p ); |
| 3880 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3881 | |
| 3882 | cur = next; |
| 3883 | } |
| 3884 | } |
| 3885 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3886 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3887 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3888 | #endif |
| 3889 | |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3890 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3891 | * Swap transform_out and out_ctr with the alternative ones |
| 3892 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3893 | static void ssl_swap_epochs( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3894 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3895 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3896 | unsigned char tmp_out_ctr[8]; |
| 3897 | |
| 3898 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) |
| 3899 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3900 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3901 | return; |
| 3902 | } |
| 3903 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3904 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3905 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3906 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3907 | tmp_transform = ssl->transform_out; |
| 3908 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 3909 | ssl->handshake->alt_transform_out = tmp_transform; |
| 3910 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3911 | /* Swap epoch + sequence_number */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3912 | memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 ); |
| 3913 | 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] | 3914 | memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3915 | |
| 3916 | /* Adjust to the newly activated transform */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 3917 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3918 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3919 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3920 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3921 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3922 | 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] | 3923 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3924 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 3925 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3926 | } |
| 3927 | } |
| 3928 | #endif |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | /* |
| 3932 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3933 | */ |
| 3934 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) |
| 3935 | { |
| 3936 | int ret = 0; |
| 3937 | |
| 3938 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); |
| 3939 | |
| 3940 | ret = mbedtls_ssl_flight_transmit( ssl ); |
| 3941 | |
| 3942 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); |
| 3943 | |
| 3944 | return( ret ); |
| 3945 | } |
| 3946 | |
| 3947 | /* |
| 3948 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3949 | * |
| 3950 | * Need to remember the current message in case flush_output returns |
| 3951 | * 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] | 3952 | * 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] | 3953 | */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3954 | int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3955 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3956 | int ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3957 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3958 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3959 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3960 | { |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3961 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3962 | |
| 3963 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3964 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3965 | ssl_swap_epochs( ssl ); |
| 3966 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3967 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3968 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3969 | |
| 3970 | while( ssl->handshake->cur_msg != NULL ) |
| 3971 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3972 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3973 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3974 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3975 | int const is_finished = |
| 3976 | ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3977 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ); |
| 3978 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3979 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
| 3980 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
| 3981 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3982 | /* Swap epochs before sending Finished: we can't do it after |
| 3983 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 3984 | * Must be done before copying, may change out_msg pointer */ |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3985 | 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] | 3986 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3987 | 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] | 3988 | ssl_swap_epochs( ssl ); |
| 3989 | } |
| 3990 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3991 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 3992 | if( ret < 0 ) |
| 3993 | return( ret ); |
| 3994 | max_frag_len = (size_t) ret; |
| 3995 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3996 | /* CCS is copied as is, while HS messages may need fragmentation */ |
| 3997 | if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3998 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3999 | if( max_frag_len == 0 ) |
| 4000 | { |
| 4001 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 4002 | return( ret ); |
| 4003 | |
| 4004 | continue; |
| 4005 | } |
| 4006 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4007 | memcpy( ssl->out_msg, cur->p, cur->len ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4008 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4009 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4010 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4011 | /* Update position inside current message */ |
| 4012 | ssl->handshake->cur_msg_p += cur->len; |
| 4013 | } |
| 4014 | else |
| 4015 | { |
| 4016 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 4017 | const size_t hs_len = cur->len - 12; |
| 4018 | const size_t frag_off = p - ( cur->p + 12 ); |
| 4019 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4020 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4021 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 4022 | 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] | 4023 | { |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 4024 | if( is_finished ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4025 | ssl_swap_epochs( ssl ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4026 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4027 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 4028 | return( ret ); |
| 4029 | |
| 4030 | continue; |
| 4031 | } |
| 4032 | max_hs_frag_len = max_frag_len - 12; |
| 4033 | |
| 4034 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
| 4035 | max_hs_frag_len : rem_len; |
| 4036 | |
| 4037 | if( frag_off == 0 && cur_hs_frag_len != hs_len ) |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 4038 | { |
| 4039 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)", |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4040 | (unsigned) cur_hs_frag_len, |
| 4041 | (unsigned) max_hs_frag_len ) ); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 4042 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 4043 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4044 | /* Messages are stored with handshake headers as if not fragmented, |
| 4045 | * copy beginning of headers then fill fragmentation fields. |
| 4046 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
| 4047 | memcpy( ssl->out_msg, cur->p, 6 ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4048 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4049 | ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); |
| 4050 | ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff ); |
| 4051 | ssl->out_msg[8] = ( ( frag_off ) & 0xff ); |
| 4052 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4053 | ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); |
| 4054 | ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff ); |
| 4055 | ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff ); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4056 | |
| 4057 | MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); |
| 4058 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 4059 | /* Copy the handshake message content and set records fields */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4060 | memcpy( ssl->out_msg + 12, p, cur_hs_frag_len ); |
| 4061 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4062 | ssl->out_msgtype = cur->type; |
| 4063 | |
| 4064 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4065 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | /* If done with the current message move to the next one if any */ |
| 4069 | if( ssl->handshake->cur_msg_p >= cur->p + cur->len ) |
| 4070 | { |
| 4071 | if( cur->next != NULL ) |
| 4072 | { |
| 4073 | ssl->handshake->cur_msg = cur->next; |
| 4074 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
| 4075 | } |
| 4076 | else |
| 4077 | { |
| 4078 | ssl->handshake->cur_msg = NULL; |
| 4079 | ssl->handshake->cur_msg_p = NULL; |
| 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | /* Actually send the message out */ |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 4084 | if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4085 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4086 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4087 | return( ret ); |
| 4088 | } |
| 4089 | } |
| 4090 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4091 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 4092 | return( ret ); |
| 4093 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4094 | /* Update state and set timer */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4095 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 4096 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 4097 | else |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 4098 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4099 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 4100 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
| 4101 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4102 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4103 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4104 | |
| 4105 | return( 0 ); |
| 4106 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4107 | |
| 4108 | /* |
| 4109 | * To be called when the last message of an incoming flight is received. |
| 4110 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4111 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4112 | { |
| 4113 | /* We won't need to resend that one any more */ |
| 4114 | ssl_flight_free( ssl->handshake->flight ); |
| 4115 | ssl->handshake->flight = NULL; |
| 4116 | ssl->handshake->cur_msg = NULL; |
| 4117 | |
| 4118 | /* The next incoming flight will start with this msg_seq */ |
| 4119 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 4120 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4121 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 4122 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4123 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4124 | /* Clear future message buffering structure. */ |
| 4125 | ssl_buffering_free( ssl ); |
| 4126 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 4127 | /* Cancel timer */ |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4128 | ssl_set_timer( ssl, 0 ); |
| 4129 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4130 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4131 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4132 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4133 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4134 | } |
| 4135 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4136 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4137 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4138 | |
| 4139 | /* |
| 4140 | * To be called when the last message of an outgoing flight is send. |
| 4141 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4142 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4143 | { |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 4144 | ssl_reset_retransmit_timeout( ssl ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 4145 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4147 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4148 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4149 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4150 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4151 | } |
| 4152 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4153 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4154 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4155 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4156 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4157 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4158 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4159 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4160 | |
| 4161 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4162 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4163 | * |
| 4164 | * - fill in handshake headers |
| 4165 | * - update handshake checksum |
| 4166 | * - DTLS: save message for resending |
| 4167 | * - then pass to the record layer |
| 4168 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4169 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 4170 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4171 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4172 | * Inputs: |
| 4173 | * - ssl->out_msglen: 4 + actual handshake message len |
| 4174 | * (4 is the size of handshake headers for TLS) |
| 4175 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 4176 | * - ssl->out_msg + 4: the handshake message body |
| 4177 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 4178 | * 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] | 4179 | * - ssl->out_msglen: the length of the record contents |
| 4180 | * (including handshake headers but excluding record headers) |
| 4181 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4182 | */ |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4183 | int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4184 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4185 | int ret; |
| 4186 | const size_t hs_len = ssl->out_msglen - 4; |
| 4187 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4188 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4189 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) ); |
| 4190 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4191 | /* |
| 4192 | * Sanity checks |
| 4193 | */ |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4194 | if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4195 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 4196 | { |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4197 | /* In SSLv3, the client might send a NoCertificate alert. */ |
| 4198 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
| 4199 | if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 4200 | ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 4201 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ) |
| 4202 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 4203 | { |
| 4204 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4205 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4206 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4207 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4208 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 4209 | /* Whenever we send anything different from a |
| 4210 | * HelloRequest we should be in a handshake - double check. */ |
| 4211 | if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4212 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) && |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4213 | ssl->handshake == NULL ) |
| 4214 | { |
| 4215 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4216 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4217 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4218 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4219 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4220 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4221 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4222 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4223 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4224 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4225 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4226 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4227 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4228 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 4229 | /* Double-check that we did not exceed the bounds |
| 4230 | * of the outgoing record buffer. |
| 4231 | * This should never fail as the various message |
| 4232 | * writing functions must obey the bounds of the |
| 4233 | * outgoing record buffer, but better be safe. |
| 4234 | * |
| 4235 | * Note: We deliberately do not check for the MTU or MFL here. |
| 4236 | */ |
| 4237 | if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 4238 | { |
| 4239 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: " |
| 4240 | "size %u, maximum %u", |
| 4241 | (unsigned) ssl->out_msglen, |
| 4242 | (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 4243 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4244 | } |
| 4245 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4246 | /* |
| 4247 | * Fill handshake headers |
| 4248 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4249 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4250 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4251 | ssl->out_msg[1] = (unsigned char)( hs_len >> 16 ); |
| 4252 | ssl->out_msg[2] = (unsigned char)( hs_len >> 8 ); |
| 4253 | ssl->out_msg[3] = (unsigned char)( hs_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4254 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4255 | /* |
| 4256 | * DTLS has additional fields in the Handshake layer, |
| 4257 | * between the length field and the actual payload: |
| 4258 | * uint16 message_seq; |
| 4259 | * uint24 fragment_offset; |
| 4260 | * uint24 fragment_length; |
| 4261 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4262 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4263 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4264 | { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4265 | /* Make room for the additional DTLS fields */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4266 | if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 ) |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4267 | { |
| 4268 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: " |
| 4269 | "size %u, maximum %u", |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4270 | (unsigned) ( hs_len ), |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4271 | (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) ); |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4272 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4273 | } |
| 4274 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4275 | 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] | 4276 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4277 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4278 | /* Write message_seq and update it, except for HelloRequest */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4279 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4280 | { |
Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 4281 | ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; |
| 4282 | ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF; |
| 4283 | ++( ssl->handshake->out_msg_seq ); |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4284 | } |
| 4285 | else |
| 4286 | { |
| 4287 | ssl->out_msg[4] = 0; |
| 4288 | ssl->out_msg[5] = 0; |
| 4289 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4290 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4291 | /* Handshake hashes are computed without fragmentation, |
| 4292 | * 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] | 4293 | memset( ssl->out_msg + 6, 0x00, 3 ); |
| 4294 | memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4295 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4296 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4297 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 4298 | /* Update running hashes of handshake messages seen */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4299 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
| 4300 | ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4303 | /* 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] | 4304 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4305 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 4306 | ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4307 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4308 | { |
| 4309 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) |
| 4310 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4311 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4312 | return( ret ); |
| 4313 | } |
| 4314 | } |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4315 | else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4316 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4317 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4318 | 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] | 4319 | { |
| 4320 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 4321 | return( ret ); |
| 4322 | } |
| 4323 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4324 | |
| 4325 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) ); |
| 4326 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4327 | return( 0 ); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4328 | } |
| 4329 | |
| 4330 | /* |
| 4331 | * Record layer functions |
| 4332 | */ |
| 4333 | |
| 4334 | /* |
| 4335 | * Write current record. |
| 4336 | * |
| 4337 | * Uses: |
| 4338 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 4339 | * - ssl->out_msglen: length of the record content (excl headers) |
| 4340 | * - ssl->out_msg: record content |
| 4341 | */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4342 | 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] | 4343 | { |
| 4344 | int ret, done = 0; |
| 4345 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4346 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4347 | |
| 4348 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4349 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4350 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4351 | if( ssl->transform_out != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4352 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4353 | { |
| 4354 | if( ( ret = ssl_compress_buf( ssl ) ) != 0 ) |
| 4355 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4356 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4357 | return( ret ); |
| 4358 | } |
| 4359 | |
| 4360 | len = ssl->out_msglen; |
| 4361 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4362 | #endif /*MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4364 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 4365 | if( mbedtls_ssl_hw_record_write != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4366 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4367 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4368 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4369 | ret = mbedtls_ssl_hw_record_write( ssl ); |
| 4370 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4371 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4372 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret ); |
| 4373 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4374 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 4375 | |
| 4376 | if( ret == 0 ) |
| 4377 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4378 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4379 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4380 | if( !done ) |
| 4381 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4382 | unsigned i; |
| 4383 | size_t protected_record_size; |
| 4384 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4385 | /* Skip writing the record content type to after the encryption, |
| 4386 | * as it may change when using the CID extension. */ |
| 4387 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4388 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4389 | ssl->conf->transport, ssl->out_hdr + 1 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4390 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 4391 | memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4392 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 4393 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4394 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4395 | if( ssl->transform_out != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4396 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4397 | mbedtls_record rec; |
| 4398 | |
| 4399 | rec.buf = ssl->out_iv; |
| 4400 | rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - |
| 4401 | ( ssl->out_iv - ssl->out_buf ); |
| 4402 | rec.data_len = ssl->out_msglen; |
| 4403 | rec.data_offset = ssl->out_msg - rec.buf; |
| 4404 | |
| 4405 | memcpy( &rec.ctr[0], ssl->out_ctr, 8 ); |
| 4406 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
| 4407 | ssl->conf->transport, rec.ver ); |
| 4408 | rec.type = ssl->out_msgtype; |
| 4409 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4410 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 4411 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4412 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4413 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4414 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 4415 | if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4416 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4417 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4418 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4419 | return( ret ); |
| 4420 | } |
| 4421 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4422 | if( rec.data_offset != 0 ) |
| 4423 | { |
| 4424 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4425 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4426 | } |
| 4427 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4428 | /* Update the record content type and CID. */ |
| 4429 | ssl->out_msgtype = rec.type; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4430 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4431 | memcpy( ssl->out_cid, rec.cid, rec.cid_len ); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4432 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 78f839d | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 4433 | ssl->out_msglen = len = rec.data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4434 | ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 4435 | ssl->out_len[1] = (unsigned char)( rec.data_len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 4438 | protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4439 | |
| 4440 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4441 | /* In case of DTLS, double-check that we don't exceed |
| 4442 | * the remaining space in the datagram. */ |
| 4443 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4444 | { |
Hanno Becker | 554b0af | 2018-08-22 20:33:41 +0100 | [diff] [blame] | 4445 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4446 | if( ret < 0 ) |
| 4447 | return( ret ); |
| 4448 | |
| 4449 | if( protected_record_size > (size_t) ret ) |
| 4450 | { |
| 4451 | /* Should never happen */ |
| 4452 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4453 | } |
| 4454 | } |
| 4455 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4456 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4457 | /* Now write the potentially updated record content type. */ |
| 4458 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
| 4459 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4460 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, " |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4461 | "version = [%d:%d], msglen = %d", |
| 4462 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 4463 | ssl->out_hdr[2], len ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4464 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4465 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4466 | ssl->out_hdr, protected_record_size ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4467 | |
| 4468 | ssl->out_left += protected_record_size; |
| 4469 | ssl->out_hdr += protected_record_size; |
| 4470 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
| 4471 | |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 4472 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 4473 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 4474 | break; |
| 4475 | |
| 4476 | /* The loop goes to its end iff the counter is wrapping */ |
| 4477 | if( i == ssl_ep_len( ssl ) ) |
| 4478 | { |
| 4479 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); |
| 4480 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 4481 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4484 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 47db877 | 2018-08-21 13:32:13 +0100 | [diff] [blame] | 4485 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4486 | flush == SSL_DONT_FORCE_FLUSH ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4487 | { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 4488 | size_t remaining; |
| 4489 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 4490 | if( ret < 0 ) |
| 4491 | { |
| 4492 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram", |
| 4493 | ret ); |
| 4494 | return( ret ); |
| 4495 | } |
| 4496 | |
| 4497 | remaining = (size_t) ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4498 | if( remaining == 0 ) |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4499 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4500 | flush = SSL_FORCE_FLUSH; |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4501 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4502 | else |
| 4503 | { |
Hanno Becker | 513815a | 2018-08-20 11:56:09 +0100 | [diff] [blame] | 4504 | 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] | 4505 | } |
| 4506 | } |
| 4507 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4508 | |
| 4509 | if( ( flush == SSL_FORCE_FLUSH ) && |
| 4510 | ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4511 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4512 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4513 | return( ret ); |
| 4514 | } |
| 4515 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4516 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4517 | |
| 4518 | return( 0 ); |
| 4519 | } |
| 4520 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4521 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4522 | |
| 4523 | static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl ) |
| 4524 | { |
| 4525 | if( ssl->in_msglen < ssl->in_hslen || |
| 4526 | memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 || |
| 4527 | memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ) |
| 4528 | { |
| 4529 | return( 1 ); |
| 4530 | } |
| 4531 | return( 0 ); |
| 4532 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4533 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4534 | 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] | 4535 | { |
| 4536 | return( ( ssl->in_msg[9] << 16 ) | |
| 4537 | ( ssl->in_msg[10] << 8 ) | |
| 4538 | ssl->in_msg[11] ); |
| 4539 | } |
| 4540 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4541 | 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] | 4542 | { |
| 4543 | return( ( ssl->in_msg[6] << 16 ) | |
| 4544 | ( ssl->in_msg[7] << 8 ) | |
| 4545 | ssl->in_msg[8] ); |
| 4546 | } |
| 4547 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4548 | static int ssl_check_hs_header( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4549 | { |
| 4550 | uint32_t msg_len, frag_off, frag_len; |
| 4551 | |
| 4552 | msg_len = ssl_get_hs_total_len( ssl ); |
| 4553 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 4554 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 4555 | |
| 4556 | if( frag_off > msg_len ) |
| 4557 | return( -1 ); |
| 4558 | |
| 4559 | if( frag_len > msg_len - frag_off ) |
| 4560 | return( -1 ); |
| 4561 | |
| 4562 | if( frag_len + 12 > ssl->in_msglen ) |
| 4563 | return( -1 ); |
| 4564 | |
| 4565 | return( 0 ); |
| 4566 | } |
| 4567 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4568 | /* |
| 4569 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 4570 | */ |
| 4571 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) |
| 4572 | { |
| 4573 | unsigned int start_bits, end_bits; |
| 4574 | |
| 4575 | start_bits = 8 - ( offset % 8 ); |
| 4576 | if( start_bits != 8 ) |
| 4577 | { |
| 4578 | size_t first_byte_idx = offset / 8; |
| 4579 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 4580 | /* Special case */ |
| 4581 | if( len <= start_bits ) |
| 4582 | { |
| 4583 | for( ; len != 0; len-- ) |
| 4584 | mask[first_byte_idx] |= 1 << ( start_bits - len ); |
| 4585 | |
| 4586 | /* Avoid potential issues with offset or len becoming invalid */ |
| 4587 | return; |
| 4588 | } |
| 4589 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4590 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 4591 | len -= start_bits; |
| 4592 | |
| 4593 | for( ; start_bits != 0; start_bits-- ) |
| 4594 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); |
| 4595 | } |
| 4596 | |
| 4597 | end_bits = len % 8; |
| 4598 | if( end_bits != 0 ) |
| 4599 | { |
| 4600 | size_t last_byte_idx = ( offset + len ) / 8; |
| 4601 | |
| 4602 | len -= end_bits; /* Now len % 8 == 0 */ |
| 4603 | |
| 4604 | for( ; end_bits != 0; end_bits-- ) |
| 4605 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); |
| 4606 | } |
| 4607 | |
| 4608 | memset( mask + offset / 8, 0xFF, len / 8 ); |
| 4609 | } |
| 4610 | |
| 4611 | /* |
| 4612 | * Check that bitmask is full |
| 4613 | */ |
| 4614 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) |
| 4615 | { |
| 4616 | size_t i; |
| 4617 | |
| 4618 | for( i = 0; i < len / 8; i++ ) |
| 4619 | if( mask[i] != 0xFF ) |
| 4620 | return( -1 ); |
| 4621 | |
| 4622 | for( i = 0; i < len % 8; i++ ) |
| 4623 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) |
| 4624 | return( -1 ); |
| 4625 | |
| 4626 | return( 0 ); |
| 4627 | } |
| 4628 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4629 | /* msg_len does not include the handshake header */ |
Hanno Becker | 65dc885 | 2018-08-23 09:40:49 +0100 | [diff] [blame] | 4630 | static size_t ssl_get_reassembly_buffer_size( size_t msg_len, |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4631 | unsigned add_bitmap ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4632 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4633 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4634 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4635 | alloc_len = 12; /* Handshake header */ |
| 4636 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4637 | |
Hanno Becker | d07df86 | 2018-08-16 09:14:58 +0100 | [diff] [blame] | 4638 | if( add_bitmap ) |
| 4639 | alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4640 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4641 | return( alloc_len ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4642 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4643 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4644 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4645 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4646 | 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] | 4647 | { |
| 4648 | return( ( ssl->in_msg[1] << 16 ) | |
| 4649 | ( ssl->in_msg[2] << 8 ) | |
| 4650 | ssl->in_msg[3] ); |
| 4651 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4652 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4653 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4654 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4655 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4656 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4657 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d", |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4658 | ssl->in_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4659 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4660 | } |
| 4661 | |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 4662 | 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] | 4663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4664 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4665 | " %d, type = %d, hslen = %d", |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4666 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4667 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4668 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4669 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4670 | { |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4671 | int ret; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4672 | 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] | 4673 | |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4674 | if( ssl_check_hs_header( ssl ) != 0 ) |
| 4675 | { |
| 4676 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) ); |
| 4677 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4678 | } |
| 4679 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4680 | if( ssl->handshake != NULL && |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4681 | ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4682 | recv_msg_seq != ssl->handshake->in_msg_seq ) || |
| 4683 | ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4684 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4685 | { |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 4686 | if( recv_msg_seq > ssl->handshake->in_msg_seq ) |
| 4687 | { |
| 4688 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)", |
| 4689 | recv_msg_seq, |
| 4690 | ssl->handshake->in_msg_seq ) ); |
| 4691 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4692 | } |
| 4693 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 4694 | /* Retransmit only on last message from previous flight, to avoid |
| 4695 | * too many retransmissions. |
| 4696 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
| 4697 | 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] | 4698 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4699 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4700 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4701 | "message_seq = %d, start_of_flight = %d", |
| 4702 | recv_msg_seq, |
| 4703 | ssl->handshake->in_flight_start_seq ) ); |
| 4704 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4705 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4706 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4707 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4708 | return( ret ); |
| 4709 | } |
| 4710 | } |
| 4711 | else |
| 4712 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4713 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4714 | "message_seq = %d, expected = %d", |
| 4715 | recv_msg_seq, |
| 4716 | ssl->handshake->in_msg_seq ) ); |
| 4717 | } |
| 4718 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4719 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4720 | } |
| 4721 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4722 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4723 | /* Message reassembly is handled alongside buffering of future |
| 4724 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 4725 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4726 | * handshake logic layer. */ |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4727 | if( ssl_hs_is_proper_fragment( ssl ) == 1 ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4728 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4729 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4730 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4731 | } |
| 4732 | } |
| 4733 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4734 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4735 | /* With TLS we don't handle fragmentation (for now) */ |
| 4736 | if( ssl->in_msglen < ssl->in_hslen ) |
| 4737 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4738 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); |
| 4739 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4740 | } |
| 4741 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4742 | return( 0 ); |
| 4743 | } |
| 4744 | |
| 4745 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ) |
| 4746 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4747 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4748 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4749 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL ) |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 4750 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4751 | 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] | 4752 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4753 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4754 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4755 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4756 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4757 | ssl->handshake != NULL ) |
| 4758 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4759 | unsigned offset; |
| 4760 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4761 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4762 | /* Increment handshake sequence number */ |
| 4763 | hs->in_msg_seq++; |
| 4764 | |
| 4765 | /* |
| 4766 | * Clear up handshake buffering and reassembly structure. |
| 4767 | */ |
| 4768 | |
| 4769 | /* Free first entry */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4770 | ssl_buffering_free_slot( ssl, 0 ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4771 | |
| 4772 | /* Shift all other entries */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4773 | for( offset = 0, hs_buf = &hs->buffering.hs[0]; |
| 4774 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4775 | offset++, hs_buf++ ) |
| 4776 | { |
| 4777 | *hs_buf = *(hs_buf + 1); |
| 4778 | } |
| 4779 | |
| 4780 | /* Create a fresh last entry */ |
| 4781 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4782 | } |
| 4783 | #endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4784 | } |
| 4785 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4786 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4787 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 4788 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4789 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 4790 | * Bit n is set iff record number in_window_top - n has been seen. |
| 4791 | * |
| 4792 | * Usually, in_window_top is the last record number seen and the lsb of |
| 4793 | * in_window is set. The only exception is the initial state (record number 0 |
| 4794 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4795 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4796 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4797 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4798 | { |
| 4799 | ssl->in_window_top = 0; |
| 4800 | ssl->in_window = 0; |
| 4801 | } |
| 4802 | |
| 4803 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) |
| 4804 | { |
| 4805 | return( ( (uint64_t) buf[0] << 40 ) | |
| 4806 | ( (uint64_t) buf[1] << 32 ) | |
| 4807 | ( (uint64_t) buf[2] << 24 ) | |
| 4808 | ( (uint64_t) buf[3] << 16 ) | |
| 4809 | ( (uint64_t) buf[4] << 8 ) | |
| 4810 | ( (uint64_t) buf[5] ) ); |
| 4811 | } |
| 4812 | |
| 4813 | /* |
| 4814 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 4815 | */ |
Hanno Becker | 0183d69 | 2019-07-12 08:50:37 +0100 | [diff] [blame] | 4816 | 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] | 4817 | { |
| 4818 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4819 | uint64_t bit; |
| 4820 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4821 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4822 | return( 0 ); |
| 4823 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4824 | if( rec_seqnum > ssl->in_window_top ) |
| 4825 | return( 0 ); |
| 4826 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4827 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4828 | |
| 4829 | if( bit >= 64 ) |
| 4830 | return( -1 ); |
| 4831 | |
| 4832 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) |
| 4833 | return( -1 ); |
| 4834 | |
| 4835 | return( 0 ); |
| 4836 | } |
| 4837 | |
| 4838 | /* |
| 4839 | * Update replay window on new validated record |
| 4840 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4841 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4842 | { |
| 4843 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4844 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4845 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4846 | return; |
| 4847 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4848 | if( rec_seqnum > ssl->in_window_top ) |
| 4849 | { |
| 4850 | /* Update window_top and the contents of the window */ |
| 4851 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 4852 | |
| 4853 | if( shift >= 64 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4854 | ssl->in_window = 1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4855 | else |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4856 | { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4857 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4858 | ssl->in_window |= 1; |
| 4859 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4860 | |
| 4861 | ssl->in_window_top = rec_seqnum; |
| 4862 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4863 | else |
| 4864 | { |
| 4865 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4866 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4867 | |
| 4868 | if( bit < 64 ) /* Always true, but be extra sure */ |
| 4869 | ssl->in_window |= (uint64_t) 1 << bit; |
| 4870 | } |
| 4871 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4872 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4873 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4874 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4875 | /* Forward declaration */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4876 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ); |
| 4877 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4878 | /* |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4879 | * Without any SSL context, check if a datagram looks like a ClientHello with |
| 4880 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 4881 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4882 | * |
| 4883 | * - if cookie is valid, return 0 |
| 4884 | * - if ClientHello looks superficially valid but cookie is not, |
| 4885 | * fill obuf and set olen, then |
| 4886 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 4887 | * - otherwise return a specific error code |
| 4888 | */ |
| 4889 | static int ssl_check_dtls_clihlo_cookie( |
| 4890 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 4891 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
| 4892 | void *p_cookie, |
| 4893 | const unsigned char *cli_id, size_t cli_id_len, |
| 4894 | const unsigned char *in, size_t in_len, |
| 4895 | unsigned char *obuf, size_t buf_len, size_t *olen ) |
| 4896 | { |
| 4897 | size_t sid_len, cookie_len; |
| 4898 | unsigned char *p; |
| 4899 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4900 | /* |
| 4901 | * Structure of ClientHello with record and handshake headers, |
| 4902 | * and expected values. We don't need to check a lot, more checks will be |
| 4903 | * done when actually parsing the ClientHello - skipping those checks |
| 4904 | * avoids code duplication and does not make cookie forging any easier. |
| 4905 | * |
| 4906 | * 0-0 ContentType type; copied, must be handshake |
| 4907 | * 1-2 ProtocolVersion version; copied |
| 4908 | * 3-4 uint16 epoch; copied, must be 0 |
| 4909 | * 5-10 uint48 sequence_number; copied |
| 4910 | * 11-12 uint16 length; (ignored) |
| 4911 | * |
| 4912 | * 13-13 HandshakeType msg_type; (ignored) |
| 4913 | * 14-16 uint24 length; (ignored) |
| 4914 | * 17-18 uint16 message_seq; copied |
| 4915 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 4916 | * 22-24 uint24 fragment_length; (ignored) |
| 4917 | * |
| 4918 | * 25-26 ProtocolVersion client_version; (ignored) |
| 4919 | * 27-58 Random random; (ignored) |
| 4920 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 4921 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 4922 | * ... |
| 4923 | * |
| 4924 | * Minimum length is 61 bytes. |
| 4925 | */ |
| 4926 | if( in_len < 61 || |
| 4927 | in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4928 | in[3] != 0 || in[4] != 0 || |
| 4929 | in[19] != 0 || in[20] != 0 || in[21] != 0 ) |
| 4930 | { |
| 4931 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4932 | } |
| 4933 | |
| 4934 | sid_len = in[59]; |
| 4935 | if( sid_len > in_len - 61 ) |
| 4936 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4937 | |
| 4938 | cookie_len = in[60 + sid_len]; |
| 4939 | if( cookie_len > in_len - 60 ) |
| 4940 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4941 | |
| 4942 | if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len, |
| 4943 | cli_id, cli_id_len ) == 0 ) |
| 4944 | { |
| 4945 | /* Valid cookie */ |
| 4946 | return( 0 ); |
| 4947 | } |
| 4948 | |
| 4949 | /* |
| 4950 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 4951 | * |
| 4952 | * 0-0 ContentType type; copied |
| 4953 | * 1-2 ProtocolVersion version; copied |
| 4954 | * 3-4 uint16 epoch; copied |
| 4955 | * 5-10 uint48 sequence_number; copied |
| 4956 | * 11-12 uint16 length; olen - 13 |
| 4957 | * |
| 4958 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 4959 | * 14-16 uint24 length; olen - 25 |
| 4960 | * 17-18 uint16 message_seq; copied |
| 4961 | * 19-21 uint24 fragment_offset; copied |
| 4962 | * 22-24 uint24 fragment_length; olen - 25 |
| 4963 | * |
| 4964 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 4965 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 4966 | * |
| 4967 | * Minimum length is 28. |
| 4968 | */ |
| 4969 | if( buf_len < 28 ) |
| 4970 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4971 | |
| 4972 | /* Copy most fields and adapt others */ |
| 4973 | memcpy( obuf, in, 25 ); |
| 4974 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 4975 | obuf[25] = 0xfe; |
| 4976 | obuf[26] = 0xff; |
| 4977 | |
| 4978 | /* Generate and write actual cookie */ |
| 4979 | p = obuf + 28; |
| 4980 | if( f_cookie_write( p_cookie, |
| 4981 | &p, obuf + buf_len, cli_id, cli_id_len ) != 0 ) |
| 4982 | { |
| 4983 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4984 | } |
| 4985 | |
| 4986 | *olen = p - obuf; |
| 4987 | |
| 4988 | /* Go back and fill length fields */ |
| 4989 | obuf[27] = (unsigned char)( *olen - 28 ); |
| 4990 | |
| 4991 | obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 ); |
| 4992 | obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 ); |
| 4993 | obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) ); |
| 4994 | |
| 4995 | obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 ); |
| 4996 | obuf[12] = (unsigned char)( ( *olen - 13 ) ); |
| 4997 | |
| 4998 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
| 4999 | } |
| 5000 | |
| 5001 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5002 | * Handle possible client reconnect with the same UDP quadruplet |
| 5003 | * (RFC 6347 Section 4.2.8). |
| 5004 | * |
| 5005 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 5006 | * that looks like a ClientHello. |
| 5007 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5008 | * - if the input looks like a ClientHello without cookies, |
| 5009 | * send back HelloVerifyRequest, then |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5010 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5011 | * - if the input looks like a ClientHello with a valid cookie, |
| 5012 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 5013 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5014 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5015 | * |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5016 | * mbedtls_ssl_read_record() will ignore the record if anything else than |
Simon Butcher | d0bf6a3 | 2015-09-11 17:34:49 +0100 | [diff] [blame] | 5017 | * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function |
| 5018 | * cannot not return 0. |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5019 | */ |
| 5020 | static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) |
| 5021 | { |
| 5022 | int ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5023 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5024 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5025 | if( ssl->conf->f_cookie_write == NULL || |
| 5026 | ssl->conf->f_cookie_check == NULL ) |
| 5027 | { |
| 5028 | /* If we can't use cookies to verify reachability of the peer, |
| 5029 | * drop the record. */ |
| 5030 | return( 0 ); |
| 5031 | } |
| 5032 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5033 | ret = ssl_check_dtls_clihlo_cookie( |
| 5034 | ssl->conf->f_cookie_write, |
| 5035 | ssl->conf->f_cookie_check, |
| 5036 | ssl->conf->p_cookie, |
| 5037 | ssl->cli_id, ssl->cli_id_len, |
| 5038 | ssl->in_buf, ssl->in_left, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5039 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5040 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5041 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret ); |
| 5042 | |
| 5043 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5044 | { |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 5045 | /* 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] | 5046 | * If the error is permanent we'll catch it later, |
| 5047 | * if it's not, then hopefully it'll work next time. */ |
| 5048 | (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len ); |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5049 | ret = 0; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5050 | } |
| 5051 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5052 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5053 | { |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5054 | /* Got a valid cookie, partially reset context */ |
| 5055 | if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 ) |
| 5056 | { |
| 5057 | MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret ); |
| 5058 | return( ret ); |
| 5059 | } |
| 5060 | |
| 5061 | return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5062 | } |
| 5063 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5064 | return( ret ); |
| 5065 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 5066 | #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] | 5067 | |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 5068 | static int ssl_check_record_type( uint8_t record_type ) |
| 5069 | { |
| 5070 | if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 5071 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 5072 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
| 5073 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 5074 | { |
| 5075 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5076 | } |
| 5077 | |
| 5078 | return( 0 ); |
| 5079 | } |
| 5080 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5081 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5082 | * ContentType type; |
| 5083 | * ProtocolVersion version; |
| 5084 | * uint16 epoch; // DTLS only |
| 5085 | * uint48 sequence_number; // DTLS only |
| 5086 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5087 | * |
| 5088 | * 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] | 5089 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5090 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 5091 | * |
| 5092 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 5093 | * 1. proceed with the record if this function returns 0 |
| 5094 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 5095 | * 3. return CLIENT_RECONNECT if this function return that value |
| 5096 | * 4. drop the whole datagram if this function returns anything else. |
| 5097 | * Point 2 is needed when the peer is resending, and we have already received |
| 5098 | * 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] | 5099 | */ |
Hanno Becker | 331de3d | 2019-07-12 11:10:16 +0100 | [diff] [blame] | 5100 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5101 | unsigned char *buf, |
| 5102 | size_t len, |
| 5103 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5104 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5105 | int major_ver, minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5106 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5107 | size_t const rec_hdr_type_offset = 0; |
| 5108 | size_t const rec_hdr_type_len = 1; |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 5109 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5110 | size_t const rec_hdr_version_offset = rec_hdr_type_offset + |
| 5111 | rec_hdr_type_len; |
| 5112 | size_t const rec_hdr_version_len = 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5113 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5114 | size_t const rec_hdr_ctr_len = 8; |
| 5115 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 5116 | uint32_t rec_epoch; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5117 | size_t const rec_hdr_ctr_offset = rec_hdr_version_offset + |
| 5118 | rec_hdr_version_len; |
| 5119 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5120 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5121 | size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset + |
| 5122 | rec_hdr_ctr_len; |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 5123 | size_t rec_hdr_cid_len = 0; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5124 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5125 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5126 | |
| 5127 | size_t rec_hdr_len_offset; /* To be determined */ |
| 5128 | size_t const rec_hdr_len_len = 2; |
| 5129 | |
| 5130 | /* |
| 5131 | * Check minimum lengths for record header. |
| 5132 | */ |
| 5133 | |
| 5134 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5135 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5136 | { |
| 5137 | rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len; |
| 5138 | } |
| 5139 | else |
| 5140 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5141 | { |
| 5142 | rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len; |
| 5143 | } |
| 5144 | |
| 5145 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) |
| 5146 | { |
| 5147 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u", |
| 5148 | (unsigned) len, |
| 5149 | (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) ); |
| 5150 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5151 | } |
| 5152 | |
| 5153 | /* |
| 5154 | * Parse and validate record content type |
| 5155 | */ |
| 5156 | |
| 5157 | rec->type = buf[ rec_hdr_type_offset ]; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5158 | |
| 5159 | /* Check record content type */ |
| 5160 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5161 | rec->cid_len = 0; |
| 5162 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5163 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5164 | ssl->conf->cid_len != 0 && |
| 5165 | rec->type == MBEDTLS_SSL_MSG_CID ) |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5166 | { |
| 5167 | /* Shift pointers to account for record header including CID |
| 5168 | * struct { |
| 5169 | * ContentType special_type = tls12_cid; |
| 5170 | * ProtocolVersion version; |
| 5171 | * uint16 epoch; |
| 5172 | * uint48 sequence_number; |
Hanno Becker | 8e55b0f | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 5173 | * opaque cid[cid_length]; // Additional field compared to |
| 5174 | * // default DTLS record format |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5175 | * uint16 length; |
| 5176 | * opaque enc_content[DTLSCiphertext.length]; |
| 5177 | * } DTLSCiphertext; |
| 5178 | */ |
| 5179 | |
| 5180 | /* So far, we only support static CID lengths |
| 5181 | * fixed in the configuration. */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5182 | rec_hdr_cid_len = ssl->conf->cid_len; |
| 5183 | rec_hdr_len_offset += rec_hdr_cid_len; |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5184 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5185 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5186 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5187 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u", |
| 5188 | (unsigned) len, |
| 5189 | (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) ); |
Hanno Becker | 59be60e | 2019-07-10 14:53:43 +0100 | [diff] [blame] | 5190 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5191 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5192 | |
Manuel Pégourié-Gonnard | 7e821b5 | 2019-08-02 10:17:15 +0200 | [diff] [blame] | 5193 | /* configured CID len is guaranteed at most 255, see |
| 5194 | * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */ |
| 5195 | rec->cid_len = (uint8_t) rec_hdr_cid_len; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5196 | memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len ); |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5197 | } |
| 5198 | else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5199 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 5200 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5201 | if( ssl_check_record_type( rec->type ) ) |
| 5202 | { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 5203 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u", |
| 5204 | (unsigned) rec->type ) ); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5205 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5206 | } |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 5207 | } |
| 5208 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5209 | /* |
| 5210 | * Parse and validate record version |
| 5211 | */ |
| 5212 | |
Hanno Becker | d0b66d0 | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 5213 | rec->ver[0] = buf[ rec_hdr_version_offset + 0 ]; |
| 5214 | rec->ver[1] = buf[ rec_hdr_version_offset + 1 ]; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5215 | mbedtls_ssl_read_version( &major_ver, &minor_ver, |
| 5216 | ssl->conf->transport, |
Hanno Becker | d0b66d0 | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 5217 | &rec->ver[0] ); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5218 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5219 | if( major_ver != ssl->major_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5220 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5221 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); |
| 5222 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5223 | } |
| 5224 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5225 | if( minor_ver > ssl->conf->max_minor_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5226 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5227 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); |
| 5228 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5229 | } |
| 5230 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5231 | /* |
| 5232 | * Parse/Copy record sequence number. |
| 5233 | */ |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5234 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5235 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5236 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 5237 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5238 | /* Copy explicit record sequence number from input buffer. */ |
| 5239 | memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset, |
| 5240 | rec_hdr_ctr_len ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 5241 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5242 | else |
| 5243 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5244 | { |
| 5245 | /* Copy implicit record sequence number from SSL context structure. */ |
| 5246 | memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len ); |
| 5247 | } |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 5248 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5249 | /* |
| 5250 | * Parse record length. |
| 5251 | */ |
| 5252 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5253 | rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len; |
Hanno Becker | 9eca276 | 2019-07-25 10:16:37 +0100 | [diff] [blame] | 5254 | rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) | |
| 5255 | ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 ); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5256 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5257 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5258 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, " |
Hanno Becker | 92d30f5 | 2019-05-23 17:03:44 +0100 | [diff] [blame] | 5259 | "version = [%d:%d], msglen = %d", |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5260 | rec->type, |
| 5261 | major_ver, minor_ver, rec->data_len ) ); |
| 5262 | |
| 5263 | rec->buf = buf; |
| 5264 | rec->buf_len = rec->data_offset + rec->data_len; |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5265 | |
Hanno Becker | d417cc9 | 2019-07-26 08:20:27 +0100 | [diff] [blame] | 5266 | if( rec->data_len == 0 ) |
| 5267 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5268 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5269 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 5270 | * DTLS-related tests. |
| 5271 | * Check epoch before checking length constraint because |
| 5272 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 5273 | * message gets duplicated before the corresponding Finished message, |
| 5274 | * the second ChangeCipherSpec should be discarded because it belongs |
| 5275 | * to an old epoch, but not because its length is shorter than |
| 5276 | * the minimum record length for packets using the new record transform. |
| 5277 | * Note that these two kinds of failures are handled differently, |
| 5278 | * as an unexpected record is silently skipped but an invalid |
| 5279 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5280 | */ |
| 5281 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5282 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5283 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5284 | rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1]; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5285 | |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5286 | /* Check that the datagram is large enough to contain a record |
| 5287 | * of the advertised length. */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5288 | if( len < rec->data_offset + rec->data_len ) |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5289 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5290 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.", |
| 5291 | (unsigned) len, |
| 5292 | (unsigned)( rec->data_offset + rec->data_len ) ) ); |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5293 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5294 | } |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5295 | |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5296 | /* Records from other, non-matching epochs are silently discarded. |
| 5297 | * (The case of same-port Client reconnects must be considered in |
| 5298 | * the caller). */ |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5299 | if( rec_epoch != ssl->in_epoch ) |
| 5300 | { |
| 5301 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " |
| 5302 | "expected %d, received %d", |
| 5303 | ssl->in_epoch, rec_epoch ) ); |
| 5304 | |
Hanno Becker | 552f747 | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 5305 | /* Records from the next epoch are considered for buffering |
| 5306 | * (concretely: early Finished messages). */ |
| 5307 | if( rec_epoch == (unsigned) ssl->in_epoch + 1 ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5308 | { |
Hanno Becker | 552f747 | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 5309 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) ); |
| 5310 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5311 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5312 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5313 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5314 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5315 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5316 | /* For records from the correct epoch, check whether their |
| 5317 | * sequence number has been seen before. */ |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5318 | else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5319 | { |
| 5320 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); |
| 5321 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 5322 | } |
| 5323 | #endif |
| 5324 | } |
| 5325 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5326 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5327 | return( 0 ); |
| 5328 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5329 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5330 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5331 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
| 5332 | static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl ) |
| 5333 | { |
| 5334 | unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1]; |
| 5335 | |
| 5336 | /* |
| 5337 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 5338 | * access the first byte of record content (handshake type), as we |
| 5339 | * have an active transform (possibly iv_len != 0), so use the |
| 5340 | * fact that the record header len is 13 instead. |
| 5341 | */ |
| 5342 | if( rec_epoch == 0 && |
| 5343 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5344 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 5345 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 5346 | ssl->in_left > 13 && |
| 5347 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO ) |
| 5348 | { |
| 5349 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect " |
| 5350 | "from the same port" ) ); |
| 5351 | return( ssl_handle_possible_reconnect( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5352 | } |
| 5353 | |
| 5354 | return( 0 ); |
| 5355 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5356 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5357 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5358 | /* |
| 5359 | * If applicable, decrypt (and decompress) record content |
| 5360 | */ |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5361 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl, |
| 5362 | mbedtls_record *rec ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5363 | { |
| 5364 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 5365 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5366 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5367 | rec->buf, rec->buf_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5368 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5369 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 5370 | if( mbedtls_ssl_hw_record_read != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5371 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5372 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5374 | ret = mbedtls_ssl_hw_record_read( ssl ); |
| 5375 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5376 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5377 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret ); |
| 5378 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5379 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 5380 | |
| 5381 | if( ret == 0 ) |
| 5382 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5383 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5384 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5385 | if( !done && ssl->transform_in != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5386 | { |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5387 | unsigned char const old_msg_type = rec->type; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5388 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 5389 | if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5390 | rec ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5391 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5392 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5393 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5394 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5395 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
| 5396 | ssl->conf->ignore_unexpected_cid |
| 5397 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 5398 | { |
Hanno Becker | e8d6afd | 2019-05-24 10:11:06 +0100 | [diff] [blame] | 5399 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) ); |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5400 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5401 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5402 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5403 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5404 | return( ret ); |
| 5405 | } |
| 5406 | |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5407 | if( old_msg_type != rec->type ) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5408 | { |
| 5409 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d", |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5410 | old_msg_type, rec->type ) ); |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5411 | } |
| 5412 | |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 5413 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5414 | rec->buf + rec->data_offset, rec->data_len ); |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 5415 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5416 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5417 | /* We have already checked the record content type |
| 5418 | * in ssl_parse_record_header(), failing or silently |
| 5419 | * dropping the record in the case of an unknown type. |
| 5420 | * |
| 5421 | * Since with the use of CIDs, the record content type |
| 5422 | * might change during decryption, re-check the record |
| 5423 | * content type, but treat a failure as fatal this time. */ |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5424 | if( ssl_check_record_type( rec->type ) ) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5425 | { |
| 5426 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
| 5427 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5428 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5429 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5430 | |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5431 | if( rec->data_len == 0 ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5432 | { |
| 5433 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5434 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5435 | && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5436 | { |
| 5437 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ |
| 5438 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) ); |
| 5439 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5440 | } |
| 5441 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5442 | |
| 5443 | ssl->nb_zero++; |
| 5444 | |
| 5445 | /* |
| 5446 | * Three or more empty messages may be a DoS attack |
| 5447 | * (excessive CPU consumption). |
| 5448 | */ |
| 5449 | if( ssl->nb_zero > 3 ) |
| 5450 | { |
| 5451 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " |
Hanno Becker | 6e7700d | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 5452 | "messages, possible DoS attack" ) ); |
| 5453 | /* Treat the records as if they were not properly authenticated, |
| 5454 | * thereby failing the connection if we see more than allowed |
| 5455 | * by the configured bad MAC threshold. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5456 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
| 5457 | } |
| 5458 | } |
| 5459 | else |
| 5460 | ssl->nb_zero = 0; |
| 5461 | |
| 5462 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5463 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5464 | { |
| 5465 | ; /* in_ctr read from peer, not maintained internally */ |
| 5466 | } |
| 5467 | else |
| 5468 | #endif |
| 5469 | { |
| 5470 | unsigned i; |
| 5471 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 5472 | if( ++ssl->in_ctr[i - 1] != 0 ) |
| 5473 | break; |
| 5474 | |
| 5475 | /* The loop goes to its end iff the counter is wrapping */ |
| 5476 | if( i == ssl_ep_len( ssl ) ) |
| 5477 | { |
| 5478 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); |
| 5479 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 5480 | } |
| 5481 | } |
| 5482 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5483 | } |
| 5484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5485 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5486 | if( ssl->transform_in != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5487 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5488 | { |
| 5489 | if( ( ret = ssl_decompress_buf( ssl ) ) != 0 ) |
| 5490 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5491 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5492 | return( ret ); |
| 5493 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5494 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5495 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5497 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5498 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5499 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5500 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5501 | } |
| 5502 | #endif |
| 5503 | |
Hanno Becker | d96e10b | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 5504 | /* Check actual (decrypted) record content length against |
| 5505 | * configured maximum. */ |
| 5506 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5507 | { |
| 5508 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 5509 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5510 | } |
| 5511 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5512 | return( 0 ); |
| 5513 | } |
| 5514 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5515 | 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] | 5516 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5517 | /* |
| 5518 | * Read a record. |
| 5519 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5520 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 5521 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 5522 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5523 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5524 | |
| 5525 | /* Helper functions for mbedtls_ssl_read_record(). */ |
| 5526 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5527 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ); |
| 5528 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 5529 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5530 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl, |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5531 | unsigned update_hs_digest ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5532 | { |
| 5533 | int ret; |
| 5534 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5535 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5536 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5537 | if( ssl->keep_current_message == 0 ) |
| 5538 | { |
| 5539 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5540 | |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5541 | ret = ssl_consume_current_message( ssl ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5542 | if( ret != 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5543 | return( ret ); |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5544 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5545 | if( ssl_record_is_in_progress( ssl ) == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5546 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5547 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5548 | int have_buffered = 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5549 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5550 | /* We only check for buffered messages if the |
| 5551 | * current datagram is fully consumed. */ |
| 5552 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5553 | ssl_next_record_is_in_datagram( ssl ) == 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5554 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5555 | if( ssl_load_buffered_message( ssl ) == 0 ) |
| 5556 | have_buffered = 1; |
| 5557 | } |
| 5558 | |
| 5559 | if( have_buffered == 0 ) |
| 5560 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5561 | { |
| 5562 | ret = ssl_get_next_record( ssl ); |
| 5563 | if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ) |
| 5564 | continue; |
| 5565 | |
| 5566 | if( ret != 0 ) |
| 5567 | { |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 5568 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5569 | return( ret ); |
| 5570 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5571 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5572 | } |
| 5573 | |
| 5574 | ret = mbedtls_ssl_handle_message_type( ssl ); |
| 5575 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5576 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5577 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 5578 | { |
| 5579 | /* Buffer future message */ |
| 5580 | ret = ssl_buffer_message( ssl ); |
| 5581 | if( ret != 0 ) |
| 5582 | return( ret ); |
| 5583 | |
| 5584 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 5585 | } |
| 5586 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5587 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5588 | } while( MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 5589 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5590 | |
| 5591 | if( 0 != ret ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5592 | { |
Hanno Becker | 05c4fc8 | 2017-11-09 14:34:06 +0000 | [diff] [blame] | 5593 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5594 | return( ret ); |
| 5595 | } |
| 5596 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5597 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5598 | update_hs_digest == 1 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5599 | { |
| 5600 | mbedtls_ssl_update_handshake_status( ssl ); |
| 5601 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5602 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5603 | else |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5604 | { |
Hanno Becker | 02f5907 | 2018-08-15 14:00:24 +0100 | [diff] [blame] | 5605 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5606 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5607 | } |
| 5608 | |
| 5609 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); |
| 5610 | |
| 5611 | return( 0 ); |
| 5612 | } |
| 5613 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5614 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5615 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5616 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5617 | if( ssl->in_left > ssl->next_record_offset ) |
| 5618 | return( 1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5619 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5620 | return( 0 ); |
| 5621 | } |
| 5622 | |
| 5623 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ) |
| 5624 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5625 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5626 | mbedtls_ssl_hs_buffer * hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5627 | int ret = 0; |
| 5628 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5629 | if( hs == NULL ) |
| 5630 | return( -1 ); |
| 5631 | |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 5632 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) ); |
| 5633 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5634 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 5635 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 5636 | { |
| 5637 | /* Check if we have seen a ChangeCipherSpec before. |
| 5638 | * If yes, synthesize a CCS record. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5639 | if( !hs->buffering.seen_ccs ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5640 | { |
| 5641 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) ); |
| 5642 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 5643 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5644 | } |
| 5645 | |
Hanno Becker | 39b8bc9 | 2018-08-28 17:17:13 +0100 | [diff] [blame] | 5646 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5647 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 5648 | ssl->in_msglen = 1; |
| 5649 | ssl->in_msg[0] = 1; |
| 5650 | |
| 5651 | /* As long as they are equal, the exact value doesn't matter. */ |
| 5652 | ssl->in_left = 0; |
| 5653 | ssl->next_record_offset = 0; |
| 5654 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5655 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5656 | goto exit; |
| 5657 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5658 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5659 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5660 | /* Debug only */ |
| 5661 | { |
| 5662 | unsigned offset; |
| 5663 | for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
| 5664 | { |
| 5665 | hs_buf = &hs->buffering.hs[offset]; |
| 5666 | if( hs_buf->is_valid == 1 ) |
| 5667 | { |
| 5668 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.", |
| 5669 | hs->in_msg_seq + offset, |
Hanno Becker | a591c48 | 2018-08-28 17:20:00 +0100 | [diff] [blame] | 5670 | hs_buf->is_complete ? "fully" : "partially" ) ); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5671 | } |
| 5672 | } |
| 5673 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5674 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5675 | |
| 5676 | /* Check if we have buffered and/or fully reassembled the |
| 5677 | * next handshake message. */ |
| 5678 | hs_buf = &hs->buffering.hs[0]; |
| 5679 | if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) ) |
| 5680 | { |
| 5681 | /* Synthesize a record containing the buffered HS message. */ |
| 5682 | size_t msg_len = ( hs_buf->data[1] << 16 ) | |
| 5683 | ( hs_buf->data[2] << 8 ) | |
| 5684 | hs_buf->data[3]; |
| 5685 | |
| 5686 | /* Double-check that we haven't accidentally buffered |
| 5687 | * a message that doesn't fit into the input buffer. */ |
| 5688 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5689 | { |
| 5690 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5691 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5692 | } |
| 5693 | |
| 5694 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) ); |
| 5695 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)", |
| 5696 | hs_buf->data, msg_len + 12 ); |
| 5697 | |
| 5698 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5699 | ssl->in_hslen = msg_len + 12; |
| 5700 | ssl->in_msglen = msg_len + 12; |
| 5701 | memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen ); |
| 5702 | |
| 5703 | ret = 0; |
| 5704 | goto exit; |
| 5705 | } |
| 5706 | else |
| 5707 | { |
| 5708 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered", |
| 5709 | hs->in_msg_seq ) ); |
| 5710 | } |
| 5711 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5712 | ret = -1; |
| 5713 | |
| 5714 | exit: |
| 5715 | |
| 5716 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) ); |
| 5717 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5718 | } |
| 5719 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5720 | static int ssl_buffer_make_space( mbedtls_ssl_context *ssl, |
| 5721 | size_t desired ) |
| 5722 | { |
| 5723 | int offset; |
| 5724 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5725 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available", |
| 5726 | (unsigned) desired ) ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5727 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5728 | /* Get rid of future records epoch first, if such exist. */ |
| 5729 | ssl_free_buffered_record( ssl ); |
| 5730 | |
| 5731 | /* Check if we have enough space available now. */ |
| 5732 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5733 | hs->buffering.total_bytes_buffered ) ) |
| 5734 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5735 | 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] | 5736 | return( 0 ); |
| 5737 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5738 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 5739 | /* We don't have enough space to buffer the next expected handshake |
| 5740 | * message. Remove buffers used for future messages to gain space, |
| 5741 | * starting with the most distant one. */ |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5742 | for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 5743 | offset >= 0; offset-- ) |
| 5744 | { |
| 5745 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message", |
| 5746 | offset ) ); |
| 5747 | |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5748 | ssl_buffering_free_slot( ssl, (uint8_t) offset ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5749 | |
| 5750 | /* Check if we have enough space available now. */ |
| 5751 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5752 | hs->buffering.total_bytes_buffered ) ) |
| 5753 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5754 | 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] | 5755 | return( 0 ); |
| 5756 | } |
| 5757 | } |
| 5758 | |
| 5759 | return( -1 ); |
| 5760 | } |
| 5761 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5762 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ) |
| 5763 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5764 | int ret = 0; |
| 5765 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5766 | |
| 5767 | if( hs == NULL ) |
| 5768 | return( 0 ); |
| 5769 | |
| 5770 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) ); |
| 5771 | |
| 5772 | switch( ssl->in_msgtype ) |
| 5773 | { |
| 5774 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
| 5775 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) ); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5776 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5777 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5778 | break; |
| 5779 | |
| 5780 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5781 | { |
| 5782 | unsigned recv_msg_seq_offset; |
| 5783 | unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
| 5784 | mbedtls_ssl_hs_buffer *hs_buf; |
| 5785 | size_t msg_len = ssl->in_hslen - 12; |
| 5786 | |
| 5787 | /* We should never receive an old handshake |
| 5788 | * message - double-check nonetheless. */ |
| 5789 | if( recv_msg_seq < ssl->handshake->in_msg_seq ) |
| 5790 | { |
| 5791 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5792 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5793 | } |
| 5794 | |
| 5795 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
| 5796 | if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 5797 | { |
| 5798 | /* Silently ignore -- message too far in the future */ |
| 5799 | MBEDTLS_SSL_DEBUG_MSG( 2, |
| 5800 | ( "Ignore future HS message with sequence number %u, " |
| 5801 | "buffering window %u - %u", |
| 5802 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 5803 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) ); |
| 5804 | |
| 5805 | goto exit; |
| 5806 | } |
| 5807 | |
| 5808 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ", |
| 5809 | recv_msg_seq, recv_msg_seq_offset ) ); |
| 5810 | |
| 5811 | hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ]; |
| 5812 | |
| 5813 | /* Check if the buffering for this seq nr has already commenced. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5814 | if( !hs_buf->is_valid ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5815 | { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5816 | size_t reassembly_buf_sz; |
| 5817 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5818 | hs_buf->is_fragmented = |
| 5819 | ( ssl_hs_is_proper_fragment( ssl ) == 1 ); |
| 5820 | |
| 5821 | /* We copy the message back into the input buffer |
| 5822 | * after reassembly, so check that it's not too large. |
| 5823 | * This is an implementation-specific limitation |
| 5824 | * and not one from the standard, hence it is not |
| 5825 | * checked in ssl_check_hs_header(). */ |
Hanno Becker | 96a6c69 | 2018-08-21 15:56:03 +0100 | [diff] [blame] | 5826 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5827 | { |
| 5828 | /* Ignore message */ |
| 5829 | goto exit; |
| 5830 | } |
| 5831 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5832 | /* Check if we have enough space to buffer the message. */ |
| 5833 | if( hs->buffering.total_bytes_buffered > |
| 5834 | MBEDTLS_SSL_DTLS_MAX_BUFFERING ) |
| 5835 | { |
| 5836 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5837 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5838 | } |
| 5839 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5840 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len, |
| 5841 | hs_buf->is_fragmented ); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5842 | |
| 5843 | if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5844 | hs->buffering.total_bytes_buffered ) ) |
| 5845 | { |
| 5846 | if( recv_msg_seq_offset > 0 ) |
| 5847 | { |
| 5848 | /* If we can't buffer a future message because |
| 5849 | * of space limitations -- ignore. */ |
| 5850 | 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", |
| 5851 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5852 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5853 | goto exit; |
| 5854 | } |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 5855 | else |
| 5856 | { |
| 5857 | 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", |
| 5858 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5859 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5860 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5861 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5862 | if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 ) |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5863 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5864 | 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", |
| 5865 | (unsigned) msg_len, |
| 5866 | (unsigned) reassembly_buf_sz, |
| 5867 | MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5868 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5869 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 5870 | goto exit; |
| 5871 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5872 | } |
| 5873 | |
| 5874 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d", |
| 5875 | msg_len ) ); |
| 5876 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5877 | hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz ); |
| 5878 | if( hs_buf->data == NULL ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5879 | { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5880 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5881 | goto exit; |
| 5882 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5883 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5884 | |
| 5885 | /* Prepare final header: copy msg_type, length and message_seq, |
| 5886 | * then add standardised fragment_offset and fragment_length */ |
| 5887 | memcpy( hs_buf->data, ssl->in_msg, 6 ); |
| 5888 | memset( hs_buf->data + 6, 0, 3 ); |
| 5889 | memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 ); |
| 5890 | |
| 5891 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5892 | |
| 5893 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5894 | } |
| 5895 | else |
| 5896 | { |
| 5897 | /* Make sure msg_type and length are consistent */ |
| 5898 | if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) |
| 5899 | { |
| 5900 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) ); |
| 5901 | /* Ignore */ |
| 5902 | goto exit; |
| 5903 | } |
| 5904 | } |
| 5905 | |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5906 | if( !hs_buf->is_complete ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5907 | { |
| 5908 | size_t frag_len, frag_off; |
| 5909 | unsigned char * const msg = hs_buf->data + 12; |
| 5910 | |
| 5911 | /* |
| 5912 | * Check and copy current fragment |
| 5913 | */ |
| 5914 | |
| 5915 | /* Validation of header fields already done in |
| 5916 | * mbedtls_ssl_prepare_handshake_record(). */ |
| 5917 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 5918 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 5919 | |
| 5920 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d", |
| 5921 | frag_off, frag_len ) ); |
| 5922 | memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); |
| 5923 | |
| 5924 | if( hs_buf->is_fragmented ) |
| 5925 | { |
| 5926 | unsigned char * const bitmask = msg + msg_len; |
| 5927 | ssl_bitmask_set( bitmask, frag_off, frag_len ); |
| 5928 | hs_buf->is_complete = ( ssl_bitmask_check( bitmask, |
| 5929 | msg_len ) == 0 ); |
| 5930 | } |
| 5931 | else |
| 5932 | { |
| 5933 | hs_buf->is_complete = 1; |
| 5934 | } |
| 5935 | |
| 5936 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete", |
| 5937 | hs_buf->is_complete ? "" : "not yet " ) ); |
| 5938 | } |
| 5939 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5940 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5941 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5942 | |
| 5943 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 5944 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5945 | break; |
| 5946 | } |
| 5947 | |
| 5948 | exit: |
| 5949 | |
| 5950 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) ); |
| 5951 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5952 | } |
| 5953 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5954 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5955 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5956 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5957 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5958 | * Consume last content-layer message and potentially |
| 5959 | * update in_msglen which keeps track of the contents' |
| 5960 | * consumption state. |
| 5961 | * |
| 5962 | * (1) Handshake messages: |
| 5963 | * Remove last handshake message, move content |
| 5964 | * and adapt in_msglen. |
| 5965 | * |
| 5966 | * (2) Alert messages: |
| 5967 | * Consume whole record content, in_msglen = 0. |
| 5968 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5969 | * (3) Change cipher spec: |
| 5970 | * Consume whole record content, in_msglen = 0. |
| 5971 | * |
| 5972 | * (4) Application data: |
| 5973 | * Don't do anything - the record layer provides |
| 5974 | * the application data as a stream transport |
| 5975 | * and consumes through mbedtls_ssl_read only. |
| 5976 | * |
| 5977 | */ |
| 5978 | |
| 5979 | /* Case (1): Handshake messages */ |
| 5980 | if( ssl->in_hslen != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5981 | { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5982 | /* Hard assertion to be sure that no application data |
| 5983 | * is in flight, as corrupting ssl->in_msglen during |
| 5984 | * ssl->in_offt != NULL is fatal. */ |
| 5985 | if( ssl->in_offt != NULL ) |
| 5986 | { |
| 5987 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5988 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5989 | } |
| 5990 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5991 | /* |
| 5992 | * Get next Handshake message in the current record |
| 5993 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5994 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5995 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5996 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5997 | * current handshake content: If DTLS handshake |
| 5998 | * fragmentation is used, that's the fragment |
| 5999 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 6000 | * size here is faulty and should be changed at |
| 6001 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6002 | * (2) While it doesn't seem to cause problems, one |
| 6003 | * has to be very careful not to assume that in_hslen |
| 6004 | * is always <= in_msglen in a sensible communication. |
| 6005 | * Again, it's wrong for DTLS handshake fragmentation. |
| 6006 | * The following check is therefore mandatory, and |
| 6007 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 6008 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 6009 | * bounds after handling a DTLS message with an unexpected |
| 6010 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6011 | */ |
| 6012 | if( ssl->in_hslen < ssl->in_msglen ) |
| 6013 | { |
| 6014 | ssl->in_msglen -= ssl->in_hslen; |
| 6015 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 6016 | ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6017 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6018 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", |
| 6019 | ssl->in_msg, ssl->in_msglen ); |
| 6020 | } |
| 6021 | else |
| 6022 | { |
| 6023 | ssl->in_msglen = 0; |
| 6024 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 6025 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6026 | ssl->in_hslen = 0; |
| 6027 | } |
| 6028 | /* Case (4): Application data */ |
| 6029 | else if( ssl->in_offt != NULL ) |
| 6030 | { |
| 6031 | return( 0 ); |
| 6032 | } |
| 6033 | /* Everything else (CCS & Alerts) */ |
| 6034 | else |
| 6035 | { |
| 6036 | ssl->in_msglen = 0; |
| 6037 | } |
| 6038 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6039 | return( 0 ); |
| 6040 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6041 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 6042 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ) |
| 6043 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6044 | if( ssl->in_msglen > 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 6045 | return( 1 ); |
| 6046 | |
| 6047 | return( 0 ); |
| 6048 | } |
| 6049 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6050 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6051 | |
| 6052 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ) |
| 6053 | { |
| 6054 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 6055 | if( hs == NULL ) |
| 6056 | return; |
| 6057 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6058 | if( hs->buffering.future_record.data != NULL ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6059 | { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6060 | hs->buffering.total_bytes_buffered -= |
| 6061 | hs->buffering.future_record.len; |
| 6062 | |
| 6063 | mbedtls_free( hs->buffering.future_record.data ); |
| 6064 | hs->buffering.future_record.data = NULL; |
| 6065 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6066 | } |
| 6067 | |
| 6068 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ) |
| 6069 | { |
| 6070 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 6071 | unsigned char * rec; |
| 6072 | size_t rec_len; |
| 6073 | unsigned rec_epoch; |
| 6074 | |
| 6075 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6076 | return( 0 ); |
| 6077 | |
| 6078 | if( hs == NULL ) |
| 6079 | return( 0 ); |
| 6080 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6081 | rec = hs->buffering.future_record.data; |
| 6082 | rec_len = hs->buffering.future_record.len; |
| 6083 | rec_epoch = hs->buffering.future_record.epoch; |
| 6084 | |
| 6085 | if( rec == NULL ) |
| 6086 | return( 0 ); |
| 6087 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 6088 | /* Only consider loading future records if the |
| 6089 | * input buffer is empty. */ |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 6090 | if( ssl_next_record_is_in_datagram( ssl ) == 1 ) |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 6091 | return( 0 ); |
| 6092 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6093 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) ); |
| 6094 | |
| 6095 | if( rec_epoch != ssl->in_epoch ) |
| 6096 | { |
| 6097 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) ); |
| 6098 | goto exit; |
| 6099 | } |
| 6100 | |
| 6101 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) ); |
| 6102 | |
| 6103 | /* Double-check that the record is not too large */ |
| 6104 | if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN - |
| 6105 | (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
| 6106 | { |
| 6107 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6108 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6109 | } |
| 6110 | |
| 6111 | memcpy( ssl->in_hdr, rec, rec_len ); |
| 6112 | ssl->in_left = rec_len; |
| 6113 | ssl->next_record_offset = 0; |
| 6114 | |
| 6115 | ssl_free_buffered_record( ssl ); |
| 6116 | |
| 6117 | exit: |
| 6118 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) ); |
| 6119 | return( 0 ); |
| 6120 | } |
| 6121 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6122 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, |
| 6123 | mbedtls_record const *rec ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6124 | { |
| 6125 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6126 | |
| 6127 | /* Don't buffer future records outside handshakes. */ |
| 6128 | if( hs == NULL ) |
| 6129 | return( 0 ); |
| 6130 | |
| 6131 | /* Only buffer handshake records (we are only interested |
| 6132 | * in Finished messages). */ |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6133 | if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6134 | return( 0 ); |
| 6135 | |
| 6136 | /* Don't buffer more than one future epoch record. */ |
| 6137 | if( hs->buffering.future_record.data != NULL ) |
| 6138 | return( 0 ); |
| 6139 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6140 | /* Don't buffer record if there's not enough buffering space remaining. */ |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6141 | if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6142 | hs->buffering.total_bytes_buffered ) ) |
| 6143 | { |
| 6144 | 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] | 6145 | (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6146 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6147 | return( 0 ); |
| 6148 | } |
| 6149 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6150 | /* Buffer record */ |
| 6151 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u", |
| 6152 | ssl->in_epoch + 1 ) ); |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6153 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6154 | |
| 6155 | /* ssl_parse_record_header() only considers records |
| 6156 | * of the next epoch as candidates for buffering. */ |
| 6157 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6158 | hs->buffering.future_record.len = rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6159 | |
| 6160 | hs->buffering.future_record.data = |
| 6161 | mbedtls_calloc( 1, hs->buffering.future_record.len ); |
| 6162 | if( hs->buffering.future_record.data == NULL ) |
| 6163 | { |
| 6164 | /* If we run out of RAM trying to buffer a |
| 6165 | * record from the next epoch, just ignore. */ |
| 6166 | return( 0 ); |
| 6167 | } |
| 6168 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6169 | memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6170 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6171 | hs->buffering.total_bytes_buffered += rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6172 | return( 0 ); |
| 6173 | } |
| 6174 | |
| 6175 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6176 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 6177 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6178 | { |
| 6179 | int ret; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 6180 | mbedtls_record rec; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6181 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6182 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6183 | /* We might have buffered a future record; if so, |
| 6184 | * and if the epoch matches now, load it. |
| 6185 | * On success, this call will set ssl->in_left to |
| 6186 | * the length of the buffered record, so that |
| 6187 | * the calls to ssl_fetch_input() below will |
| 6188 | * essentially be no-ops. */ |
| 6189 | ret = ssl_load_buffered_record( ssl ); |
| 6190 | if( ret != 0 ) |
| 6191 | return( ret ); |
| 6192 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6193 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 6194 | /* Ensure that we have enough space available for the default form |
| 6195 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 6196 | * with no space for CIDs counted in). */ |
| 6197 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 6198 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6199 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6200 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6201 | return( ret ); |
| 6202 | } |
| 6203 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 6204 | ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec ); |
| 6205 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6206 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6207 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6208 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6209 | { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6210 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 6211 | { |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6212 | ret = ssl_buffer_future_record( ssl, &rec ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6213 | if( ret != 0 ) |
| 6214 | return( ret ); |
| 6215 | |
| 6216 | /* Fall through to handling of unexpected records */ |
| 6217 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 6218 | } |
| 6219 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6220 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ) |
| 6221 | { |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6222 | #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] | 6223 | /* Reset in pointers to default state for TLS/DTLS records, |
| 6224 | * assuming no CID and no offset between record content and |
| 6225 | * record plaintext. */ |
| 6226 | ssl_update_in_pointers( ssl ); |
| 6227 | |
Hanno Becker | 7ae20e0 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 6228 | /* Setup internal message pointers from record structure. */ |
| 6229 | ssl->in_msgtype = rec.type; |
| 6230 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6231 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 6232 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6233 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
| 6234 | ssl->in_msglen = rec.data_len; |
| 6235 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6236 | ret = ssl_check_client_reconnect( ssl ); |
| 6237 | if( ret != 0 ) |
| 6238 | return( ret ); |
| 6239 | #endif |
| 6240 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6241 | /* Skip unexpected record (but not whole datagram) */ |
Hanno Becker | 4acada3 | 2019-07-11 12:48:53 +0100 | [diff] [blame] | 6242 | ssl->next_record_offset = rec.buf_len; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6243 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6244 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record " |
| 6245 | "(header)" ) ); |
| 6246 | } |
| 6247 | else |
| 6248 | { |
| 6249 | /* Skip invalid record and the rest of the datagram */ |
| 6250 | ssl->next_record_offset = 0; |
| 6251 | ssl->in_left = 0; |
| 6252 | |
| 6253 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record " |
| 6254 | "(header)" ) ); |
| 6255 | } |
| 6256 | |
| 6257 | /* Get next record */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6258 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6259 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6260 | else |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6261 | #endif |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6262 | { |
| 6263 | return( ret ); |
| 6264 | } |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6265 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6266 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6267 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6268 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 6269 | { |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6270 | /* Remember offset of next record within datagram. */ |
Hanno Becker | f50da50 | 2019-07-11 12:50:10 +0100 | [diff] [blame] | 6271 | ssl->next_record_offset = rec.buf_len; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 6272 | if( ssl->next_record_offset < ssl->in_left ) |
| 6273 | { |
| 6274 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) ); |
| 6275 | } |
| 6276 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6277 | else |
| 6278 | #endif |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6279 | { |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 6280 | /* |
| 6281 | * Fetch record contents from underlying transport. |
| 6282 | */ |
Hanno Becker | a317566 | 2019-07-11 12:50:29 +0100 | [diff] [blame] | 6283 | ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len ); |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6284 | if( ret != 0 ) |
| 6285 | { |
| 6286 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
| 6287 | return( ret ); |
| 6288 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6289 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6290 | ssl->in_left = 0; |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6291 | } |
| 6292 | |
| 6293 | /* |
| 6294 | * Decrypt record contents. |
| 6295 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6296 | |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 6297 | if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6298 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6299 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6300 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6301 | { |
| 6302 | /* Silently discard invalid records */ |
Hanno Becker | 82e2a39 | 2019-05-03 16:36:59 +0100 | [diff] [blame] | 6303 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6304 | { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 6305 | /* Except when waiting for Finished as a bad mac here |
| 6306 | * probably means something went wrong in the handshake |
| 6307 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
| 6308 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 6309 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) |
| 6310 | { |
| 6311 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 6312 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 6313 | { |
| 6314 | mbedtls_ssl_send_alert_message( ssl, |
| 6315 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6316 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
| 6317 | } |
| 6318 | #endif |
| 6319 | return( ret ); |
| 6320 | } |
| 6321 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6322 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6323 | if( ssl->conf->badmac_limit != 0 && |
| 6324 | ++ssl->badmac_seen >= ssl->conf->badmac_limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6325 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6326 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); |
| 6327 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6328 | } |
| 6329 | #endif |
| 6330 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6331 | /* As above, invalid records cause |
| 6332 | * dismissal of the whole datagram. */ |
| 6333 | |
| 6334 | ssl->next_record_offset = 0; |
| 6335 | ssl->in_left = 0; |
| 6336 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6337 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6338 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6339 | } |
| 6340 | |
| 6341 | return( ret ); |
| 6342 | } |
| 6343 | else |
| 6344 | #endif |
| 6345 | { |
| 6346 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6347 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 6348 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6349 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6350 | mbedtls_ssl_send_alert_message( ssl, |
| 6351 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6352 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6353 | } |
| 6354 | #endif |
| 6355 | return( ret ); |
| 6356 | } |
| 6357 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6358 | |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6359 | |
| 6360 | /* Reset in pointers to default state for TLS/DTLS records, |
| 6361 | * assuming no CID and no offset between record content and |
| 6362 | * record plaintext. */ |
| 6363 | ssl_update_in_pointers( ssl ); |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6364 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6365 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 6366 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6367 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6368 | |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 6369 | /* The record content type may change during decryption, |
| 6370 | * so re-read it. */ |
| 6371 | ssl->in_msgtype = rec.type; |
| 6372 | /* Also update the input buffer, because unfortunately |
| 6373 | * the server-side ssl_parse_client_hello() reparses the |
| 6374 | * record header when receiving a ClientHello initiating |
| 6375 | * a renegotiation. */ |
| 6376 | ssl->in_hdr[0] = rec.type; |
| 6377 | ssl->in_msg = rec.buf + rec.data_offset; |
| 6378 | ssl->in_msglen = rec.data_len; |
| 6379 | ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 6380 | ssl->in_len[1] = (unsigned char)( rec.data_len ); |
| 6381 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6382 | return( 0 ); |
| 6383 | } |
| 6384 | |
| 6385 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ) |
| 6386 | { |
| 6387 | int ret; |
| 6388 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6389 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6390 | * Handle particular types of records |
| 6391 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6392 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6393 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6394 | if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 ) |
| 6395 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 6396 | return( ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6397 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6398 | } |
| 6399 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6400 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6401 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6402 | if( ssl->in_msglen != 1 ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6403 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6404 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d", |
| 6405 | ssl->in_msglen ) ); |
| 6406 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6407 | } |
| 6408 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6409 | if( ssl->in_msg[0] != 1 ) |
| 6410 | { |
| 6411 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x", |
| 6412 | ssl->in_msg[0] ) ); |
| 6413 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6414 | } |
| 6415 | |
| 6416 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6417 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6418 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
| 6419 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 6420 | { |
| 6421 | if( ssl->handshake == NULL ) |
| 6422 | { |
| 6423 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) ); |
| 6424 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 6425 | } |
| 6426 | |
| 6427 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) ); |
| 6428 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 6429 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6430 | #endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6431 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6432 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6433 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6434 | { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 6435 | if( ssl->in_msglen != 2 ) |
| 6436 | { |
| 6437 | /* Note: Standard allows for more than one 2 byte alert |
| 6438 | to be packed in a single message, but Mbed TLS doesn't |
| 6439 | currently support this. */ |
| 6440 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d", |
| 6441 | ssl->in_msglen ) ); |
| 6442 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6443 | } |
| 6444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6445 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6446 | ssl->in_msg[0], ssl->in_msg[1] ) ); |
| 6447 | |
| 6448 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 6449 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6450 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6451 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6452 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6453 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 6454 | ssl->in_msg[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6455 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6456 | } |
| 6457 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6458 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6459 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6460 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6461 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); |
| 6462 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6463 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 6464 | |
| 6465 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
| 6466 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6467 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) |
| 6468 | { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6469 | 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] | 6470 | /* Will be handled when trying to parse ServerHello */ |
| 6471 | return( 0 ); |
| 6472 | } |
| 6473 | #endif |
| 6474 | |
| 6475 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) |
| 6476 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 6477 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 6478 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6479 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 6480 | { |
| 6481 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) ); |
| 6482 | /* Will be handled in mbedtls_ssl_parse_certificate() */ |
| 6483 | return( 0 ); |
| 6484 | } |
| 6485 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 6486 | |
| 6487 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6488 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6489 | } |
| 6490 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6491 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6492 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6493 | { |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6494 | /* Drop unexpected ApplicationData records, |
| 6495 | * except at the beginning of renegotiations */ |
| 6496 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
| 6497 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 6498 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6499 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 6500 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6501 | #endif |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6502 | ) |
| 6503 | { |
| 6504 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); |
| 6505 | return( MBEDTLS_ERR_SSL_NON_FATAL ); |
| 6506 | } |
| 6507 | |
| 6508 | if( ssl->handshake != NULL && |
| 6509 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 6510 | { |
| 6511 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 6512 | } |
| 6513 | } |
Hanno Becker | 4a4af9f | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 6514 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6515 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6516 | return( 0 ); |
| 6517 | } |
| 6518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6519 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6520 | { |
| 6521 | int ret; |
| 6522 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6523 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 6524 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6525 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6526 | { |
| 6527 | return( ret ); |
| 6528 | } |
| 6529 | |
| 6530 | return( 0 ); |
| 6531 | } |
| 6532 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6533 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6534 | unsigned char level, |
| 6535 | unsigned char message ) |
| 6536 | { |
| 6537 | int ret; |
| 6538 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 6539 | if( ssl == NULL || ssl->conf == NULL ) |
| 6540 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6541 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6542 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6543 | 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] | 6544 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6545 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6546 | ssl->out_msglen = 2; |
| 6547 | ssl->out_msg[0] = level; |
| 6548 | ssl->out_msg[1] = message; |
| 6549 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 6550 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6551 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6552 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6553 | return( ret ); |
| 6554 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6555 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6556 | |
| 6557 | return( 0 ); |
| 6558 | } |
| 6559 | |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6560 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6561 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 6562 | { |
| 6563 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6564 | if( session->peer_cert != NULL ) |
| 6565 | { |
| 6566 | mbedtls_x509_crt_free( session->peer_cert ); |
| 6567 | mbedtls_free( session->peer_cert ); |
| 6568 | session->peer_cert = NULL; |
| 6569 | } |
| 6570 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6571 | if( session->peer_cert_digest != NULL ) |
| 6572 | { |
| 6573 | /* Zeroization is not necessary. */ |
| 6574 | mbedtls_free( session->peer_cert_digest ); |
| 6575 | session->peer_cert_digest = NULL; |
| 6576 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 6577 | session->peer_cert_digest_len = 0; |
| 6578 | } |
| 6579 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6580 | } |
| 6581 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 6582 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6583 | /* |
| 6584 | * Handshake functions |
| 6585 | */ |
Hanno Becker | 2148993 | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6586 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6587 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6588 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6589 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6590 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6591 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6592 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6593 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6594 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6595 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6596 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6597 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6598 | ssl->state++; |
| 6599 | return( 0 ); |
| 6600 | } |
| 6601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6602 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6603 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6604 | } |
| 6605 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6606 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6607 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6608 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6609 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6610 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6611 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6612 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6613 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6614 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6615 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6616 | ssl->state++; |
| 6617 | return( 0 ); |
| 6618 | } |
| 6619 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6620 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6621 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6622 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6623 | |
Hanno Becker | 2148993 | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6624 | #else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6625 | /* Some certificate support -> implement write and parse */ |
| 6626 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6627 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6628 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6629 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6630 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6631 | const mbedtls_x509_crt *crt; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6632 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6633 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6634 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6635 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6636 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6637 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6638 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6639 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6640 | ssl->state++; |
| 6641 | return( 0 ); |
| 6642 | } |
| 6643 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6644 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6645 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6646 | { |
| 6647 | if( ssl->client_auth == 0 ) |
| 6648 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6649 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6650 | ssl->state++; |
| 6651 | return( 0 ); |
| 6652 | } |
| 6653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6654 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6655 | /* |
| 6656 | * If using SSLv3 and got no cert, send an Alert message |
| 6657 | * (otherwise an empty Certificate message will be sent). |
| 6658 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6659 | if( mbedtls_ssl_own_cert( ssl ) == NULL && |
| 6660 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6661 | { |
| 6662 | ssl->out_msglen = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6663 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
| 6664 | ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING; |
| 6665 | ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6666 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6667 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6668 | goto write_msg; |
| 6669 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6670 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6671 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6672 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6673 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6674 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6675 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6676 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6677 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6678 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); |
| 6679 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6680 | } |
| 6681 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6682 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6683 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6684 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6685 | |
| 6686 | /* |
| 6687 | * 0 . 0 handshake type |
| 6688 | * 1 . 3 handshake length |
| 6689 | * 4 . 6 length of all certs |
| 6690 | * 7 . 9 length of cert. 1 |
| 6691 | * 10 . n-1 peer certificate |
| 6692 | * n . n+2 length of cert. 2 |
| 6693 | * n+3 . ... upper level cert, etc. |
| 6694 | */ |
| 6695 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6696 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6697 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 6698 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6699 | { |
| 6700 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6701 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6702 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6703 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6704 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6705 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6706 | } |
| 6707 | |
| 6708 | ssl->out_msg[i ] = (unsigned char)( n >> 16 ); |
| 6709 | ssl->out_msg[i + 1] = (unsigned char)( n >> 8 ); |
| 6710 | ssl->out_msg[i + 2] = (unsigned char)( n ); |
| 6711 | |
| 6712 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6713 | i += n; crt = crt->next; |
| 6714 | } |
| 6715 | |
| 6716 | ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 ); |
| 6717 | ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 ); |
| 6718 | ssl->out_msg[6] = (unsigned char)( ( i - 7 ) ); |
| 6719 | |
| 6720 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6721 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6722 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6723 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 6724 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6725 | write_msg: |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6726 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6727 | |
| 6728 | ssl->state++; |
| 6729 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6730 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6731 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6732 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6733 | return( ret ); |
| 6734 | } |
| 6735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6736 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6737 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 6738 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6739 | } |
| 6740 | |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6741 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6742 | |
| 6743 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6744 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6745 | unsigned char *crt_buf, |
| 6746 | size_t crt_buf_len ) |
| 6747 | { |
| 6748 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6749 | |
| 6750 | if( peer_crt == NULL ) |
| 6751 | return( -1 ); |
| 6752 | |
| 6753 | if( peer_crt->raw.len != crt_buf_len ) |
| 6754 | return( -1 ); |
| 6755 | |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6756 | return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6757 | } |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6758 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6759 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6760 | unsigned char *crt_buf, |
| 6761 | size_t crt_buf_len ) |
| 6762 | { |
| 6763 | int ret; |
| 6764 | unsigned char const * const peer_cert_digest = |
| 6765 | ssl->session->peer_cert_digest; |
| 6766 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6767 | ssl->session->peer_cert_digest_type; |
| 6768 | mbedtls_md_info_t const * const digest_info = |
| 6769 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6770 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6771 | size_t digest_len; |
| 6772 | |
| 6773 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6774 | return( -1 ); |
| 6775 | |
| 6776 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6777 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6778 | return( -1 ); |
| 6779 | |
| 6780 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6781 | if( ret != 0 ) |
| 6782 | return( -1 ); |
| 6783 | |
| 6784 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6785 | } |
| 6786 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6787 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6788 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6789 | /* |
| 6790 | * Once the certificate message is read, parse it into a cert chain and |
| 6791 | * perform basic checks, but leave actual verification to the caller |
| 6792 | */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6793 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6794 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6795 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6796 | int ret; |
| 6797 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6798 | int crt_cnt=0; |
| 6799 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6800 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 6801 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6802 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6803 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6804 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6805 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6806 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6807 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6808 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6809 | } |
| 6810 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6811 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || |
| 6812 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6813 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6814 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6815 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6816 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6817 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6818 | } |
| 6819 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6820 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6821 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6822 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6823 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6824 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6825 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6826 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6827 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6828 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6829 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6830 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6831 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6832 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6833 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6834 | } |
| 6835 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6836 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6837 | i += 3; |
| 6838 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6839 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6840 | while( i < ssl->in_hslen ) |
| 6841 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6842 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6843 | if ( i + 3 > ssl->in_hslen ) { |
| 6844 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6845 | mbedtls_ssl_send_alert_message( ssl, |
| 6846 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6847 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6848 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 6849 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6850 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6851 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6852 | if( ssl->in_msg[i] != 0 ) |
| 6853 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6854 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6855 | mbedtls_ssl_send_alert_message( ssl, |
| 6856 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6857 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6858 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6859 | } |
| 6860 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6861 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6862 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6863 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6864 | i += 3; |
| 6865 | |
| 6866 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6867 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6868 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6869 | mbedtls_ssl_send_alert_message( ssl, |
| 6870 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6871 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6872 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6873 | } |
| 6874 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6875 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6876 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6877 | if( crt_cnt++ == 0 && |
| 6878 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6879 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6880 | { |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6881 | /* During client-side renegotiation, check that the server's |
| 6882 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6883 | * mitigating the triple handshake attack. On success, reuse |
| 6884 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6885 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6886 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6887 | &ssl->in_msg[i], |
| 6888 | n ) != 0 ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6889 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6890 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6891 | mbedtls_ssl_send_alert_message( ssl, |
| 6892 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6893 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6894 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6895 | } |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6896 | |
| 6897 | /* Now we can safely free the original chain. */ |
| 6898 | ssl_clear_peer_cert( ssl->session ); |
| 6899 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6900 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6901 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6902 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6903 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6904 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6905 | #else |
Hanno Becker | 353a6f0 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 6906 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6907 | * it in-place from the input buffer instead of making a copy. */ |
| 6908 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6909 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6910 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6911 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6912 | case 0: /*ok*/ |
| 6913 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6914 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6915 | prior certificate was already trusted. */ |
| 6916 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6917 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6918 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6919 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6920 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6921 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6922 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6923 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6924 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6925 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6926 | default: |
| 6927 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6928 | crt_parse_der_failed: |
| 6929 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6930 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6931 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6932 | } |
| 6933 | |
| 6934 | i += n; |
| 6935 | } |
| 6936 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6937 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6938 | return( 0 ); |
| 6939 | } |
| 6940 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6941 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6942 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6943 | { |
| 6944 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6945 | return( -1 ); |
| 6946 | |
| 6947 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 6948 | /* |
| 6949 | * Check if the client sent an empty certificate |
| 6950 | */ |
| 6951 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
| 6952 | { |
| 6953 | if( ssl->in_msglen == 2 && |
| 6954 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 6955 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6956 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 6957 | { |
| 6958 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); |
| 6959 | return( 0 ); |
| 6960 | } |
| 6961 | |
| 6962 | return( -1 ); |
| 6963 | } |
| 6964 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 6965 | |
| 6966 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 6967 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6968 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6969 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6970 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6971 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6972 | { |
| 6973 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 6974 | return( 0 ); |
| 6975 | } |
| 6976 | |
| 6977 | return( -1 ); |
| 6978 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 6979 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6980 | } |
| 6981 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6982 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6983 | /* Check if a certificate message is expected. |
| 6984 | * Return either |
| 6985 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6986 | * - SSL_CERTIFICATE_SKIP |
| 6987 | * indicating whether a Certificate message is expected or not. |
| 6988 | */ |
| 6989 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6990 | #define SSL_CERTIFICATE_SKIP 1 |
| 6991 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6992 | int authmode ) |
| 6993 | { |
| 6994 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6995 | ssl->handshake->ciphersuite_info; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6996 | |
| 6997 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6998 | return( SSL_CERTIFICATE_SKIP ); |
| 6999 | |
| 7000 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7001 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7002 | { |
| 7003 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 7004 | return( SSL_CERTIFICATE_SKIP ); |
| 7005 | |
| 7006 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 7007 | { |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7008 | ssl->session_negotiate->verify_result = |
| 7009 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 7010 | return( SSL_CERTIFICATE_SKIP ); |
| 7011 | } |
| 7012 | } |
Hanno Becker | 84d9d27 | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 7013 | #else |
| 7014 | ((void) authmode); |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7015 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7016 | |
| 7017 | return( SSL_CERTIFICATE_EXPECTED ); |
| 7018 | } |
| 7019 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7020 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 7021 | int authmode, |
| 7022 | mbedtls_x509_crt *chain, |
| 7023 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7024 | { |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7025 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7026 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 7027 | ssl->handshake->ciphersuite_info; |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7028 | int have_ca_chain = 0; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7029 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 7030 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 7031 | void *p_vrfy; |
| 7032 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7033 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 7034 | return( 0 ); |
| 7035 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 7036 | if( ssl->f_vrfy != NULL ) |
| 7037 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 7038 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 7039 | f_vrfy = ssl->f_vrfy; |
| 7040 | p_vrfy = ssl->p_vrfy; |
| 7041 | } |
| 7042 | else |
| 7043 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 7044 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 7045 | f_vrfy = ssl->conf->f_vrfy; |
| 7046 | p_vrfy = ssl->conf->p_vrfy; |
| 7047 | } |
| 7048 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7049 | /* |
| 7050 | * Main check: verify certificate |
| 7051 | */ |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7052 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 7053 | if( ssl->conf->f_ca_cb != NULL ) |
| 7054 | { |
| 7055 | ((void) rs_ctx); |
| 7056 | have_ca_chain = 1; |
| 7057 | |
| 7058 | 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] | 7059 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7060 | chain, |
| 7061 | ssl->conf->f_ca_cb, |
| 7062 | ssl->conf->p_ca_cb, |
| 7063 | ssl->conf->cert_profile, |
| 7064 | ssl->hostname, |
| 7065 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 7066 | f_vrfy, p_vrfy ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7067 | } |
| 7068 | else |
| 7069 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 7070 | { |
| 7071 | mbedtls_x509_crt *ca_chain; |
| 7072 | mbedtls_x509_crl *ca_crl; |
| 7073 | |
| 7074 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7075 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 7076 | { |
| 7077 | ca_chain = ssl->handshake->sni_ca_chain; |
| 7078 | ca_crl = ssl->handshake->sni_ca_crl; |
| 7079 | } |
| 7080 | else |
| 7081 | #endif |
| 7082 | { |
| 7083 | ca_chain = ssl->conf->ca_chain; |
| 7084 | ca_crl = ssl->conf->ca_crl; |
| 7085 | } |
| 7086 | |
| 7087 | if( ca_chain != NULL ) |
| 7088 | have_ca_chain = 1; |
| 7089 | |
| 7090 | ret = mbedtls_x509_crt_verify_restartable( |
| 7091 | chain, |
| 7092 | ca_chain, ca_crl, |
| 7093 | ssl->conf->cert_profile, |
| 7094 | ssl->hostname, |
| 7095 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 7096 | f_vrfy, p_vrfy, rs_ctx ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7097 | } |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7098 | |
| 7099 | if( ret != 0 ) |
| 7100 | { |
| 7101 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 7102 | } |
| 7103 | |
| 7104 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7105 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 7106 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 7107 | #endif |
| 7108 | |
| 7109 | /* |
| 7110 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 7111 | */ |
| 7112 | |
| 7113 | #if defined(MBEDTLS_ECP_C) |
| 7114 | { |
| 7115 | const mbedtls_pk_context *pk = &chain->pk; |
| 7116 | |
| 7117 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 7118 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 7119 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 7120 | { |
| 7121 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 7122 | |
| 7123 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 7124 | if( ret == 0 ) |
| 7125 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
| 7126 | } |
| 7127 | } |
| 7128 | #endif /* MBEDTLS_ECP_C */ |
| 7129 | |
| 7130 | if( mbedtls_ssl_check_cert_usage( chain, |
| 7131 | ciphersuite_info, |
| 7132 | ! ssl->conf->endpoint, |
| 7133 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 7134 | { |
| 7135 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 7136 | if( ret == 0 ) |
| 7137 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
| 7138 | } |
| 7139 | |
| 7140 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 7141 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 7142 | * with details encoded in the verification flags. All other kinds |
| 7143 | * of error codes, including those from the user provided f_vrfy |
| 7144 | * functions, are treated as fatal and lead to a failure of |
| 7145 | * ssl_parse_certificate even if verification was optional. */ |
| 7146 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 7147 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 7148 | ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) |
| 7149 | { |
| 7150 | ret = 0; |
| 7151 | } |
| 7152 | |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7153 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7154 | { |
| 7155 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 7156 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 7157 | } |
| 7158 | |
| 7159 | if( ret != 0 ) |
| 7160 | { |
| 7161 | uint8_t alert; |
| 7162 | |
| 7163 | /* The certificate may have been rejected for several reasons. |
| 7164 | Pick one and send the corresponding alert. Which alert to send |
| 7165 | may be a subject of debate in some cases. */ |
| 7166 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 7167 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 7168 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 7169 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 7170 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 7171 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7172 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 7173 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7174 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 7175 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7176 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 7177 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7178 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 7179 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7180 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 7181 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 7182 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 7183 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 7184 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 7185 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 7186 | else |
| 7187 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 7188 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7189 | alert ); |
| 7190 | } |
| 7191 | |
| 7192 | #if defined(MBEDTLS_DEBUG_C) |
| 7193 | if( ssl->session_negotiate->verify_result != 0 ) |
| 7194 | { |
| 7195 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", |
| 7196 | ssl->session_negotiate->verify_result ) ); |
| 7197 | } |
| 7198 | else |
| 7199 | { |
| 7200 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 7201 | } |
| 7202 | #endif /* MBEDTLS_DEBUG_C */ |
| 7203 | |
| 7204 | return( ret ); |
| 7205 | } |
| 7206 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7207 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7208 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 7209 | unsigned char *start, size_t len ) |
| 7210 | { |
| 7211 | int ret; |
| 7212 | /* Remember digest of the peer's end-CRT. */ |
| 7213 | ssl->session_negotiate->peer_cert_digest = |
| 7214 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 7215 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 7216 | { |
| 7217 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7218 | sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) ); |
| 7219 | mbedtls_ssl_send_alert_message( ssl, |
| 7220 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7221 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7222 | |
| 7223 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7224 | } |
| 7225 | |
| 7226 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 7227 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 7228 | start, len, |
| 7229 | ssl->session_negotiate->peer_cert_digest ); |
| 7230 | |
| 7231 | ssl->session_negotiate->peer_cert_digest_type = |
| 7232 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7233 | ssl->session_negotiate->peer_cert_digest_len = |
| 7234 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7235 | |
| 7236 | return( ret ); |
| 7237 | } |
| 7238 | |
| 7239 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 7240 | unsigned char *start, size_t len ) |
| 7241 | { |
| 7242 | unsigned char *end = start + len; |
| 7243 | int ret; |
| 7244 | |
| 7245 | /* Make a copy of the peer's raw public key. */ |
| 7246 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 7247 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 7248 | &ssl->handshake->peer_pubkey ); |
| 7249 | if( ret != 0 ) |
| 7250 | { |
| 7251 | /* We should have parsed the public key before. */ |
| 7252 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7253 | } |
| 7254 | |
| 7255 | return( 0 ); |
| 7256 | } |
| 7257 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7258 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7259 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 7260 | { |
| 7261 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7262 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7263 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7264 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7265 | ? ssl->handshake->sni_authmode |
| 7266 | : ssl->conf->authmode; |
| 7267 | #else |
| 7268 | const int authmode = ssl->conf->authmode; |
| 7269 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7270 | void *rs_ctx = NULL; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7271 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7272 | |
| 7273 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 7274 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7275 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 7276 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7277 | { |
| 7278 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7279 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7280 | } |
| 7281 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7282 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7283 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7284 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7285 | { |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7286 | chain = ssl->handshake->ecrs_peer_cert; |
| 7287 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7288 | goto crt_verify; |
| 7289 | } |
| 7290 | #endif |
| 7291 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 7292 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7293 | { |
| 7294 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7295 | let it decide whether to alert. */ |
| 7296 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7297 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7298 | } |
| 7299 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7300 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7301 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 7302 | { |
| 7303 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7304 | |
| 7305 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7306 | ret = 0; |
| 7307 | else |
| 7308 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7309 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7310 | goto exit; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7311 | } |
| 7312 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7313 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7314 | /* Clear existing peer CRT structure in case we tried to |
| 7315 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7316 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7317 | |
| 7318 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7319 | if( chain == NULL ) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7320 | { |
| 7321 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7322 | sizeof( mbedtls_x509_crt ) ) ); |
| 7323 | mbedtls_ssl_send_alert_message( ssl, |
| 7324 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7325 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7326 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7327 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7328 | goto exit; |
| 7329 | } |
| 7330 | mbedtls_x509_crt_init( chain ); |
| 7331 | |
| 7332 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7333 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7334 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7335 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7336 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7337 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7338 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7339 | |
| 7340 | crt_verify: |
| 7341 | if( ssl->handshake->ecrs_enabled) |
| 7342 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 7343 | #endif |
| 7344 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7345 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7346 | chain, rs_ctx ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7347 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7348 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7349 | |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7350 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7351 | { |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7352 | unsigned char *crt_start, *pk_start; |
| 7353 | size_t crt_len, pk_len; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7354 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7355 | /* We parse the CRT chain without copying, so |
| 7356 | * these pointers point into the input buffer, |
| 7357 | * and are hence still valid after freeing the |
| 7358 | * CRT chain. */ |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7359 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7360 | crt_start = chain->raw.p; |
| 7361 | crt_len = chain->raw.len; |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7362 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7363 | pk_start = chain->pk_raw.p; |
| 7364 | pk_len = chain->pk_raw.len; |
| 7365 | |
| 7366 | /* Free the CRT structures before computing |
| 7367 | * digest and copying the peer's public key. */ |
| 7368 | mbedtls_x509_crt_free( chain ); |
| 7369 | mbedtls_free( chain ); |
| 7370 | chain = NULL; |
| 7371 | |
| 7372 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7373 | if( ret != 0 ) |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7374 | goto exit; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7375 | |
| 7376 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 7377 | if( ret != 0 ) |
| 7378 | goto exit; |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7379 | } |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7380 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7381 | /* Pass ownership to session structure. */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7382 | ssl->session_negotiate->peer_cert = chain; |
| 7383 | chain = NULL; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7384 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7385 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7386 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7387 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7388 | exit: |
| 7389 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7390 | if( ret == 0 ) |
| 7391 | ssl->state++; |
| 7392 | |
| 7393 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7394 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7395 | { |
| 7396 | ssl->handshake->ecrs_peer_cert = chain; |
| 7397 | chain = NULL; |
| 7398 | } |
| 7399 | #endif |
| 7400 | |
| 7401 | if( chain != NULL ) |
| 7402 | { |
| 7403 | mbedtls_x509_crt_free( chain ); |
| 7404 | mbedtls_free( chain ); |
| 7405 | } |
| 7406 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7407 | return( ret ); |
| 7408 | } |
Hanno Becker | 2148993 | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 7409 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7410 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7411 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7412 | { |
| 7413 | int ret; |
| 7414 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7415 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7417 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7418 | ssl->out_msglen = 1; |
| 7419 | ssl->out_msg[0] = 1; |
| 7420 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7421 | ssl->state++; |
| 7422 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7423 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7424 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7425 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7426 | return( ret ); |
| 7427 | } |
| 7428 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7429 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7430 | |
| 7431 | return( 0 ); |
| 7432 | } |
| 7433 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7434 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7435 | { |
| 7436 | int ret; |
| 7437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7438 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7439 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 7440 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7441 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7442 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7443 | return( ret ); |
| 7444 | } |
| 7445 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7446 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7447 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7448 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7449 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7450 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7451 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7452 | } |
| 7453 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 7454 | /* CCS records are only accepted if they have length 1 and content '1', |
| 7455 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7456 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7457 | /* |
| 7458 | * Switch to our negotiated transform and session parameters for inbound |
| 7459 | * data. |
| 7460 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7461 | 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] | 7462 | ssl->transform_in = ssl->transform_negotiate; |
| 7463 | ssl->session_in = ssl->session_negotiate; |
| 7464 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7465 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7466 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7467 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7468 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7469 | ssl_dtls_replay_reset( ssl ); |
| 7470 | #endif |
| 7471 | |
| 7472 | /* Increment epoch */ |
| 7473 | if( ++ssl->in_epoch == 0 ) |
| 7474 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7475 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7476 | /* This is highly unlikely to happen for legitimate reasons, so |
| 7477 | 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] | 7478 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7479 | } |
| 7480 | } |
| 7481 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7482 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7483 | memset( ssl->in_ctr, 0, 8 ); |
| 7484 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7485 | ssl_update_in_pointers( ssl ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7486 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7487 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7488 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7489 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7490 | 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] | 7491 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7492 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7493 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7494 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7495 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7496 | } |
| 7497 | } |
| 7498 | #endif |
| 7499 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7500 | ssl->state++; |
| 7501 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7502 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7503 | |
| 7504 | return( 0 ); |
| 7505 | } |
| 7506 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7507 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 7508 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7509 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 7510 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7511 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7512 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7513 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7514 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7515 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7516 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7517 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7518 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7519 | #if defined(MBEDTLS_SHA512_C) |
| 7520 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7521 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 7522 | else |
| 7523 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7524 | #if defined(MBEDTLS_SHA256_C) |
| 7525 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7526 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7527 | else |
| 7528 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7529 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 7530 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7531 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7532 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 7533 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7534 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 7535 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7536 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7537 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7538 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7539 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7540 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 ); |
| 7541 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7542 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7543 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7544 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7545 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 7546 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7547 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 7548 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7549 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7550 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7551 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7552 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7553 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 7554 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7555 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7556 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7557 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7558 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7559 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7560 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7561 | } |
| 7562 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7563 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7564 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7565 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7566 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7567 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7568 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7569 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7570 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7571 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7572 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7573 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7574 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 7575 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7576 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7577 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7578 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7579 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7580 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7581 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7582 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7583 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7584 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7585 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7586 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7587 | } |
| 7588 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7589 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7590 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7591 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7592 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7593 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7594 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7595 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7596 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7597 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7598 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7599 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7600 | #if defined(MBEDTLS_SHA256_C) |
| 7601 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7602 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7603 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7604 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7605 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 7606 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7607 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7608 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7609 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7610 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7611 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7612 | #if defined(MBEDTLS_SHA512_C) |
| 7613 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7614 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7615 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7616 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7617 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7618 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7619 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7620 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7621 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7622 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7623 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7624 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7625 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7626 | static void ssl_calc_finished_ssl( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7627 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7628 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7629 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7630 | mbedtls_md5_context md5; |
| 7631 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7632 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7633 | unsigned char padbuf[48]; |
| 7634 | unsigned char md5sum[16]; |
| 7635 | unsigned char sha1sum[20]; |
| 7636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7637 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7638 | if( !session ) |
| 7639 | session = ssl->session; |
| 7640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7641 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7642 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7643 | mbedtls_md5_init( &md5 ); |
| 7644 | mbedtls_sha1_init( &sha1 ); |
| 7645 | |
| 7646 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7647 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7648 | |
| 7649 | /* |
| 7650 | * SSLv3: |
| 7651 | * hash = |
| 7652 | * MD5( master + pad2 + |
| 7653 | * MD5( handshake + sender + master + pad1 ) ) |
| 7654 | * + SHA1( master + pad2 + |
| 7655 | * SHA1( handshake + sender + master + pad1 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7656 | */ |
| 7657 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7658 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7659 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7660 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7661 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7662 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7663 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7664 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7665 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7666 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7667 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7668 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7669 | : "SRVR"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7670 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7671 | memset( padbuf, 0x36, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7672 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7673 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); |
| 7674 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7675 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7676 | mbedtls_md5_finish_ret( &md5, md5sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7677 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7678 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); |
| 7679 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7680 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); |
| 7681 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7682 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7683 | memset( padbuf, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7684 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7685 | mbedtls_md5_starts_ret( &md5 ); |
| 7686 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7687 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7688 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); |
| 7689 | mbedtls_md5_finish_ret( &md5, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7690 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7691 | mbedtls_sha1_starts_ret( &sha1 ); |
| 7692 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7693 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); |
| 7694 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); |
| 7695 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7696 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7697 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7698 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7699 | mbedtls_md5_free( &md5 ); |
| 7700 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7701 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7702 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7703 | mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) ); |
| 7704 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7705 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7706 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7707 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7708 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7709 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7710 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7711 | static void ssl_calc_finished_tls( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7712 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7713 | { |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7714 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7715 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7716 | mbedtls_md5_context md5; |
| 7717 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7718 | unsigned char padbuf[36]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7719 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7720 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7721 | if( !session ) |
| 7722 | session = ssl->session; |
| 7723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7724 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7725 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7726 | mbedtls_md5_init( &md5 ); |
| 7727 | mbedtls_sha1_init( &sha1 ); |
| 7728 | |
| 7729 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7730 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7731 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7732 | /* |
| 7733 | * TLSv1: |
| 7734 | * hash = PRF( master, finished_label, |
| 7735 | * MD5( handshake ) + SHA1( handshake ) )[0..11] |
| 7736 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7737 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7738 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7739 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7740 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7741 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7742 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7743 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7744 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7745 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7746 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7747 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7748 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7749 | ? "client finished" |
| 7750 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7751 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7752 | mbedtls_md5_finish_ret( &md5, padbuf ); |
| 7753 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7754 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7755 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7756 | padbuf, 36, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7757 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7758 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7759 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7760 | mbedtls_md5_free( &md5 ); |
| 7761 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7762 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7763 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7764 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7765 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7766 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7767 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7768 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7769 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7770 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7771 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7772 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7773 | { |
| 7774 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7775 | const char *sender; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7776 | unsigned char padbuf[32]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7777 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7778 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7779 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7780 | psa_status_t status; |
| 7781 | #else |
| 7782 | mbedtls_sha256_context sha256; |
| 7783 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7784 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7785 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7786 | if( !session ) |
| 7787 | session = ssl->session; |
| 7788 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7789 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7790 | ? "client finished" |
| 7791 | : "server finished"; |
| 7792 | |
| 7793 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7794 | sha256_psa = psa_hash_operation_init(); |
| 7795 | |
| 7796 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 7797 | |
| 7798 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 7799 | if( status != PSA_SUCCESS ) |
| 7800 | { |
| 7801 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7802 | return; |
| 7803 | } |
| 7804 | |
| 7805 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7806 | if( status != PSA_SUCCESS ) |
| 7807 | { |
| 7808 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7809 | return; |
| 7810 | } |
| 7811 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 7812 | #else |
| 7813 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7814 | mbedtls_sha256_init( &sha256 ); |
| 7815 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7816 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7817 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7818 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7819 | |
| 7820 | /* |
| 7821 | * TLSv1.2: |
| 7822 | * hash = PRF( master, finished_label, |
| 7823 | * Hash( handshake ) )[0.11] |
| 7824 | */ |
| 7825 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7826 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7827 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7828 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7829 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7830 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7831 | mbedtls_sha256_finish_ret( &sha256, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7832 | mbedtls_sha256_free( &sha256 ); |
| 7833 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7834 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7835 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7836 | padbuf, 32, buf, len ); |
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 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7839 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7840 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7841 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7842 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7843 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7844 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7845 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7846 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7847 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7848 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7849 | { |
| 7850 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7851 | const char *sender; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7852 | unsigned char padbuf[48]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7853 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7854 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7855 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7856 | psa_status_t status; |
| 7857 | #else |
| 7858 | mbedtls_sha512_context sha512; |
| 7859 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7860 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7861 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7862 | if( !session ) |
| 7863 | session = ssl->session; |
| 7864 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7865 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7866 | ? "client finished" |
| 7867 | : "server finished"; |
| 7868 | |
| 7869 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7870 | sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7871 | |
| 7872 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7873 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7874 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7875 | if( status != PSA_SUCCESS ) |
| 7876 | { |
| 7877 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7878 | return; |
| 7879 | } |
| 7880 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7881 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7882 | if( status != PSA_SUCCESS ) |
| 7883 | { |
| 7884 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7885 | return; |
| 7886 | } |
| 7887 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7888 | #else |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7889 | mbedtls_sha512_init( &sha512 ); |
| 7890 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7891 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7892 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7893 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7894 | |
| 7895 | /* |
| 7896 | * TLSv1.2: |
| 7897 | * hash = PRF( master, finished_label, |
| 7898 | * Hash( handshake ) )[0.11] |
| 7899 | */ |
| 7900 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7901 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7902 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7903 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7904 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7905 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7906 | mbedtls_sha512_finish_ret( &sha512, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7907 | mbedtls_sha512_free( &sha512 ); |
| 7908 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7909 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7910 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7911 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7912 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7913 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7914 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7915 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7916 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7917 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7918 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7919 | #endif /* MBEDTLS_SHA512_C */ |
| 7920 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7921 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7922 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7923 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7924 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7925 | |
| 7926 | /* |
| 7927 | * Free our handshake params |
| 7928 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 7929 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7930 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7931 | ssl->handshake = NULL; |
| 7932 | |
| 7933 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7934 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7935 | */ |
| 7936 | if( ssl->transform ) |
| 7937 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7938 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7939 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7940 | } |
| 7941 | ssl->transform = ssl->transform_negotiate; |
| 7942 | ssl->transform_negotiate = NULL; |
| 7943 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7944 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7945 | } |
| 7946 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7947 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7948 | { |
| 7949 | int resume = ssl->handshake->resume; |
| 7950 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7951 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7952 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7953 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7954 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7955 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7956 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7957 | ssl->renego_records_seen = 0; |
| 7958 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7959 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7960 | |
| 7961 | /* |
| 7962 | * Free the previous session and switch in the current one |
| 7963 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7964 | if( ssl->session ) |
| 7965 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7966 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 7967 | /* RFC 7366 3.1: keep the EtM state */ |
| 7968 | ssl->session_negotiate->encrypt_then_mac = |
| 7969 | ssl->session->encrypt_then_mac; |
| 7970 | #endif |
| 7971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7972 | mbedtls_ssl_session_free( ssl->session ); |
| 7973 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7974 | } |
| 7975 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7976 | ssl->session_negotiate = NULL; |
| 7977 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7978 | /* |
| 7979 | * Add cache entry |
| 7980 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7981 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 7982 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7983 | resume == 0 ) |
| 7984 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7985 | 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] | 7986 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7987 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7988 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7989 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7990 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7991 | ssl->handshake->flight != NULL ) |
| 7992 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 7993 | /* Cancel handshake timer */ |
| 7994 | ssl_set_timer( ssl, 0 ); |
| 7995 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7996 | /* Keep last flight around in case we need to resend it: |
| 7997 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7998 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7999 | } |
| 8000 | else |
| 8001 | #endif |
| 8002 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 8003 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8004 | ssl->state++; |
| 8005 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8006 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8007 | } |
| 8008 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8009 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8010 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8011 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8012 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8013 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8014 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8015 | ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 8016 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8017 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8018 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 8019 | /* |
| 8020 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 8021 | * may define some other value. Currently (early 2016), no defined |
| 8022 | * ciphersuite does this (and this is unlikely to change as activity has |
| 8023 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 8024 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8025 | hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8026 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8027 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8028 | ssl->verify_data_len = hash_len; |
| 8029 | 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] | 8030 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8031 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8032 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8033 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 8034 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8035 | |
| 8036 | /* |
| 8037 | * In case of session resuming, invert the client and server |
| 8038 | * ChangeCipherSpec messages order. |
| 8039 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8040 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8041 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8042 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8043 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8044 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8045 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8046 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8047 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8048 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8049 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8050 | } |
| 8051 | else |
| 8052 | ssl->state++; |
| 8053 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8054 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 8055 | * Switch to our negotiated transform and session parameters for outbound |
| 8056 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8057 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8058 | 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] | 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 | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8062 | { |
| 8063 | unsigned char i; |
| 8064 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8065 | /* Remember current epoch settings for resending */ |
| 8066 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8067 | 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] | 8068 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8069 | /* Set sequence_number to zero */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8070 | memset( ssl->cur_out_ctr + 2, 0, 6 ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8071 | |
| 8072 | /* Increment epoch */ |
| 8073 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8074 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8075 | break; |
| 8076 | |
| 8077 | /* The loop goes to its end iff the counter is wrapping */ |
| 8078 | if( i == 0 ) |
| 8079 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8080 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 8081 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8082 | } |
| 8083 | } |
| 8084 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8085 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8086 | memset( ssl->cur_out_ctr, 0, 8 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8087 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8088 | ssl->transform_out = ssl->transform_negotiate; |
| 8089 | ssl->session_out = ssl->session_negotiate; |
| 8090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8091 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 8092 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 8093 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8094 | 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] | 8095 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8096 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 8097 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 8098 | } |
| 8099 | } |
| 8100 | #endif |
| 8101 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8102 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8103 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8104 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 8105 | #endif |
| 8106 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 8107 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8108 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 8109 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8110 | return( ret ); |
| 8111 | } |
| 8112 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 8113 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8114 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8115 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 8116 | { |
| 8117 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 8118 | return( ret ); |
| 8119 | } |
| 8120 | #endif |
| 8121 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8122 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8123 | |
| 8124 | return( 0 ); |
| 8125 | } |
| 8126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8127 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8128 | #define SSL_MAX_HASH_LEN 36 |
| 8129 | #else |
| 8130 | #define SSL_MAX_HASH_LEN 12 |
| 8131 | #endif |
| 8132 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8133 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8134 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 8135 | int ret; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8136 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8137 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8139 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8140 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8141 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8142 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 8143 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8144 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8145 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8146 | return( ret ); |
| 8147 | } |
| 8148 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8149 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8150 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8151 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 8152 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8153 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8154 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8155 | } |
| 8156 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8157 | /* 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] | 8158 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 8159 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8160 | hash_len = 36; |
| 8161 | else |
| 8162 | #endif |
| 8163 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8164 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8165 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || |
| 8166 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8167 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8168 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 8169 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8170 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8171 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8172 | } |
| 8173 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8174 | 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] | 8175 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8176 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8177 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 8178 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8179 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8180 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8181 | } |
| 8182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8183 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8184 | ssl->verify_data_len = hash_len; |
| 8185 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8186 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8187 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8188 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8189 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8190 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8191 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8192 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8193 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8194 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8195 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8196 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8197 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8198 | } |
| 8199 | else |
| 8200 | ssl->state++; |
| 8201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8202 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8203 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8204 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8205 | #endif |
| 8206 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8207 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8208 | |
| 8209 | return( 0 ); |
| 8210 | } |
| 8211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8212 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8213 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8214 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8216 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 8217 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 8218 | mbedtls_md5_init( &handshake->fin_md5 ); |
| 8219 | mbedtls_sha1_init( &handshake->fin_sha1 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8220 | mbedtls_md5_starts_ret( &handshake->fin_md5 ); |
| 8221 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8222 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8223 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 8224 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8225 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8226 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 8227 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 8228 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8229 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8230 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8231 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8232 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8233 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8234 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 8235 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 8236 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8237 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8238 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8239 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8240 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8241 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8242 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8243 | |
| 8244 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 8245 | |
| 8246 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 8247 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 8248 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 8249 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8251 | #if defined(MBEDTLS_DHM_C) |
| 8252 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8253 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8254 | #if defined(MBEDTLS_ECDH_C) |
| 8255 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8256 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8257 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8258 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 8259 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8260 | handshake->ecjpake_cache = NULL; |
| 8261 | handshake->ecjpake_cache_len = 0; |
| 8262 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8263 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8264 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 8265 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 8266 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 8267 | #endif |
| 8268 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8269 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8270 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 8271 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 8272 | |
| 8273 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 8274 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8275 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 8276 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8277 | } |
| 8278 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 8279 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8280 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8281 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 8282 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8283 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 8284 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 8285 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 8286 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8287 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 8288 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 8289 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8290 | } |
| 8291 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8292 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8293 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8294 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8295 | } |
| 8296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8297 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8298 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8299 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8300 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8301 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8302 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8303 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8304 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 8305 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8306 | |
| 8307 | /* |
| 8308 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 8309 | * Now allocate missing structures. |
| 8310 | */ |
| 8311 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8312 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8313 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8314 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8315 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8316 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8317 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8318 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8319 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8320 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 8321 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8322 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8323 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8324 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8325 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8326 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8327 | if( ssl->handshake == NULL || |
| 8328 | ssl->transform_negotiate == NULL || |
| 8329 | ssl->session_negotiate == NULL ) |
| 8330 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 8331 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8332 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8333 | mbedtls_free( ssl->handshake ); |
| 8334 | mbedtls_free( ssl->transform_negotiate ); |
| 8335 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8336 | |
| 8337 | ssl->handshake = NULL; |
| 8338 | ssl->transform_negotiate = NULL; |
| 8339 | ssl->session_negotiate = NULL; |
| 8340 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8341 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8342 | } |
| 8343 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8344 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8345 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 8346 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 8347 | ssl_handshake_params_init( ssl->handshake ); |
| 8348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8349 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8350 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8351 | { |
| 8352 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8353 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8354 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 8355 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 8356 | else |
| 8357 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8358 | |
| 8359 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8360 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8361 | #endif |
| 8362 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8363 | return( 0 ); |
| 8364 | } |
| 8365 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8366 | #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] | 8367 | /* Dummy cookie callbacks for defaults */ |
| 8368 | static int ssl_cookie_write_dummy( void *ctx, |
| 8369 | unsigned char **p, unsigned char *end, |
| 8370 | const unsigned char *cli_id, size_t cli_id_len ) |
| 8371 | { |
| 8372 | ((void) ctx); |
| 8373 | ((void) p); |
| 8374 | ((void) end); |
| 8375 | ((void) cli_id); |
| 8376 | ((void) cli_id_len); |
| 8377 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8378 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8379 | } |
| 8380 | |
| 8381 | static int ssl_cookie_check_dummy( void *ctx, |
| 8382 | const unsigned char *cookie, size_t cookie_len, |
| 8383 | const unsigned char *cli_id, size_t cli_id_len ) |
| 8384 | { |
| 8385 | ((void) ctx); |
| 8386 | ((void) cookie); |
| 8387 | ((void) cookie_len); |
| 8388 | ((void) cli_id); |
| 8389 | ((void) cli_id_len); |
| 8390 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8391 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8392 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8393 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8394 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8395 | /* Once ssl->out_hdr as the address of the beginning of the |
| 8396 | * next outgoing record is set, deduce the other pointers. |
| 8397 | * |
| 8398 | * Note: For TLS, we save the implicit record sequence number |
| 8399 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 8400 | * and the caller has to make sure there's space for this. |
| 8401 | */ |
| 8402 | |
| 8403 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 8404 | mbedtls_ssl_transform *transform ) |
| 8405 | { |
| 8406 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8407 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8408 | { |
| 8409 | ssl->out_ctr = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8410 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8411 | ssl->out_cid = ssl->out_ctr + 8; |
| 8412 | ssl->out_len = ssl->out_cid; |
| 8413 | if( transform != NULL ) |
| 8414 | ssl->out_len += transform->out_cid_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8415 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8416 | ssl->out_len = ssl->out_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8417 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8418 | ssl->out_iv = ssl->out_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8419 | } |
| 8420 | else |
| 8421 | #endif |
| 8422 | { |
| 8423 | ssl->out_ctr = ssl->out_hdr - 8; |
| 8424 | ssl->out_len = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8425 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8426 | ssl->out_cid = ssl->out_len; |
| 8427 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8428 | ssl->out_iv = ssl->out_hdr + 5; |
| 8429 | } |
| 8430 | |
| 8431 | /* Adjust out_msg to make space for explicit IV, if used. */ |
| 8432 | if( transform != NULL && |
| 8433 | ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 8434 | { |
| 8435 | ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen; |
| 8436 | } |
| 8437 | else |
| 8438 | ssl->out_msg = ssl->out_iv; |
| 8439 | } |
| 8440 | |
| 8441 | /* Once ssl->in_hdr as the address of the beginning of the |
| 8442 | * next incoming record is set, deduce the other pointers. |
| 8443 | * |
| 8444 | * Note: For TLS, we save the implicit record sequence number |
| 8445 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 8446 | * and the caller has to make sure there's space for this. |
| 8447 | */ |
| 8448 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8449 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8450 | { |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8451 | /* This function sets the pointers to match the case |
| 8452 | * of unprotected TLS/DTLS records, with both ssl->in_iv |
| 8453 | * and ssl->in_msg pointing to the beginning of the record |
| 8454 | * content. |
| 8455 | * |
| 8456 | * When decrypting a protected record, ssl->in_msg |
| 8457 | * will be shifted to point to the beginning of the |
| 8458 | * record plaintext. |
| 8459 | */ |
| 8460 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8461 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8462 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8463 | { |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8464 | /* This sets the header pointers to match records |
| 8465 | * without CID. When we receive a record containing |
| 8466 | * a CID, the fields are shifted accordingly in |
| 8467 | * ssl_parse_record_header(). */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8468 | ssl->in_ctr = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8469 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8470 | ssl->in_cid = ssl->in_ctr + 8; |
| 8471 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8472 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8473 | ssl->in_len = ssl->in_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8474 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8475 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8476 | } |
| 8477 | else |
| 8478 | #endif |
| 8479 | { |
| 8480 | ssl->in_ctr = ssl->in_hdr - 8; |
| 8481 | ssl->in_len = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8482 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8483 | ssl->in_cid = ssl->in_len; |
| 8484 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8485 | ssl->in_iv = ssl->in_hdr + 5; |
| 8486 | } |
| 8487 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8488 | /* This will be adjusted at record decryption time. */ |
| 8489 | ssl->in_msg = ssl->in_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8490 | } |
| 8491 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8492 | /* |
| 8493 | * Initialize an SSL context |
| 8494 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 8495 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 8496 | { |
| 8497 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 8498 | } |
| 8499 | |
| 8500 | /* |
| 8501 | * Setup an SSL context |
| 8502 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8503 | |
| 8504 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ) |
| 8505 | { |
| 8506 | /* Set the incoming and outgoing record pointers. */ |
| 8507 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8508 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8509 | { |
| 8510 | ssl->out_hdr = ssl->out_buf; |
| 8511 | ssl->in_hdr = ssl->in_buf; |
| 8512 | } |
| 8513 | else |
| 8514 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8515 | { |
| 8516 | ssl->out_hdr = ssl->out_buf + 8; |
| 8517 | ssl->in_hdr = ssl->in_buf + 8; |
| 8518 | } |
| 8519 | |
| 8520 | /* Derive other internal pointers. */ |
| 8521 | ssl_update_out_pointers( ssl, NULL /* no transform enabled */ ); |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8522 | ssl_update_in_pointers ( ssl ); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8523 | } |
| 8524 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8525 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 8526 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8527 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8528 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8529 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8530 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8531 | |
| 8532 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 8533 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8534 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 8535 | |
| 8536 | /* Set to NULL in case of an error condition */ |
| 8537 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8538 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8539 | ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 8540 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8541 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8542 | 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] | 8543 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8544 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8545 | } |
| 8546 | |
| 8547 | ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 8548 | if( ssl->out_buf == NULL ) |
| 8549 | { |
| 8550 | 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] | 8551 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8552 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8553 | } |
| 8554 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8555 | ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 8556 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8557 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8558 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8559 | |
| 8560 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8561 | |
| 8562 | error: |
| 8563 | mbedtls_free( ssl->in_buf ); |
| 8564 | mbedtls_free( ssl->out_buf ); |
| 8565 | |
| 8566 | ssl->conf = NULL; |
| 8567 | |
| 8568 | ssl->in_buf = NULL; |
| 8569 | ssl->out_buf = NULL; |
| 8570 | |
| 8571 | ssl->in_hdr = NULL; |
| 8572 | ssl->in_ctr = NULL; |
| 8573 | ssl->in_len = NULL; |
| 8574 | ssl->in_iv = NULL; |
| 8575 | ssl->in_msg = NULL; |
| 8576 | |
| 8577 | ssl->out_hdr = NULL; |
| 8578 | ssl->out_ctr = NULL; |
| 8579 | ssl->out_len = NULL; |
| 8580 | ssl->out_iv = NULL; |
| 8581 | ssl->out_msg = NULL; |
| 8582 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 8583 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8584 | } |
| 8585 | |
| 8586 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8587 | * Reset an initialized and used SSL context for re-use while retaining |
| 8588 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8589 | * |
| 8590 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 8591 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8592 | */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8593 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8594 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8595 | int ret; |
| 8596 | |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 8597 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \ |
| 8598 | !defined(MBEDTLS_SSL_SRV_C) |
| 8599 | ((void) partial); |
| 8600 | #endif |
| 8601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8602 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8603 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8604 | /* Cancel any possibly running timer */ |
| 8605 | ssl_set_timer( ssl, 0 ); |
| 8606 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8607 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8608 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8609 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8610 | |
| 8611 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8612 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 8613 | 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] | 8614 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8615 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8616 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8617 | ssl->in_offt = NULL; |
Hanno Becker | f29d470 | 2018-08-10 11:31:15 +0100 | [diff] [blame] | 8618 | ssl_reset_in_out_pointers( ssl ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8619 | |
| 8620 | ssl->in_msgtype = 0; |
| 8621 | ssl->in_msglen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8622 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8623 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 8624 | ssl->in_epoch = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8625 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8626 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 8627 | ssl_dtls_replay_reset( ssl ); |
| 8628 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8629 | |
| 8630 | ssl->in_hslen = 0; |
| 8631 | ssl->nb_zero = 0; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 8632 | |
| 8633 | ssl->keep_current_message = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8634 | |
| 8635 | ssl->out_msgtype = 0; |
| 8636 | ssl->out_msglen = 0; |
| 8637 | ssl->out_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8638 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 8639 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 8640 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8641 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8642 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8643 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 8644 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8645 | ssl->transform_in = NULL; |
| 8646 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8647 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 8648 | ssl->session_in = NULL; |
| 8649 | ssl->session_out = NULL; |
| 8650 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8651 | memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8652 | |
| 8653 | #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] | 8654 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8655 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 8656 | { |
| 8657 | ssl->in_left = 0; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8658 | memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8659 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8661 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 8662 | if( mbedtls_ssl_hw_record_reset != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8663 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8664 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); |
| 8665 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8666 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8667 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); |
| 8668 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8669 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8670 | } |
| 8671 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8672 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8673 | if( ssl->transform ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8674 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8675 | mbedtls_ssl_transform_free( ssl->transform ); |
| 8676 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8677 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8678 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8679 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8680 | if( ssl->session ) |
| 8681 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8682 | mbedtls_ssl_session_free( ssl->session ); |
| 8683 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8684 | ssl->session = NULL; |
| 8685 | } |
| 8686 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8687 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8688 | ssl->alpn_chosen = NULL; |
| 8689 | #endif |
| 8690 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8691 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8692 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8693 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8694 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8695 | { |
| 8696 | mbedtls_free( ssl->cli_id ); |
| 8697 | ssl->cli_id = NULL; |
| 8698 | ssl->cli_id_len = 0; |
| 8699 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 8700 | #endif |
| 8701 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8702 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 8703 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8704 | |
| 8705 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8706 | } |
| 8707 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 8708 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8709 | * Reset an initialized and used SSL context for re-use while retaining |
| 8710 | * all application-set variables, function pointers and data. |
| 8711 | */ |
| 8712 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 8713 | { |
| 8714 | return( ssl_session_reset_int( ssl, 0 ) ); |
| 8715 | } |
| 8716 | |
| 8717 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8718 | * SSL set accessors |
| 8719 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8720 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8721 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8722 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8723 | } |
| 8724 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8725 | 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] | 8726 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8727 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 8728 | } |
| 8729 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8730 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8731 | 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] | 8732 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8733 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 8734 | } |
| 8735 | #endif |
| 8736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8737 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8738 | 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] | 8739 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8740 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8741 | } |
| 8742 | #endif |
| 8743 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8744 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8745 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 8746 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 8747 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8748 | { |
| 8749 | ssl->disable_datagram_packing = !allow_packing; |
| 8750 | } |
| 8751 | |
| 8752 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 8753 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8754 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8755 | conf->hs_timeout_min = min; |
| 8756 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8757 | } |
| 8758 | #endif |
| 8759 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8760 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8761 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8762 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8763 | } |
| 8764 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8765 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8766 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 8767 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8768 | void *p_vrfy ) |
| 8769 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8770 | conf->f_vrfy = f_vrfy; |
| 8771 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8772 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8773 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8774 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8775 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 8776 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8777 | void *p_rng ) |
| 8778 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 8779 | conf->f_rng = f_rng; |
| 8780 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8781 | } |
| 8782 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8783 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 8784 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8785 | void *p_dbg ) |
| 8786 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8787 | conf->f_dbg = f_dbg; |
| 8788 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8789 | } |
| 8790 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8791 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8792 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8793 | mbedtls_ssl_send_t *f_send, |
| 8794 | mbedtls_ssl_recv_t *f_recv, |
| 8795 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8796 | { |
| 8797 | ssl->p_bio = p_bio; |
| 8798 | ssl->f_send = f_send; |
| 8799 | ssl->f_recv = f_recv; |
| 8800 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 8801 | } |
| 8802 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 8803 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8804 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 8805 | { |
| 8806 | ssl->mtu = mtu; |
| 8807 | } |
| 8808 | #endif |
| 8809 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8810 | 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] | 8811 | { |
| 8812 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8813 | } |
| 8814 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8815 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 8816 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8817 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 8818 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8819 | { |
| 8820 | ssl->p_timer = p_timer; |
| 8821 | ssl->f_set_timer = f_set_timer; |
| 8822 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8823 | |
| 8824 | /* Make sure we start with no timer running */ |
| 8825 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8826 | } |
| 8827 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8828 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8829 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8830 | void *p_cache, |
| 8831 | int (*f_get_cache)(void *, mbedtls_ssl_session *), |
| 8832 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8833 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8834 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8835 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8836 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8837 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8838 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8839 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8840 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8841 | 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] | 8842 | { |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8843 | int ret; |
| 8844 | |
| 8845 | if( ssl == NULL || |
| 8846 | session == NULL || |
| 8847 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8848 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8849 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8850 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8851 | } |
| 8852 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 8853 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 8854 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8855 | return( ret ); |
| 8856 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8857 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8858 | |
| 8859 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8860 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8861 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8862 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8863 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8864 | const int *ciphersuites ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8865 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8866 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; |
| 8867 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; |
| 8868 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; |
| 8869 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8870 | } |
| 8871 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8872 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 8873 | const int *ciphersuites, |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8874 | int major, int minor ) |
| 8875 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8876 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8877 | return; |
| 8878 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8879 | 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] | 8880 | return; |
| 8881 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8882 | conf->ciphersuite_list[minor] = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8883 | } |
| 8884 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8885 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8886 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 8887 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8888 | { |
| 8889 | conf->cert_profile = profile; |
| 8890 | } |
| 8891 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8892 | /* Append a new keycert entry to a (possibly empty) list */ |
| 8893 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 8894 | mbedtls_x509_crt *cert, |
| 8895 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8896 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8897 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8898 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8899 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 8900 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8901 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8902 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8903 | new_cert->cert = cert; |
| 8904 | new_cert->key = key; |
| 8905 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8906 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8907 | /* Update head is the list was null, else add to the end */ |
| 8908 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8909 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8910 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8911 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8912 | else |
| 8913 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8914 | mbedtls_ssl_key_cert *cur = *head; |
| 8915 | while( cur->next != NULL ) |
| 8916 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8917 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8918 | } |
| 8919 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8920 | return( 0 ); |
| 8921 | } |
| 8922 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8923 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8924 | mbedtls_x509_crt *own_cert, |
| 8925 | mbedtls_pk_context *pk_key ) |
| 8926 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 8927 | 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] | 8928 | } |
| 8929 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8930 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8931 | mbedtls_x509_crt *ca_chain, |
| 8932 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8933 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8934 | conf->ca_chain = ca_chain; |
| 8935 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 8936 | |
| 8937 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 8938 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 8939 | * cannot be used together. */ |
| 8940 | conf->f_ca_cb = NULL; |
| 8941 | conf->p_ca_cb = NULL; |
| 8942 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8943 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 8944 | |
| 8945 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 8946 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 8947 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 8948 | void *p_ca_cb ) |
| 8949 | { |
| 8950 | conf->f_ca_cb = f_ca_cb; |
| 8951 | conf->p_ca_cb = p_ca_cb; |
| 8952 | |
| 8953 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 8954 | * cannot be used together. */ |
| 8955 | conf->ca_chain = NULL; |
| 8956 | conf->ca_crl = NULL; |
| 8957 | } |
| 8958 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8959 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 8960 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8961 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8962 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 8963 | mbedtls_x509_crt *own_cert, |
| 8964 | mbedtls_pk_context *pk_key ) |
| 8965 | { |
| 8966 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 8967 | own_cert, pk_key ) ); |
| 8968 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 8969 | |
| 8970 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 8971 | mbedtls_x509_crt *ca_chain, |
| 8972 | mbedtls_x509_crl *ca_crl ) |
| 8973 | { |
| 8974 | ssl->handshake->sni_ca_chain = ca_chain; |
| 8975 | ssl->handshake->sni_ca_crl = ca_crl; |
| 8976 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8977 | |
| 8978 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 8979 | int authmode ) |
| 8980 | { |
| 8981 | ssl->handshake->sni_authmode = authmode; |
| 8982 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8983 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8984 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 8985 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8986 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 8987 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 8988 | void *p_vrfy ) |
| 8989 | { |
| 8990 | ssl->f_vrfy = f_vrfy; |
| 8991 | ssl->p_vrfy = p_vrfy; |
| 8992 | } |
| 8993 | #endif |
| 8994 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8995 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8996 | /* |
| 8997 | * Set EC J-PAKE password for current handshake |
| 8998 | */ |
| 8999 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 9000 | const unsigned char *pw, |
| 9001 | size_t pw_len ) |
| 9002 | { |
| 9003 | mbedtls_ecjpake_role role; |
| 9004 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 9005 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 9006 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9007 | |
| 9008 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 9009 | role = MBEDTLS_ECJPAKE_SERVER; |
| 9010 | else |
| 9011 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 9012 | |
| 9013 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 9014 | role, |
| 9015 | MBEDTLS_MD_SHA256, |
| 9016 | MBEDTLS_ECP_DP_SECP256R1, |
| 9017 | pw, pw_len ) ); |
| 9018 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 9019 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 9020 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9021 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9022 | |
| 9023 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 9024 | { |
| 9025 | /* Remove reference to existing PSK, if any. */ |
| 9026 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9027 | if( conf->psk_opaque != 0 ) |
| 9028 | { |
| 9029 | /* The maintenance of the PSK key slot is the |
| 9030 | * user's responsibility. */ |
| 9031 | conf->psk_opaque = 0; |
| 9032 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 9033 | /* This and the following branch should never |
| 9034 | * be taken simultaenously as we maintain the |
| 9035 | * invariant that raw and opaque PSKs are never |
| 9036 | * configured simultaneously. As a safeguard, |
| 9037 | * though, `else` is omitted here. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9038 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9039 | if( conf->psk != NULL ) |
| 9040 | { |
| 9041 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 9042 | |
| 9043 | mbedtls_free( conf->psk ); |
| 9044 | conf->psk = NULL; |
| 9045 | conf->psk_len = 0; |
| 9046 | } |
| 9047 | |
| 9048 | /* Remove reference to PSK identity, if any. */ |
| 9049 | if( conf->psk_identity != NULL ) |
| 9050 | { |
| 9051 | mbedtls_free( conf->psk_identity ); |
| 9052 | conf->psk_identity = NULL; |
| 9053 | conf->psk_identity_len = 0; |
| 9054 | } |
| 9055 | } |
| 9056 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9057 | /* This function assumes that PSK identity in the SSL config is unset. |
| 9058 | * It checks that the provided identity is well-formed and attempts |
| 9059 | * to make a copy of it in the SSL config. |
| 9060 | * On failure, the PSK identity in the config remains unset. */ |
| 9061 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 9062 | unsigned char const *psk_identity, |
| 9063 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 9064 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 9065 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9066 | if( psk_identity == NULL || |
| 9067 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9068 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 9069 | { |
| 9070 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9071 | } |
| 9072 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9073 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 9074 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 9075 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9076 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 9077 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 9078 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 9079 | |
| 9080 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9081 | } |
| 9082 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9083 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 9084 | const unsigned char *psk, size_t psk_len, |
| 9085 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 9086 | { |
| 9087 | int ret; |
| 9088 | /* Remove opaque/raw PSK + PSK Identity */ |
| 9089 | ssl_conf_remove_psk( conf ); |
| 9090 | |
| 9091 | /* Check and set raw PSK */ |
| 9092 | if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 9093 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9094 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 9095 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9096 | conf->psk_len = psk_len; |
| 9097 | memcpy( conf->psk, psk, conf->psk_len ); |
| 9098 | |
| 9099 | /* Check and set PSK Identity */ |
| 9100 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 9101 | if( ret != 0 ) |
| 9102 | ssl_conf_remove_psk( conf ); |
| 9103 | |
| 9104 | return( ret ); |
| 9105 | } |
| 9106 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9107 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 9108 | { |
| 9109 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9110 | if( ssl->handshake->psk_opaque != 0 ) |
| 9111 | { |
| 9112 | ssl->handshake->psk_opaque = 0; |
| 9113 | } |
| 9114 | else |
| 9115 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9116 | if( ssl->handshake->psk != NULL ) |
| 9117 | { |
| 9118 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 9119 | ssl->handshake->psk_len ); |
| 9120 | mbedtls_free( ssl->handshake->psk ); |
| 9121 | ssl->handshake->psk_len = 0; |
| 9122 | } |
| 9123 | } |
| 9124 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9125 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 9126 | const unsigned char *psk, size_t psk_len ) |
| 9127 | { |
| 9128 | if( psk == NULL || ssl->handshake == NULL ) |
| 9129 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9130 | |
| 9131 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 9132 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9133 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9134 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9135 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 9136 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 9137 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9138 | |
| 9139 | ssl->handshake->psk_len = psk_len; |
| 9140 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 9141 | |
| 9142 | return( 0 ); |
| 9143 | } |
| 9144 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9145 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9146 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 9147 | psa_key_handle_t psk_slot, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9148 | const unsigned char *psk_identity, |
| 9149 | size_t psk_identity_len ) |
| 9150 | { |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9151 | int ret; |
| 9152 | /* Clear opaque/raw PSK + PSK Identity, if present. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9153 | ssl_conf_remove_psk( conf ); |
| 9154 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9155 | /* Check and set opaque PSK */ |
| 9156 | if( psk_slot == 0 ) |
| 9157 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9158 | conf->psk_opaque = psk_slot; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9159 | |
| 9160 | /* Check and set PSK Identity */ |
| 9161 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 9162 | psk_identity_len ); |
| 9163 | if( ret != 0 ) |
| 9164 | ssl_conf_remove_psk( conf ); |
| 9165 | |
| 9166 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9167 | } |
| 9168 | |
| 9169 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 9170 | psa_key_handle_t psk_slot ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9171 | { |
| 9172 | if( psk_slot == 0 || ssl->handshake == NULL ) |
| 9173 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9174 | |
| 9175 | ssl_remove_psk( ssl ); |
| 9176 | ssl->handshake->psk_opaque = psk_slot; |
| 9177 | return( 0 ); |
| 9178 | } |
| 9179 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9180 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9181 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9182 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9183 | size_t), |
| 9184 | void *p_psk ) |
| 9185 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9186 | conf->f_psk = f_psk; |
| 9187 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 9188 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9189 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 9190 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 9191 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 9192 | |
| 9193 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9194 | 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] | 9195 | { |
| 9196 | int ret; |
| 9197 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9198 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || |
| 9199 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) |
| 9200 | { |
| 9201 | mbedtls_mpi_free( &conf->dhm_P ); |
| 9202 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9203 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9204 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9205 | |
| 9206 | return( 0 ); |
| 9207 | } |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 9208 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9209 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 9210 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 9211 | const unsigned char *dhm_P, size_t P_len, |
| 9212 | const unsigned char *dhm_G, size_t G_len ) |
| 9213 | { |
| 9214 | int ret; |
| 9215 | |
| 9216 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 9217 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 9218 | { |
| 9219 | mbedtls_mpi_free( &conf->dhm_P ); |
| 9220 | mbedtls_mpi_free( &conf->dhm_G ); |
| 9221 | return( ret ); |
| 9222 | } |
| 9223 | |
| 9224 | return( 0 ); |
| 9225 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9226 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9227 | 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] | 9228 | { |
| 9229 | int ret; |
| 9230 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9231 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || |
| 9232 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) |
| 9233 | { |
| 9234 | mbedtls_mpi_free( &conf->dhm_P ); |
| 9235 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9236 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9237 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9238 | |
| 9239 | return( 0 ); |
| 9240 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 9241 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9242 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 9243 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 9244 | /* |
| 9245 | * Set the minimum length for Diffie-Hellman parameters |
| 9246 | */ |
| 9247 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 9248 | unsigned int bitlen ) |
| 9249 | { |
| 9250 | conf->dhm_min_bitlen = bitlen; |
| 9251 | } |
| 9252 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 9253 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9254 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 9255 | /* |
| 9256 | * Set allowed/preferred hashes for handshake signatures |
| 9257 | */ |
| 9258 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 9259 | const int *hashes ) |
| 9260 | { |
| 9261 | conf->sig_hashes = hashes; |
| 9262 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9263 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 9264 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 9265 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9266 | /* |
| 9267 | * Set the allowed elliptic curves |
| 9268 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9269 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9270 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9271 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9272 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9273 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9274 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9275 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9276 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9277 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9278 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9279 | /* Initialize to suppress unnecessary compiler warning */ |
| 9280 | size_t hostname_len = 0; |
| 9281 | |
| 9282 | /* Check if new hostname is valid before |
| 9283 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9284 | if( hostname != NULL ) |
| 9285 | { |
| 9286 | hostname_len = strlen( hostname ); |
| 9287 | |
| 9288 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 9289 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9290 | } |
| 9291 | |
| 9292 | /* Now it's clear that we will overwrite the old hostname, |
| 9293 | * so we can free it safely */ |
| 9294 | |
| 9295 | if( ssl->hostname != NULL ) |
| 9296 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9297 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9298 | mbedtls_free( ssl->hostname ); |
| 9299 | } |
| 9300 | |
| 9301 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 9302 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9303 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9304 | { |
| 9305 | ssl->hostname = NULL; |
| 9306 | } |
| 9307 | else |
| 9308 | { |
| 9309 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9310 | if( ssl->hostname == NULL ) |
| 9311 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 9312 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9313 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 9314 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9315 | ssl->hostname[hostname_len] = '\0'; |
| 9316 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9317 | |
| 9318 | return( 0 ); |
| 9319 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 9320 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9321 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9322 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9323 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9324 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9325 | const unsigned char *, size_t), |
| 9326 | void *p_sni ) |
| 9327 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9328 | conf->f_sni = f_sni; |
| 9329 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9330 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9331 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9332 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9333 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9334 | 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] | 9335 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9336 | size_t cur_len, tot_len; |
| 9337 | const char **p; |
| 9338 | |
| 9339 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 9340 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 9341 | * MUST NOT be truncated." |
| 9342 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9343 | */ |
| 9344 | tot_len = 0; |
| 9345 | for( p = protos; *p != NULL; p++ ) |
| 9346 | { |
| 9347 | cur_len = strlen( *p ); |
| 9348 | tot_len += cur_len; |
| 9349 | |
| 9350 | if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9351 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9352 | } |
| 9353 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9354 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9355 | |
| 9356 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9357 | } |
| 9358 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9359 | 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] | 9360 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9361 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9362 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9363 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9364 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9365 | 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] | 9366 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9367 | conf->max_major_ver = major; |
| 9368 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9369 | } |
| 9370 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9371 | 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] | 9372 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9373 | conf->min_major_ver = major; |
| 9374 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9375 | } |
| 9376 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9377 | #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] | 9378 | 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] | 9379 | { |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 9380 | conf->fallback = fallback; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 9381 | } |
| 9382 | #endif |
| 9383 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 9384 | #if defined(MBEDTLS_SSL_SRV_C) |
| 9385 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 9386 | char cert_req_ca_list ) |
| 9387 | { |
| 9388 | conf->cert_req_ca_list = cert_req_ca_list; |
| 9389 | } |
| 9390 | #endif |
| 9391 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9392 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9393 | 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] | 9394 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9395 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 9396 | } |
| 9397 | #endif |
| 9398 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9399 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9400 | 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] | 9401 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9402 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 9403 | } |
| 9404 | #endif |
| 9405 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9406 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9407 | 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] | 9408 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9409 | conf->arc4_disabled = arc4; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9410 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9411 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9412 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9413 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9414 | 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] | 9415 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9416 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9417 | 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] | 9418 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9419 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9420 | } |
| 9421 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 9422 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9423 | |
| 9424 | return( 0 ); |
| 9425 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9426 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9427 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9428 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9429 | 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] | 9430 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9431 | conf->trunc_hmac = truncate; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9432 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9433 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9434 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9435 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9436 | 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] | 9437 | { |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 9438 | conf->cbc_record_splitting = split; |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 9439 | } |
| 9440 | #endif |
| 9441 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9442 | 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] | 9443 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9444 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9445 | } |
| 9446 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9447 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9448 | 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] | 9449 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9450 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 9451 | } |
| 9452 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9453 | 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] | 9454 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9455 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9456 | } |
| 9457 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9458 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9459 | const unsigned char period[8] ) |
| 9460 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9461 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9462 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9463 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9464 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9465 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9466 | #if defined(MBEDTLS_SSL_CLI_C) |
| 9467 | 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] | 9468 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 9469 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9470 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9471 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9472 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9473 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9474 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 9475 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 9476 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 9477 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9478 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9479 | conf->f_ticket_write = f_ticket_write; |
| 9480 | conf->f_ticket_parse = f_ticket_parse; |
| 9481 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9482 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9483 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9484 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9485 | |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 9486 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 9487 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, |
| 9488 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 9489 | void *p_export_keys ) |
| 9490 | { |
| 9491 | conf->f_export_keys = f_export_keys; |
| 9492 | conf->p_export_keys = p_export_keys; |
| 9493 | } |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 9494 | |
| 9495 | void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf, |
| 9496 | mbedtls_ssl_export_keys_ext_t *f_export_keys_ext, |
| 9497 | void *p_export_keys ) |
| 9498 | { |
| 9499 | conf->f_export_keys_ext = f_export_keys_ext; |
| 9500 | conf->p_export_keys = p_export_keys; |
| 9501 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 9502 | #endif |
| 9503 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9504 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9505 | void mbedtls_ssl_conf_async_private_cb( |
| 9506 | mbedtls_ssl_config *conf, |
| 9507 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 9508 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 9509 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 9510 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9511 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9512 | { |
| 9513 | conf->f_async_sign_start = f_async_sign; |
| 9514 | conf->f_async_decrypt_start = f_async_decrypt; |
| 9515 | conf->f_async_resume = f_async_resume; |
| 9516 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9517 | conf->p_async_config_data = async_config_data; |
| 9518 | } |
| 9519 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 9520 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 9521 | { |
| 9522 | return( conf->p_async_config_data ); |
| 9523 | } |
| 9524 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9525 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9526 | { |
| 9527 | if( ssl->handshake == NULL ) |
| 9528 | return( NULL ); |
| 9529 | else |
| 9530 | return( ssl->handshake->user_async_ctx ); |
| 9531 | } |
| 9532 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9533 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9534 | void *ctx ) |
| 9535 | { |
| 9536 | if( ssl->handshake != NULL ) |
| 9537 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9538 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9539 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9540 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9541 | /* |
| 9542 | * SSL get accessors |
| 9543 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9544 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9545 | { |
| 9546 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); |
| 9547 | } |
| 9548 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9549 | int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl ) |
| 9550 | { |
| 9551 | /* |
| 9552 | * Case A: We're currently holding back |
| 9553 | * a message for further processing. |
| 9554 | */ |
| 9555 | |
| 9556 | if( ssl->keep_current_message == 1 ) |
| 9557 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9558 | 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] | 9559 | return( 1 ); |
| 9560 | } |
| 9561 | |
| 9562 | /* |
| 9563 | * Case B: Further records are pending in the current datagram. |
| 9564 | */ |
| 9565 | |
| 9566 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9567 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 9568 | ssl->in_left > ssl->next_record_offset ) |
| 9569 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9570 | 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] | 9571 | return( 1 ); |
| 9572 | } |
| 9573 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9574 | |
| 9575 | /* |
| 9576 | * Case C: A handshake message is being processed. |
| 9577 | */ |
| 9578 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9579 | if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen ) |
| 9580 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9581 | 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] | 9582 | return( 1 ); |
| 9583 | } |
| 9584 | |
| 9585 | /* |
| 9586 | * Case D: An application data message is being processed |
| 9587 | */ |
| 9588 | if( ssl->in_offt != NULL ) |
| 9589 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9590 | 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] | 9591 | return( 1 ); |
| 9592 | } |
| 9593 | |
| 9594 | /* |
| 9595 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 9596 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9597 | * we implement support for multiple alerts in single records. |
| 9598 | */ |
| 9599 | |
| 9600 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) ); |
| 9601 | return( 0 ); |
| 9602 | } |
| 9603 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 9604 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9605 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 9606 | if( ssl->session != NULL ) |
| 9607 | return( ssl->session->verify_result ); |
| 9608 | |
| 9609 | if( ssl->session_negotiate != NULL ) |
| 9610 | return( ssl->session_negotiate->verify_result ); |
| 9611 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 9612 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9613 | } |
| 9614 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9615 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9616 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9617 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9618 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9619 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9620 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9621 | } |
| 9622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9623 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9624 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9625 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9626 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9627 | { |
| 9628 | switch( ssl->minor_ver ) |
| 9629 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9630 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9631 | return( "DTLSv1.0" ); |
| 9632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9633 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9634 | return( "DTLSv1.2" ); |
| 9635 | |
| 9636 | default: |
| 9637 | return( "unknown (DTLS)" ); |
| 9638 | } |
| 9639 | } |
| 9640 | #endif |
| 9641 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9642 | switch( ssl->minor_ver ) |
| 9643 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9644 | case MBEDTLS_SSL_MINOR_VERSION_0: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9645 | return( "SSLv3.0" ); |
| 9646 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9647 | case MBEDTLS_SSL_MINOR_VERSION_1: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9648 | return( "TLSv1.0" ); |
| 9649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9650 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9651 | return( "TLSv1.1" ); |
| 9652 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9653 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 9654 | return( "TLSv1.2" ); |
| 9655 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9656 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9657 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9658 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9659 | } |
| 9660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9661 | 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] | 9662 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9663 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9664 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9665 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9666 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9667 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl ); |
| 9668 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9669 | if( transform == NULL ) |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9670 | return( (int) out_hdr_len ); |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9671 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9672 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 9673 | if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL ) |
| 9674 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9675 | #endif |
| 9676 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9677 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9678 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9679 | case MBEDTLS_MODE_GCM: |
| 9680 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9681 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9682 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9683 | transform_expansion = transform->minlen; |
| 9684 | break; |
| 9685 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9686 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9687 | |
| 9688 | block_size = mbedtls_cipher_get_block_size( |
| 9689 | &transform->cipher_ctx_enc ); |
| 9690 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9691 | /* Expansion due to the addition of the MAC. */ |
| 9692 | transform_expansion += transform->maclen; |
| 9693 | |
| 9694 | /* Expansion due to the addition of CBC padding; |
| 9695 | * Theoretically up to 256 bytes, but we never use |
| 9696 | * more than the block size of the underlying cipher. */ |
| 9697 | transform_expansion += block_size; |
| 9698 | |
| 9699 | /* For TLS 1.1 or higher, an explicit IV is added |
| 9700 | * after the record header. */ |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9701 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9702 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9703 | transform_expansion += block_size; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9704 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9705 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9706 | break; |
| 9707 | |
| 9708 | default: |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 9709 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9710 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9711 | } |
| 9712 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9713 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9714 | if( transform->out_cid_len != 0 ) |
| 9715 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9716 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9717 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9718 | return( (int)( out_hdr_len + transform_expansion ) ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9719 | } |
| 9720 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9721 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9722 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 9723 | { |
| 9724 | size_t max_len; |
| 9725 | |
| 9726 | /* |
| 9727 | * Assume mfl_code is correct since it was checked when set |
| 9728 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9729 | 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] | 9730 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9731 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9732 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9733 | 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] | 9734 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9735 | 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] | 9736 | } |
| 9737 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9738 | /* During a handshake, use the value being negotiated */ |
| 9739 | if( ssl->session_negotiate != NULL && |
| 9740 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 9741 | { |
| 9742 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 9743 | } |
| 9744 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9745 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9746 | } |
| 9747 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 9748 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9749 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9750 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) |
| 9751 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9752 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 9753 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 9754 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 9755 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 9756 | return ( 0 ); |
| 9757 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9758 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 9759 | return( ssl->mtu ); |
| 9760 | |
| 9761 | if( ssl->mtu == 0 ) |
| 9762 | return( ssl->handshake->mtu ); |
| 9763 | |
| 9764 | return( ssl->mtu < ssl->handshake->mtu ? |
| 9765 | ssl->mtu : ssl->handshake->mtu ); |
| 9766 | } |
| 9767 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9768 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9769 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 9770 | { |
| 9771 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 9772 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 9773 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9774 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9775 | (void) ssl; |
| 9776 | #endif |
| 9777 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9778 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9779 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 9780 | |
| 9781 | if( max_len > mfl ) |
| 9782 | max_len = mfl; |
| 9783 | #endif |
| 9784 | |
| 9785 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9786 | if( ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9787 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9788 | const size_t mtu = ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9789 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 9790 | const size_t overhead = (size_t) ret; |
| 9791 | |
| 9792 | if( ret < 0 ) |
| 9793 | return( ret ); |
| 9794 | |
| 9795 | if( mtu <= overhead ) |
| 9796 | { |
| 9797 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 9798 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 9799 | } |
| 9800 | |
| 9801 | if( max_len > mtu - overhead ) |
| 9802 | max_len = mtu - overhead; |
| 9803 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9804 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9805 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 9806 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9807 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9808 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9809 | #endif |
| 9810 | |
| 9811 | return( (int) max_len ); |
| 9812 | } |
| 9813 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9814 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9815 | 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] | 9816 | { |
| 9817 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9818 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9819 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9820 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9821 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9822 | #else |
| 9823 | return( NULL ); |
| 9824 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9825 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9826 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9827 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9828 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 9829 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 9830 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9831 | { |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9832 | if( ssl == NULL || |
| 9833 | dst == NULL || |
| 9834 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9835 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9836 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9837 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9838 | } |
| 9839 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 9840 | return( mbedtls_ssl_session_copy( dst, ssl->session ) ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9841 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9842 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9843 | |
Manuel Pégourié-Gonnard | b5e4e0a | 2019-05-20 11:12:28 +0200 | [diff] [blame] | 9844 | const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl ) |
| 9845 | { |
| 9846 | if( ssl == NULL ) |
| 9847 | return( NULL ); |
| 9848 | |
| 9849 | return( ssl->session ); |
| 9850 | } |
| 9851 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9852 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9853 | * Define ticket header determining Mbed TLS version |
| 9854 | * and structure of the ticket. |
| 9855 | */ |
| 9856 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9857 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9858 | * Define bitflag determining compile-time settings influencing |
| 9859 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9860 | */ |
| 9861 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9862 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9863 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9864 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9865 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9866 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9867 | |
| 9868 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9869 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9870 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9871 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9872 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9873 | |
| 9874 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9875 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9876 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9877 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9878 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 9879 | |
| 9880 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9881 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9882 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9883 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9884 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 9885 | |
| 9886 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9887 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9888 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9889 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9890 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 9891 | |
| 9892 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9893 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9894 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9895 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9896 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 9897 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9898 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 9899 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 9900 | #else |
| 9901 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 9902 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 9903 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9904 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 9905 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 9906 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 9907 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
| 9908 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4 |
| 9909 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5 |
| 9910 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9911 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9912 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9913 | ( (uint16_t) ( \ |
| 9914 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 9915 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 9916 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 9917 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
| 9918 | ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \ |
| 9919 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 9920 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9921 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 9922 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9923 | MBEDTLS_VERSION_MAJOR, |
| 9924 | MBEDTLS_VERSION_MINOR, |
| 9925 | MBEDTLS_VERSION_PATCH, |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9926 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 9927 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 9928 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9929 | |
| 9930 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9931 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9932 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9933 | * |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9934 | * opaque mbedtls_version[3]; // major, minor, patch |
| 9935 | * opaque session_format[2]; // version-specific 16-bit field determining |
| 9936 | * // the format of the remaining |
| 9937 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 9938 | * |
| 9939 | * Note: When updating the format, remember to keep |
| 9940 | * these version+format bytes. |
| 9941 | * |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 9942 | * // In this version, `session_format` determines |
| 9943 | * // the setting of those compile-time |
| 9944 | * // configuration options which influence |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9945 | * // the structure of mbedtls_ssl_session. |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9946 | * uint64 start_time; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9947 | * uint8 ciphersuite[2]; // defined by the standard |
| 9948 | * uint8 compression; // 0 or 1 |
| 9949 | * uint8 session_id_len; // at most 32 |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9950 | * opaque session_id[32]; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9951 | * opaque master[48]; // fixed length in the standard |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9952 | * uint32 verify_result; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9953 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 9954 | * 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] | 9955 | * uint32 ticket_lifetime; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9956 | * uint8 mfl_code; // up to 255 according to standard |
| 9957 | * uint8 trunc_hmac; // 0 or 1 |
| 9958 | * uint8 encrypt_then_mac; // 0 or 1 |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9959 | * |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9960 | * The order is the same as in the definition of the structure, except |
| 9961 | * verify_result is put before peer_cert so that all mandatory fields come |
| 9962 | * together in one block. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9963 | */ |
| 9964 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 9965 | unsigned char *buf, |
| 9966 | size_t buf_len, |
| 9967 | size_t *olen ) |
| 9968 | { |
| 9969 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9970 | size_t used = 0; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9971 | #if defined(MBEDTLS_HAVE_TIME) |
| 9972 | uint64_t start; |
| 9973 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9974 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9975 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 9976 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9977 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9978 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9979 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9980 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9981 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9982 | * Add version identifier |
| 9983 | */ |
| 9984 | |
| 9985 | used += sizeof( ssl_serialized_session_header ); |
| 9986 | |
| 9987 | if( used <= buf_len ) |
| 9988 | { |
| 9989 | memcpy( p, ssl_serialized_session_header, |
| 9990 | sizeof( ssl_serialized_session_header ) ); |
| 9991 | p += sizeof( ssl_serialized_session_header ); |
| 9992 | } |
| 9993 | |
| 9994 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9995 | * Time |
| 9996 | */ |
| 9997 | #if defined(MBEDTLS_HAVE_TIME) |
| 9998 | used += 8; |
| 9999 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10000 | if( used <= buf_len ) |
| 10001 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10002 | start = (uint64_t) session->start; |
| 10003 | |
| 10004 | *p++ = (unsigned char)( ( start >> 56 ) & 0xFF ); |
| 10005 | *p++ = (unsigned char)( ( start >> 48 ) & 0xFF ); |
| 10006 | *p++ = (unsigned char)( ( start >> 40 ) & 0xFF ); |
| 10007 | *p++ = (unsigned char)( ( start >> 32 ) & 0xFF ); |
| 10008 | *p++ = (unsigned char)( ( start >> 24 ) & 0xFF ); |
| 10009 | *p++ = (unsigned char)( ( start >> 16 ) & 0xFF ); |
| 10010 | *p++ = (unsigned char)( ( start >> 8 ) & 0xFF ); |
| 10011 | *p++ = (unsigned char)( ( start ) & 0xFF ); |
| 10012 | } |
| 10013 | #endif /* MBEDTLS_HAVE_TIME */ |
| 10014 | |
| 10015 | /* |
| 10016 | * Basic mandatory fields |
| 10017 | */ |
| 10018 | used += 2 /* ciphersuite */ |
| 10019 | + 1 /* compression */ |
| 10020 | + 1 /* id_len */ |
| 10021 | + sizeof( session->id ) |
| 10022 | + sizeof( session->master ) |
| 10023 | + 4; /* verify_result */ |
| 10024 | |
| 10025 | if( used <= buf_len ) |
| 10026 | { |
| 10027 | *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF ); |
| 10028 | *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF ); |
| 10029 | |
| 10030 | *p++ = (unsigned char)( session->compression & 0xFF ); |
| 10031 | |
| 10032 | *p++ = (unsigned char)( session->id_len & 0xFF ); |
| 10033 | memcpy( p, session->id, 32 ); |
| 10034 | p += 32; |
| 10035 | |
| 10036 | memcpy( p, session->master, 48 ); |
| 10037 | p += 48; |
| 10038 | |
| 10039 | *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF ); |
| 10040 | *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF ); |
| 10041 | *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF ); |
| 10042 | *p++ = (unsigned char)( ( session->verify_result ) & 0xFF ); |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10043 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10044 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10045 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10046 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10047 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10048 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10049 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10050 | if( session->peer_cert == NULL ) |
| 10051 | cert_len = 0; |
| 10052 | else |
| 10053 | cert_len = session->peer_cert->raw.len; |
| 10054 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10055 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10056 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10057 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10058 | { |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10059 | *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF ); |
| 10060 | *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF ); |
| 10061 | *p++ = (unsigned char)( ( cert_len ) & 0xFF ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10062 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10063 | if( session->peer_cert != NULL ) |
| 10064 | { |
| 10065 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 10066 | p += cert_len; |
| 10067 | } |
| 10068 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10069 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10070 | if( session->peer_cert_digest != NULL ) |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10071 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10072 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 10073 | if( used <= buf_len ) |
| 10074 | { |
| 10075 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 10076 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 10077 | memcpy( p, session->peer_cert_digest, |
| 10078 | session->peer_cert_digest_len ); |
| 10079 | p += session->peer_cert_digest_len; |
| 10080 | } |
| 10081 | } |
| 10082 | else |
| 10083 | { |
| 10084 | used += 2; |
| 10085 | if( used <= buf_len ) |
| 10086 | { |
| 10087 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 10088 | *p++ = 0; |
| 10089 | } |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10090 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10091 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10092 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 10093 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10094 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10095 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10096 | */ |
| 10097 | #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] | 10098 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10099 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10100 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10101 | { |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10102 | *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF ); |
| 10103 | *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF ); |
| 10104 | *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF ); |
| 10105 | |
| 10106 | if( session->ticket != NULL ) |
| 10107 | { |
| 10108 | memcpy( p, session->ticket, session->ticket_len ); |
| 10109 | p += session->ticket_len; |
| 10110 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10111 | |
| 10112 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF ); |
| 10113 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF ); |
| 10114 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF ); |
| 10115 | *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF ); |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10116 | } |
| 10117 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 10118 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10119 | /* |
| 10120 | * Misc extension-related info |
| 10121 | */ |
| 10122 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 10123 | used += 1; |
| 10124 | |
| 10125 | if( used <= buf_len ) |
| 10126 | *p++ = session->mfl_code; |
| 10127 | #endif |
| 10128 | |
| 10129 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 10130 | used += 1; |
| 10131 | |
| 10132 | if( used <= buf_len ) |
| 10133 | *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF ); |
| 10134 | #endif |
| 10135 | |
| 10136 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 10137 | used += 1; |
| 10138 | |
| 10139 | if( used <= buf_len ) |
| 10140 | *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF ); |
| 10141 | #endif |
| 10142 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10143 | /* Done */ |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10144 | *olen = used; |
| 10145 | |
| 10146 | if( used > buf_len ) |
| 10147 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10148 | |
| 10149 | return( 0 ); |
| 10150 | } |
| 10151 | |
| 10152 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10153 | * Unserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10154 | * |
| 10155 | * This internal version is wrapped by a public function that cleans up in |
| 10156 | * case of error. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10157 | */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10158 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 10159 | const unsigned char *buf, |
| 10160 | size_t len ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10161 | { |
| 10162 | const unsigned char *p = buf; |
| 10163 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10164 | #if defined(MBEDTLS_HAVE_TIME) |
| 10165 | uint64_t start; |
| 10166 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10167 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10168 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10169 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10170 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10171 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 10172 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10173 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10174 | * Check version identifier |
| 10175 | */ |
| 10176 | |
| 10177 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
Hanno Becker | f37d918 | 2019-05-28 13:59:44 +0100 | [diff] [blame] | 10178 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10179 | |
| 10180 | if( memcmp( p, ssl_serialized_session_header, |
| 10181 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 10182 | { |
Hanno Becker | f9b3303 | 2019-06-03 12:58:39 +0100 | [diff] [blame] | 10183 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10184 | } |
| 10185 | p += sizeof( ssl_serialized_session_header ); |
| 10186 | |
| 10187 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10188 | * Time |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10189 | */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10190 | #if defined(MBEDTLS_HAVE_TIME) |
| 10191 | if( 8 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10192 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10193 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10194 | start = ( (uint64_t) p[0] << 56 ) | |
| 10195 | ( (uint64_t) p[1] << 48 ) | |
| 10196 | ( (uint64_t) p[2] << 40 ) | |
| 10197 | ( (uint64_t) p[3] << 32 ) | |
| 10198 | ( (uint64_t) p[4] << 24 ) | |
| 10199 | ( (uint64_t) p[5] << 16 ) | |
| 10200 | ( (uint64_t) p[6] << 8 ) | |
| 10201 | ( (uint64_t) p[7] ); |
| 10202 | p += 8; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10203 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10204 | session->start = (time_t) start; |
| 10205 | #endif /* MBEDTLS_HAVE_TIME */ |
| 10206 | |
| 10207 | /* |
| 10208 | * Basic mandatory fields |
| 10209 | */ |
| 10210 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 10211 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10212 | |
| 10213 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 10214 | p += 2; |
| 10215 | |
| 10216 | session->compression = *p++; |
| 10217 | |
| 10218 | session->id_len = *p++; |
| 10219 | memcpy( session->id, p, 32 ); |
| 10220 | p += 32; |
| 10221 | |
| 10222 | memcpy( session->master, p, 48 ); |
| 10223 | p += 48; |
| 10224 | |
| 10225 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 10226 | ( (uint32_t) p[1] << 16 ) | |
| 10227 | ( (uint32_t) p[2] << 8 ) | |
| 10228 | ( (uint32_t) p[3] ); |
| 10229 | p += 4; |
| 10230 | |
| 10231 | /* Immediately clear invalid pointer values that have been read, in case |
| 10232 | * we exit early before we replaced them with valid ones. */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10233 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10234 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10235 | session->peer_cert = NULL; |
| 10236 | #else |
| 10237 | session->peer_cert_digest = NULL; |
| 10238 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10239 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 10240 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 10241 | session->ticket = NULL; |
| 10242 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 10243 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10244 | /* |
| 10245 | * Peer certificate |
| 10246 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10247 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10248 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10249 | /* Deserialize CRT from the end of the ticket. */ |
| 10250 | if( 3 > (size_t)( end - p ) ) |
| 10251 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10252 | |
| 10253 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 10254 | p += 3; |
| 10255 | |
| 10256 | if( cert_len != 0 ) |
| 10257 | { |
| 10258 | int ret; |
| 10259 | |
| 10260 | if( cert_len > (size_t)( end - p ) ) |
| 10261 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10262 | |
| 10263 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 10264 | |
| 10265 | if( session->peer_cert == NULL ) |
| 10266 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 10267 | |
| 10268 | mbedtls_x509_crt_init( session->peer_cert ); |
| 10269 | |
| 10270 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 10271 | p, cert_len ) ) != 0 ) |
| 10272 | { |
| 10273 | mbedtls_x509_crt_free( session->peer_cert ); |
| 10274 | mbedtls_free( session->peer_cert ); |
| 10275 | session->peer_cert = NULL; |
| 10276 | return( ret ); |
| 10277 | } |
| 10278 | |
| 10279 | p += cert_len; |
| 10280 | } |
| 10281 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10282 | /* Deserialize CRT digest from the end of the ticket. */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10283 | if( 2 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10284 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10285 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10286 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 10287 | session->peer_cert_digest_len = (size_t) *p++; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10288 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10289 | if( session->peer_cert_digest_len != 0 ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10290 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10291 | const mbedtls_md_info_t *md_info = |
| 10292 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 10293 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10294 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10295 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 10296 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10297 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10298 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 10299 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10300 | |
| 10301 | session->peer_cert_digest = |
| 10302 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10303 | if( session->peer_cert_digest == NULL ) |
| 10304 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 10305 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10306 | memcpy( session->peer_cert_digest, p, |
| 10307 | session->peer_cert_digest_len ); |
| 10308 | p += session->peer_cert_digest_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10309 | } |
| 10310 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10311 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 10312 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10313 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10314 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10315 | */ |
| 10316 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 10317 | if( 3 > (size_t)( end - p ) ) |
| 10318 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10319 | |
| 10320 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 10321 | p += 3; |
| 10322 | |
| 10323 | if( session->ticket_len != 0 ) |
| 10324 | { |
| 10325 | if( session->ticket_len > (size_t)( end - p ) ) |
| 10326 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10327 | |
| 10328 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 10329 | if( session->ticket == NULL ) |
| 10330 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 10331 | |
| 10332 | memcpy( session->ticket, p, session->ticket_len ); |
| 10333 | p += session->ticket_len; |
| 10334 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10335 | |
| 10336 | if( 4 > (size_t)( end - p ) ) |
| 10337 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10338 | |
| 10339 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 10340 | ( (uint32_t) p[1] << 16 ) | |
| 10341 | ( (uint32_t) p[2] << 8 ) | |
| 10342 | ( (uint32_t) p[3] ); |
| 10343 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10344 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 10345 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10346 | /* |
| 10347 | * Misc extension-related info |
| 10348 | */ |
| 10349 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 10350 | if( 1 > (size_t)( end - p ) ) |
| 10351 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10352 | |
| 10353 | session->mfl_code = *p++; |
| 10354 | #endif |
| 10355 | |
| 10356 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 10357 | if( 1 > (size_t)( end - p ) ) |
| 10358 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10359 | |
| 10360 | session->trunc_hmac = *p++; |
| 10361 | #endif |
| 10362 | |
| 10363 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 10364 | if( 1 > (size_t)( end - p ) ) |
| 10365 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10366 | |
| 10367 | session->encrypt_then_mac = *p++; |
| 10368 | #endif |
| 10369 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10370 | /* Done, should have consumed entire buffer */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10371 | if( p != end ) |
| 10372 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10373 | |
| 10374 | return( 0 ); |
| 10375 | } |
| 10376 | |
| 10377 | /* |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 10378 | * Unserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10379 | */ |
| 10380 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 10381 | const unsigned char *buf, |
| 10382 | size_t len ) |
| 10383 | { |
| 10384 | int ret = ssl_session_load( session, buf, len ); |
| 10385 | |
| 10386 | if( ret != 0 ) |
| 10387 | mbedtls_ssl_session_free( session ); |
| 10388 | |
| 10389 | return( ret ); |
| 10390 | } |
| 10391 | |
| 10392 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10393 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10394 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10395 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10396 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10397 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10398 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10399 | if( ssl == NULL || ssl->conf == NULL ) |
| 10400 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10401 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10402 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10403 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10404 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10405 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10406 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10407 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10408 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10409 | #endif |
| 10410 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10411 | return( ret ); |
| 10412 | } |
| 10413 | |
| 10414 | /* |
| 10415 | * Perform the SSL handshake |
| 10416 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10417 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10418 | { |
| 10419 | int ret = 0; |
| 10420 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10421 | if( ssl == NULL || ssl->conf == NULL ) |
| 10422 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10424 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10425 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10426 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10427 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10428 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10429 | |
| 10430 | if( ret != 0 ) |
| 10431 | break; |
| 10432 | } |
| 10433 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10434 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10435 | |
| 10436 | return( ret ); |
| 10437 | } |
| 10438 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10439 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 10440 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10441 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10442 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10443 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10444 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10445 | { |
| 10446 | int ret; |
| 10447 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10448 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10449 | |
| 10450 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10451 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 10452 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10453 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 10454 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10455 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 10456 | 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] | 10457 | return( ret ); |
| 10458 | } |
| 10459 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10460 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10461 | |
| 10462 | return( 0 ); |
| 10463 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10464 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10465 | |
| 10466 | /* |
| 10467 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10468 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 10469 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 10470 | * - 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] | 10471 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10472 | * 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] | 10473 | * 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] | 10474 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10475 | static int ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10476 | { |
| 10477 | int ret; |
| 10478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10479 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10480 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10481 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 10482 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10483 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10484 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 10485 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10486 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10487 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10488 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10489 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10490 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 10491 | ssl->handshake->out_msg_seq = 1; |
| 10492 | else |
| 10493 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10494 | } |
| 10495 | #endif |
| 10496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10497 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 10498 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10499 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10500 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10501 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10502 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10503 | return( ret ); |
| 10504 | } |
| 10505 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10506 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10507 | |
| 10508 | return( 0 ); |
| 10509 | } |
| 10510 | |
| 10511 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10512 | * Renegotiate current connection on client, |
| 10513 | * or request renegotiation on server |
| 10514 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10515 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10516 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10517 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10518 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10519 | if( ssl == NULL || ssl->conf == NULL ) |
| 10520 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10521 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10522 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10523 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10524 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10525 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10526 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 10527 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10528 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10529 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 10530 | |
| 10531 | /* Did we already try/start sending HelloRequest? */ |
| 10532 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10533 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 10534 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10535 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10536 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10537 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10538 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10539 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10540 | /* |
| 10541 | * On client, either start the renegotiation process or, |
| 10542 | * if already in progress, continue the handshake |
| 10543 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10544 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10545 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10546 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 10547 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10548 | |
| 10549 | if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 ) |
| 10550 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10551 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10552 | return( ret ); |
| 10553 | } |
| 10554 | } |
| 10555 | else |
| 10556 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10557 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10558 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10559 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10560 | return( ret ); |
| 10561 | } |
| 10562 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10563 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10564 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 10565 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10566 | } |
| 10567 | |
| 10568 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10569 | * Check record counters and renegotiate if they're above the limit. |
| 10570 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10571 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10572 | { |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10573 | size_t ep_len = ssl_ep_len( ssl ); |
| 10574 | int in_ctr_cmp; |
| 10575 | int out_ctr_cmp; |
| 10576 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10577 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
| 10578 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10579 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10580 | { |
| 10581 | return( 0 ); |
| 10582 | } |
| 10583 | |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10584 | in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, |
| 10585 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 10586 | out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len, |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10587 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
| 10588 | |
| 10589 | if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10590 | { |
| 10591 | return( 0 ); |
| 10592 | } |
| 10593 | |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 10594 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10595 | return( mbedtls_ssl_renegotiate( ssl ) ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10596 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10597 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10598 | |
| 10599 | /* |
| 10600 | * Receive application data decrypted from the SSL layer |
| 10601 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10602 | 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] | 10603 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10604 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 10605 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10606 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10607 | if( ssl == NULL || ssl->conf == NULL ) |
| 10608 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10609 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10610 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10611 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10612 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10613 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10614 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10615 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10616 | return( ret ); |
| 10617 | |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10618 | if( ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10619 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10620 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 10621 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10622 | return( ret ); |
| 10623 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10624 | } |
| 10625 | #endif |
| 10626 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10627 | /* |
| 10628 | * Check if renegotiation is necessary and/or handshake is |
| 10629 | * in process. If yes, perform/continue, and fall through |
| 10630 | * if an unexpected packet is received while the client |
| 10631 | * is waiting for the ServerHello. |
| 10632 | * |
| 10633 | * (There is no equivalent to the last condition on |
| 10634 | * the server-side as it is not treated as within |
| 10635 | * a handshake while waiting for the ClientHello |
| 10636 | * after a renegotiation request.) |
| 10637 | */ |
| 10638 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10639 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10640 | ret = ssl_check_ctr_renegotiate( ssl ); |
| 10641 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10642 | ret != 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10643 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10644 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10645 | return( ret ); |
| 10646 | } |
| 10647 | #endif |
| 10648 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10649 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10650 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10651 | ret = mbedtls_ssl_handshake( ssl ); |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10652 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10653 | ret != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10654 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10655 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10656 | return( ret ); |
| 10657 | } |
| 10658 | } |
| 10659 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 10660 | /* Loop as long as no application data record is available */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10661 | while( ssl->in_offt == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10662 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10663 | /* Start timer if not already running */ |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10664 | if( ssl->f_get_timer != NULL && |
| 10665 | ssl->f_get_timer( ssl->p_timer ) == -1 ) |
| 10666 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10667 | ssl_set_timer( ssl, ssl->conf->read_timeout ); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10668 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10669 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10670 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10671 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10672 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
| 10673 | return( 0 ); |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10674 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10675 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 10676 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10677 | } |
| 10678 | |
| 10679 | if( ssl->in_msglen == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10680 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10681 | { |
| 10682 | /* |
| 10683 | * OpenSSL sends empty messages to randomize the IV |
| 10684 | */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10685 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10686 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10687 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10688 | return( 0 ); |
| 10689 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10690 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10691 | return( ret ); |
| 10692 | } |
| 10693 | } |
| 10694 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10695 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10696 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10697 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10698 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10699 | /* |
| 10700 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 10701 | * - For server-side, expect CLIENT_HELLO. |
| 10702 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 10703 | */ |
| 10704 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10705 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10706 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10707 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10708 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10709 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10710 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10711 | |
| 10712 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10713 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10714 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10715 | { |
| 10716 | continue; |
| 10717 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10718 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10719 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10720 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10721 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10722 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10723 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10724 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10725 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10726 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10727 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10728 | |
| 10729 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10730 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10731 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10732 | { |
| 10733 | continue; |
| 10734 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10735 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10736 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10737 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10738 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 10739 | |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10740 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10741 | /* Determine whether renegotiation attempt should be accepted */ |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10742 | if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 10743 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 10744 | ssl->conf->allow_legacy_renegotiation == |
| 10745 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) ) |
| 10746 | { |
| 10747 | /* |
| 10748 | * Accept renegotiation request |
| 10749 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10750 | |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10751 | /* DTLS clients need to know renego is server-initiated */ |
| 10752 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10753 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 10754 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 10755 | { |
| 10756 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 10757 | } |
| 10758 | #endif |
| 10759 | ret = ssl_start_renegotiation( ssl ); |
| 10760 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10761 | ret != 0 ) |
| 10762 | { |
| 10763 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
| 10764 | return( ret ); |
| 10765 | } |
| 10766 | } |
| 10767 | else |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10768 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10769 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10770 | /* |
| 10771 | * Refuse renegotiation |
| 10772 | */ |
| 10773 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10774 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10775 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10776 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 10777 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10778 | { |
Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 10779 | /* SSLv3 does not have a "no_renegotiation" warning, so |
| 10780 | we send a fatal alert and abort the connection. */ |
| 10781 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 10782 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 10783 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10784 | } |
| 10785 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10786 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 10787 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 10788 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10789 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10790 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10791 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 10792 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 10793 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10794 | { |
| 10795 | return( ret ); |
| 10796 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10797 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 10798 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10799 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || |
| 10800 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 10801 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10802 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 10803 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 10804 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10805 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10806 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10807 | /* At this point, we don't know whether the renegotiation has been |
| 10808 | * completed or not. The cases to consider are the following: |
| 10809 | * 1) The renegotiation is complete. In this case, no new record |
| 10810 | * has been read yet. |
| 10811 | * 2) The renegotiation is incomplete because the client received |
| 10812 | * an application data record while awaiting the ServerHello. |
| 10813 | * 3) The renegotiation is incomplete because the client received |
| 10814 | * a non-handshake, non-application data message while awaiting |
| 10815 | * the ServerHello. |
| 10816 | * In each of these case, looping will be the proper action: |
| 10817 | * - For 1), the next iteration will read a new record and check |
| 10818 | * if it's application data. |
| 10819 | * - For 2), the loop condition isn't satisfied as application data |
| 10820 | * is present, hence continue is the same as break |
| 10821 | * - For 3), the loop condition is satisfied and read_record |
| 10822 | * will re-deliver the message that was held back by the client |
| 10823 | * when expecting the ServerHello. |
| 10824 | */ |
| 10825 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10826 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10827 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10828 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 10829 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10830 | if( ssl->conf->renego_max_records >= 0 ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 10831 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10832 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10833 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10834 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10835 | "but not honored by client" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10836 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10837 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 10838 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 10839 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10840 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10841 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10842 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
| 10843 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10844 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10845 | 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] | 10846 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10847 | } |
| 10848 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10849 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10850 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10851 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); |
| 10852 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10853 | } |
| 10854 | |
| 10855 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10856 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 10857 | /* We're going to return something now, cancel timer, |
| 10858 | * except if handshake (renegotiation) is in progress */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10859 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 10860 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10861 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 10862 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10863 | /* If we requested renego but received AppData, resend HelloRequest. |
| 10864 | * Do it now, after setting in_offt, to avoid taking this branch |
| 10865 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10866 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10867 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10868 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10869 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10870 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10871 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10872 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10873 | return( ret ); |
| 10874 | } |
| 10875 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10876 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10877 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10878 | } |
| 10879 | |
| 10880 | n = ( len < ssl->in_msglen ) |
| 10881 | ? len : ssl->in_msglen; |
| 10882 | |
| 10883 | memcpy( buf, ssl->in_offt, n ); |
| 10884 | ssl->in_msglen -= n; |
| 10885 | |
| 10886 | if( ssl->in_msglen == 0 ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10887 | { |
| 10888 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10889 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 10890 | ssl->keep_current_message = 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10891 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10892 | else |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10893 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10894 | /* more data available */ |
| 10895 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10896 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10897 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10898 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10899 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 10900 | return( (int) n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10901 | } |
| 10902 | |
| 10903 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10904 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 10905 | * fragment length and buffer size. |
| 10906 | * |
| 10907 | * According to RFC 5246 Section 6.2.1: |
| 10908 | * |
| 10909 | * Zero-length fragments of Application data MAY be sent as they are |
| 10910 | * potentially useful as a traffic analysis countermeasure. |
| 10911 | * |
| 10912 | * Therefore, it is possible that the input message length is 0 and the |
| 10913 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10914 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10915 | static int ssl_write_real( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10916 | const unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10917 | { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 10918 | int ret = mbedtls_ssl_get_max_out_record_payload( ssl ); |
| 10919 | const size_t max_len = (size_t) ret; |
| 10920 | |
| 10921 | if( ret < 0 ) |
| 10922 | { |
| 10923 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret ); |
| 10924 | return( ret ); |
| 10925 | } |
| 10926 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10927 | if( len > max_len ) |
| 10928 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10929 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10930 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10931 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10932 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10933 | "maximum fragment length: %d > %d", |
| 10934 | len, max_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10935 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10936 | } |
| 10937 | else |
| 10938 | #endif |
| 10939 | len = max_len; |
| 10940 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10941 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10942 | if( ssl->out_left != 0 ) |
| 10943 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10944 | /* |
| 10945 | * The user has previously tried to send the data and |
| 10946 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 10947 | * written. In this case, we expect the high-level write function |
| 10948 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 10949 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10950 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10951 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10952 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10953 | return( ret ); |
| 10954 | } |
| 10955 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10956 | else |
Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 10957 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10958 | /* |
| 10959 | * The user is trying to send a message the first time, so we need to |
| 10960 | * copy the data into the internal buffers and setup the data structure |
| 10961 | * to keep track of partial writes |
| 10962 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10963 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10964 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10965 | memcpy( ssl->out_msg, buf, len ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10966 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 10967 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10968 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10969 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10970 | return( ret ); |
| 10971 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10972 | } |
| 10973 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10974 | return( (int) len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10975 | } |
| 10976 | |
| 10977 | /* |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10978 | * Write application data, doing 1/n-1 splitting if necessary. |
| 10979 | * |
| 10980 | * 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] | 10981 | * then the caller will call us again with the same arguments, so |
Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 10982 | * remember whether we already did the split or not. |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10983 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10984 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10985 | static int ssl_write_split( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10986 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10987 | { |
| 10988 | int ret; |
| 10989 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 10990 | if( ssl->conf->cbc_record_splitting == |
| 10991 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 10992 | len <= 1 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10993 | ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 || |
| 10994 | mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) |
| 10995 | != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10996 | { |
| 10997 | return( ssl_write_real( ssl, buf, len ) ); |
| 10998 | } |
| 10999 | |
| 11000 | if( ssl->split_done == 0 ) |
| 11001 | { |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 11002 | if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11003 | return( ret ); |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 11004 | ssl->split_done = 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11005 | } |
| 11006 | |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 11007 | if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) |
| 11008 | return( ret ); |
| 11009 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11010 | |
| 11011 | return( ret + 1 ); |
| 11012 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11013 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11014 | |
| 11015 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11016 | * Write application data (public-facing wrapper) |
| 11017 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11018 | 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] | 11019 | { |
| 11020 | int ret; |
| 11021 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11022 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11023 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 11024 | if( ssl == NULL || ssl->conf == NULL ) |
| 11025 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11026 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11027 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11028 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) |
| 11029 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11030 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11031 | return( ret ); |
| 11032 | } |
| 11033 | #endif |
| 11034 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11035 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11036 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11037 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11038 | { |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 11039 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11040 | return( ret ); |
| 11041 | } |
| 11042 | } |
| 11043 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11044 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11045 | ret = ssl_write_split( ssl, buf, len ); |
| 11046 | #else |
| 11047 | ret = ssl_write_real( ssl, buf, len ); |
| 11048 | #endif |
| 11049 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11050 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11051 | |
| 11052 | return( ret ); |
| 11053 | } |
| 11054 | |
| 11055 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11056 | * Notify the peer that the connection is being closed |
| 11057 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11058 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11059 | { |
| 11060 | int ret; |
| 11061 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 11062 | if( ssl == NULL || ssl->conf == NULL ) |
| 11063 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11064 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11065 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11066 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 11067 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11068 | return( mbedtls_ssl_flush_output( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11069 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11070 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11071 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11072 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 11073 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 11074 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11075 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11076 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11077 | return( ret ); |
| 11078 | } |
| 11079 | } |
| 11080 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11081 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11082 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 11083 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11084 | } |
| 11085 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11086 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11087 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11088 | if( transform == NULL ) |
| 11089 | return; |
| 11090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11091 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11092 | deflateEnd( &transform->ctx_deflate ); |
| 11093 | inflateEnd( &transform->ctx_inflate ); |
| 11094 | #endif |
| 11095 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11096 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); |
| 11097 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 11098 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 11099 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11100 | mbedtls_md_free( &transform->md_ctx_enc ); |
| 11101 | mbedtls_md_free( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 11102 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 11103 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11104 | mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11105 | } |
| 11106 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11107 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 11108 | 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] | 11109 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11110 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 11111 | |
| 11112 | while( cur != NULL ) |
| 11113 | { |
| 11114 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11115 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 11116 | cur = next; |
| 11117 | } |
| 11118 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11119 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 11120 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11121 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11122 | |
| 11123 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ) |
| 11124 | { |
| 11125 | unsigned offset; |
| 11126 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 11127 | |
| 11128 | if( hs == NULL ) |
| 11129 | return; |
| 11130 | |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 11131 | ssl_free_buffered_record( ssl ); |
| 11132 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11133 | for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 11134 | ssl_buffering_free_slot( ssl, offset ); |
| 11135 | } |
| 11136 | |
| 11137 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 11138 | uint8_t slot ) |
| 11139 | { |
| 11140 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 11141 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 11142 | |
| 11143 | if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 11144 | return; |
| 11145 | |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 11146 | if( hs_buf->is_valid == 1 ) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11147 | { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 11148 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Hanno Becker | 805f2e1 | 2018-10-12 16:31:41 +0100 | [diff] [blame] | 11149 | mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len ); |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 11150 | mbedtls_free( hs_buf->data ); |
| 11151 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11152 | } |
| 11153 | } |
| 11154 | |
| 11155 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 11156 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 11157 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11158 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 11159 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 11160 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11161 | if( handshake == NULL ) |
| 11162 | return; |
| 11163 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 11164 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 11165 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 11166 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 11167 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 11168 | handshake->async_in_progress = 0; |
| 11169 | } |
| 11170 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 11171 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 11172 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 11173 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 11174 | mbedtls_md5_free( &handshake->fin_md5 ); |
| 11175 | mbedtls_sha1_free( &handshake->fin_sha1 ); |
| 11176 | #endif |
| 11177 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 11178 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 11179 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 11180 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 11181 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 11182 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 11183 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 11184 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 11185 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 11186 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 11187 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 11188 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 11189 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 11190 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 11191 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 11192 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 11193 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11194 | #if defined(MBEDTLS_DHM_C) |
| 11195 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11196 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11197 | #if defined(MBEDTLS_ECDH_C) |
| 11198 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 11199 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 11200 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 11201 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 11202 | #if defined(MBEDTLS_SSL_CLI_C) |
| 11203 | mbedtls_free( handshake->ecjpake_cache ); |
| 11204 | handshake->ecjpake_cache = NULL; |
| 11205 | handshake->ecjpake_cache_len = 0; |
| 11206 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 11207 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 11208 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 11209 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 11210 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 11211 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11212 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 11213 | #endif |
| 11214 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 11215 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 11216 | if( handshake->psk != NULL ) |
| 11217 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11218 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 11219 | mbedtls_free( handshake->psk ); |
| 11220 | } |
| 11221 | #endif |
| 11222 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11223 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 11224 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 11225 | /* |
| 11226 | * Free only the linked list wrapper, not the keys themselves |
| 11227 | * since the belong to the SNI callback |
| 11228 | */ |
| 11229 | if( handshake->sni_key_cert != NULL ) |
| 11230 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11231 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 11232 | |
| 11233 | while( cur != NULL ) |
| 11234 | { |
| 11235 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11236 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 11237 | cur = next; |
| 11238 | } |
| 11239 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11240 | #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] | 11241 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 11242 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 11243 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 11244 | if( handshake->ecrs_peer_cert != NULL ) |
| 11245 | { |
| 11246 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 11247 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 11248 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 11249 | #endif |
| 11250 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 11251 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 11252 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 11253 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 11254 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 11255 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11256 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11257 | mbedtls_free( handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 11258 | ssl_flight_free( handshake->flight ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11259 | ssl_buffering_free( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 11260 | #endif |
| 11261 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 11262 | #if defined(MBEDTLS_ECDH_C) && \ |
| 11263 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 11264 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 11265 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 11266 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11267 | mbedtls_platform_zeroize( handshake, |
| 11268 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11269 | } |
| 11270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11271 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11272 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11273 | if( session == NULL ) |
| 11274 | return; |
| 11275 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11276 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 11277 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 11278 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 11279 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 11280 | #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] | 11281 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 11282 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 11283 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11284 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11285 | } |
| 11286 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11287 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11288 | * Serialize a full SSL context |
| 11289 | */ |
| 11290 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 11291 | unsigned char *buf, |
| 11292 | size_t buf_len, |
| 11293 | size_t *olen ) |
| 11294 | { |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame^] | 11295 | /* |
| 11296 | * Enforce current usage restrictions |
| 11297 | */ |
| 11298 | if( /* The initial handshake is over ... */ |
| 11299 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
| 11300 | ssl->handshake != NULL || |
| 11301 | /* ... and the various sub-structures are indeed ready. */ |
| 11302 | ssl->transform == NULL || |
| 11303 | ssl->session == NULL || |
| 11304 | /* There is no pending incoming or outgoing data ... */ |
| 11305 | mbedtls_ssl_check_pending( ssl ) != 0 || |
| 11306 | ssl->out_left != 0 || |
| 11307 | /* We're using DTLS 1.2 ... */ |
| 11308 | ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 11309 | ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 11310 | ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 || |
| 11311 | /* ... with an AEAD ciphersuite. */ |
| 11312 | mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 || |
| 11313 | /* Renegotation is disabled. */ |
| 11314 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 11315 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED |
| 11316 | #endif |
| 11317 | ) |
| 11318 | { |
| 11319 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 11320 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11321 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame^] | 11322 | /* Unimplemented */ |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11323 | if( buf != NULL ) |
| 11324 | memset( buf, 0, buf_len ); |
| 11325 | |
| 11326 | *olen = 0; |
| 11327 | |
| 11328 | return( 0 ); |
| 11329 | } |
| 11330 | |
| 11331 | /* |
| 11332 | * Deserialize a full SSL context |
| 11333 | */ |
| 11334 | int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl, |
| 11335 | const unsigned char *buf, |
| 11336 | size_t len ) |
| 11337 | { |
Manuel Pégourié-Gonnard | 6d8f128 | 2019-06-05 09:47:18 +0200 | [diff] [blame] | 11338 | /* Unimplemented */ |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11339 | (void) ssl; |
| 11340 | (void) buf; |
| 11341 | (void) len; |
| 11342 | |
| 11343 | return( 0 ); |
| 11344 | } |
| 11345 | |
| 11346 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11347 | * Free an SSL context |
| 11348 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11349 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11350 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11351 | if( ssl == NULL ) |
| 11352 | return; |
| 11353 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11354 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11355 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 11356 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11357 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 11358 | 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] | 11359 | mbedtls_free( ssl->out_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11360 | } |
| 11361 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 11362 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11363 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 11364 | 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] | 11365 | mbedtls_free( ssl->in_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11366 | } |
| 11367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11368 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 11369 | if( ssl->compress_buf != NULL ) |
| 11370 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 11371 | 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] | 11372 | mbedtls_free( ssl->compress_buf ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 11373 | } |
| 11374 | #endif |
| 11375 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11376 | if( ssl->transform ) |
| 11377 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11378 | mbedtls_ssl_transform_free( ssl->transform ); |
| 11379 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11380 | } |
| 11381 | |
| 11382 | if( ssl->handshake ) |
| 11383 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 11384 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11385 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 11386 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11387 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11388 | mbedtls_free( ssl->handshake ); |
| 11389 | mbedtls_free( ssl->transform_negotiate ); |
| 11390 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11391 | } |
| 11392 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 11393 | if( ssl->session ) |
| 11394 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11395 | mbedtls_ssl_session_free( ssl->session ); |
| 11396 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 11397 | } |
| 11398 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 11399 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 11400 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11401 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11402 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11403 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11404 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 11405 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11406 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11407 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 11408 | if( mbedtls_ssl_hw_record_finish != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 11409 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11410 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); |
| 11411 | mbedtls_ssl_hw_record_finish( ssl ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 11412 | } |
| 11413 | #endif |
| 11414 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 11415 | #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] | 11416 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 11417 | #endif |
| 11418 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11419 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 11420 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 11421 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11422 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11423 | } |
| 11424 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11425 | /* |
| 11426 | * Initialze mbedtls_ssl_config |
| 11427 | */ |
| 11428 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 11429 | { |
| 11430 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 11431 | } |
| 11432 | |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 11433 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 11434 | static int ssl_preset_default_hashes[] = { |
| 11435 | #if defined(MBEDTLS_SHA512_C) |
| 11436 | MBEDTLS_MD_SHA512, |
| 11437 | MBEDTLS_MD_SHA384, |
| 11438 | #endif |
| 11439 | #if defined(MBEDTLS_SHA256_C) |
| 11440 | MBEDTLS_MD_SHA256, |
| 11441 | MBEDTLS_MD_SHA224, |
| 11442 | #endif |
Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 11443 | #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] | 11444 | MBEDTLS_MD_SHA1, |
| 11445 | #endif |
| 11446 | MBEDTLS_MD_NONE |
| 11447 | }; |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 11448 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 11449 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11450 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 11451 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 11452 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 11453 | 0 |
| 11454 | }; |
| 11455 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11456 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11457 | static int ssl_preset_suiteb_hashes[] = { |
| 11458 | MBEDTLS_MD_SHA256, |
| 11459 | MBEDTLS_MD_SHA384, |
| 11460 | MBEDTLS_MD_NONE |
| 11461 | }; |
| 11462 | #endif |
| 11463 | |
| 11464 | #if defined(MBEDTLS_ECP_C) |
| 11465 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 11466 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11467 | MBEDTLS_ECP_DP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 11468 | #endif |
| 11469 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11470 | MBEDTLS_ECP_DP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 11471 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11472 | MBEDTLS_ECP_DP_NONE |
| 11473 | }; |
| 11474 | #endif |
| 11475 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11476 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 11477 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11478 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 11479 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11480 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11481 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 11482 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11483 | int ret; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 11484 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11485 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 11486 | /* Use the functions here so that they are covered in tests, |
| 11487 | * but otherwise access member directly for efficiency */ |
| 11488 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 11489 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11490 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11491 | /* |
| 11492 | * Things that are common to all presets |
| 11493 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 11494 | #if defined(MBEDTLS_SSL_CLI_C) |
| 11495 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 11496 | { |
| 11497 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 11498 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 11499 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 11500 | #endif |
| 11501 | } |
| 11502 | #endif |
| 11503 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 11504 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11505 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 11506 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11507 | |
| 11508 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 11509 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 11510 | #endif |
| 11511 | |
| 11512 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 11513 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 11514 | #endif |
| 11515 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 11516 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 11517 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; |
| 11518 | #endif |
| 11519 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 11520 | #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] | 11521 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 11522 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 11523 | #endif |
| 11524 | |
| 11525 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 11526 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 11527 | #endif |
| 11528 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 11529 | #if defined(MBEDTLS_SSL_SRV_C) |
| 11530 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
| 11531 | #endif |
| 11532 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11533 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11534 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 11535 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 11536 | #endif |
| 11537 | |
| 11538 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 11539 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 11540 | memset( conf->renego_period, 0x00, 2 ); |
| 11541 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11542 | #endif |
| 11543 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11544 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 11545 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 11546 | { |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 11547 | const unsigned char dhm_p[] = |
| 11548 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 11549 | const unsigned char dhm_g[] = |
| 11550 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
| 11551 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 11552 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 11553 | dhm_p, sizeof( dhm_p ), |
| 11554 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11555 | { |
| 11556 | return( ret ); |
| 11557 | } |
| 11558 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 11559 | #endif |
| 11560 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11561 | /* |
| 11562 | * Preset-specific defaults |
| 11563 | */ |
| 11564 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11565 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11566 | /* |
| 11567 | * NSA Suite B |
| 11568 | */ |
| 11569 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 11570 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 11571 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 11572 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 11573 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 11574 | |
| 11575 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 11576 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 11577 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 11578 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 11579 | ssl_preset_suiteb_ciphersuites; |
| 11580 | |
| 11581 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 11582 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11583 | #endif |
| 11584 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11585 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11586 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
| 11587 | #endif |
| 11588 | |
| 11589 | #if defined(MBEDTLS_ECP_C) |
| 11590 | conf->curve_list = ssl_preset_suiteb_curves; |
| 11591 | #endif |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 11592 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11593 | |
| 11594 | /* |
| 11595 | * Default |
| 11596 | */ |
| 11597 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 11598 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 11599 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 11600 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 11601 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 11602 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 11603 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 11604 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 11605 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11606 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 11607 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 11608 | |
| 11609 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11610 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 11611 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 11612 | #endif |
| 11613 | |
| 11614 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 11615 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 11616 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 11617 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 11618 | mbedtls_ssl_list_ciphersuites(); |
| 11619 | |
| 11620 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 11621 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 11622 | #endif |
| 11623 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11624 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 11625 | conf->sig_hashes = ssl_preset_default_hashes; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 11626 | #endif |
| 11627 | |
| 11628 | #if defined(MBEDTLS_ECP_C) |
| 11629 | conf->curve_list = mbedtls_ecp_grp_id_list(); |
| 11630 | #endif |
| 11631 | |
| 11632 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 11633 | conf->dhm_min_bitlen = 1024; |
| 11634 | #endif |
| 11635 | } |
| 11636 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11637 | return( 0 ); |
| 11638 | } |
| 11639 | |
| 11640 | /* |
| 11641 | * Free mbedtls_ssl_config |
| 11642 | */ |
| 11643 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 11644 | { |
| 11645 | #if defined(MBEDTLS_DHM_C) |
| 11646 | mbedtls_mpi_free( &conf->dhm_P ); |
| 11647 | mbedtls_mpi_free( &conf->dhm_G ); |
| 11648 | #endif |
| 11649 | |
| 11650 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 11651 | if( conf->psk != NULL ) |
| 11652 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11653 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11654 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 11655 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11656 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 11657 | } |
| 11658 | |
| 11659 | if( conf->psk_identity != NULL ) |
| 11660 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11661 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 11662 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 11663 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11664 | conf->psk_identity_len = 0; |
| 11665 | } |
| 11666 | #endif |
| 11667 | |
| 11668 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 11669 | ssl_key_cert_free( conf->key_cert ); |
| 11670 | #endif |
| 11671 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11672 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11673 | } |
| 11674 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 11675 | #if defined(MBEDTLS_PK_C) && \ |
| 11676 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11677 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11678 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11679 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11680 | 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] | 11681 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11682 | #if defined(MBEDTLS_RSA_C) |
| 11683 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 11684 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11685 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11686 | #if defined(MBEDTLS_ECDSA_C) |
| 11687 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 11688 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11689 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11690 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11691 | } |
| 11692 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 11693 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 11694 | { |
| 11695 | switch( type ) { |
| 11696 | case MBEDTLS_PK_RSA: |
| 11697 | return( MBEDTLS_SSL_SIG_RSA ); |
| 11698 | case MBEDTLS_PK_ECDSA: |
| 11699 | case MBEDTLS_PK_ECKEY: |
| 11700 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 11701 | default: |
| 11702 | return( MBEDTLS_SSL_SIG_ANON ); |
| 11703 | } |
| 11704 | } |
| 11705 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11706 | 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] | 11707 | { |
| 11708 | switch( sig ) |
| 11709 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11710 | #if defined(MBEDTLS_RSA_C) |
| 11711 | case MBEDTLS_SSL_SIG_RSA: |
| 11712 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11713 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11714 | #if defined(MBEDTLS_ECDSA_C) |
| 11715 | case MBEDTLS_SSL_SIG_ECDSA: |
| 11716 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11717 | #endif |
| 11718 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11719 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11720 | } |
| 11721 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 11722 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11723 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 11724 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 11725 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 11726 | |
| 11727 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 11728 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 11729 | mbedtls_pk_type_t sig_alg ) |
| 11730 | { |
| 11731 | switch( sig_alg ) |
| 11732 | { |
| 11733 | case MBEDTLS_PK_RSA: |
| 11734 | return( set->rsa ); |
| 11735 | case MBEDTLS_PK_ECDSA: |
| 11736 | return( set->ecdsa ); |
| 11737 | default: |
| 11738 | return( MBEDTLS_MD_NONE ); |
| 11739 | } |
| 11740 | } |
| 11741 | |
| 11742 | /* Add a signature-hash-pair to a signature-hash set */ |
| 11743 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 11744 | mbedtls_pk_type_t sig_alg, |
| 11745 | mbedtls_md_type_t md_alg ) |
| 11746 | { |
| 11747 | switch( sig_alg ) |
| 11748 | { |
| 11749 | case MBEDTLS_PK_RSA: |
| 11750 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 11751 | set->rsa = md_alg; |
| 11752 | break; |
| 11753 | |
| 11754 | case MBEDTLS_PK_ECDSA: |
| 11755 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 11756 | set->ecdsa = md_alg; |
| 11757 | break; |
| 11758 | |
| 11759 | default: |
| 11760 | break; |
| 11761 | } |
| 11762 | } |
| 11763 | |
| 11764 | /* Allow exactly one hash algorithm for each signature. */ |
| 11765 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 11766 | mbedtls_md_type_t md_alg ) |
| 11767 | { |
| 11768 | set->rsa = md_alg; |
| 11769 | set->ecdsa = md_alg; |
| 11770 | } |
| 11771 | |
| 11772 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
| 11773 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 11774 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 11775 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11776 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 11777 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11778 | 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] | 11779 | { |
| 11780 | switch( hash ) |
| 11781 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11782 | #if defined(MBEDTLS_MD5_C) |
| 11783 | case MBEDTLS_SSL_HASH_MD5: |
| 11784 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11785 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11786 | #if defined(MBEDTLS_SHA1_C) |
| 11787 | case MBEDTLS_SSL_HASH_SHA1: |
| 11788 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11789 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11790 | #if defined(MBEDTLS_SHA256_C) |
| 11791 | case MBEDTLS_SSL_HASH_SHA224: |
| 11792 | return( MBEDTLS_MD_SHA224 ); |
| 11793 | case MBEDTLS_SSL_HASH_SHA256: |
| 11794 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11795 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11796 | #if defined(MBEDTLS_SHA512_C) |
| 11797 | case MBEDTLS_SSL_HASH_SHA384: |
| 11798 | return( MBEDTLS_MD_SHA384 ); |
| 11799 | case MBEDTLS_SSL_HASH_SHA512: |
| 11800 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11801 | #endif |
| 11802 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11803 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11804 | } |
| 11805 | } |
| 11806 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11807 | /* |
| 11808 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 11809 | */ |
| 11810 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 11811 | { |
| 11812 | switch( md ) |
| 11813 | { |
| 11814 | #if defined(MBEDTLS_MD5_C) |
| 11815 | case MBEDTLS_MD_MD5: |
| 11816 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 11817 | #endif |
| 11818 | #if defined(MBEDTLS_SHA1_C) |
| 11819 | case MBEDTLS_MD_SHA1: |
| 11820 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 11821 | #endif |
| 11822 | #if defined(MBEDTLS_SHA256_C) |
| 11823 | case MBEDTLS_MD_SHA224: |
| 11824 | return( MBEDTLS_SSL_HASH_SHA224 ); |
| 11825 | case MBEDTLS_MD_SHA256: |
| 11826 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 11827 | #endif |
| 11828 | #if defined(MBEDTLS_SHA512_C) |
| 11829 | case MBEDTLS_MD_SHA384: |
| 11830 | return( MBEDTLS_SSL_HASH_SHA384 ); |
| 11831 | case MBEDTLS_MD_SHA512: |
| 11832 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 11833 | #endif |
| 11834 | default: |
| 11835 | return( MBEDTLS_SSL_HASH_NONE ); |
| 11836 | } |
| 11837 | } |
| 11838 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 11839 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11840 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11841 | * 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] | 11842 | * 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] | 11843 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 11844 | 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] | 11845 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11846 | const mbedtls_ecp_group_id *gid; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11847 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 11848 | if( ssl->conf->curve_list == NULL ) |
| 11849 | return( -1 ); |
| 11850 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 11851 | 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] | 11852 | if( *gid == grp_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 11853 | return( 0 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11854 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 11855 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11856 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 11857 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11858 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11859 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11860 | /* |
| 11861 | * Check if a hash proposed by the peer is in our list. |
| 11862 | * Return 0 if we're willing to use it, -1 otherwise. |
| 11863 | */ |
| 11864 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 11865 | mbedtls_md_type_t md ) |
| 11866 | { |
| 11867 | const int *cur; |
| 11868 | |
| 11869 | if( ssl->conf->sig_hashes == NULL ) |
| 11870 | return( -1 ); |
| 11871 | |
| 11872 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) |
| 11873 | if( *cur == (int) md ) |
| 11874 | return( 0 ); |
| 11875 | |
| 11876 | return( -1 ); |
| 11877 | } |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11878 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11879 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11880 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 11881 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 11882 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11883 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 11884 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11885 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11886 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11887 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11888 | int usage = 0; |
| 11889 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11890 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11891 | const char *ext_oid; |
| 11892 | size_t ext_len; |
| 11893 | #endif |
| 11894 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11895 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \ |
| 11896 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11897 | ((void) cert); |
| 11898 | ((void) cert_endpoint); |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11899 | ((void) flags); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11900 | #endif |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11901 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11902 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 11903 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11904 | { |
| 11905 | /* Server part of the key exchange */ |
| 11906 | switch( ciphersuite->key_exchange ) |
| 11907 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11908 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 11909 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11910 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11911 | break; |
| 11912 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11913 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 11914 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 11915 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 11916 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11917 | break; |
| 11918 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11919 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 11920 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11921 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11922 | break; |
| 11923 | |
| 11924 | /* 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] | 11925 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 11926 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 11927 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 11928 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 11929 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11930 | usage = 0; |
| 11931 | } |
| 11932 | } |
| 11933 | else |
| 11934 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11935 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 11936 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11937 | } |
| 11938 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11939 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11940 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11941 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11942 | ret = -1; |
| 11943 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11944 | #else |
| 11945 | ((void) ciphersuite); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11946 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11947 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11948 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 11949 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11950 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11951 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 11952 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11953 | } |
| 11954 | else |
| 11955 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11956 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 11957 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11958 | } |
| 11959 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11960 | 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] | 11961 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11962 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11963 | ret = -1; |
| 11964 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11965 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11966 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11967 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11968 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11969 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 11970 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11971 | /* |
| 11972 | * Convert version numbers to/from wire format |
| 11973 | * and, for DTLS, to/from TLS equivalent. |
| 11974 | * |
| 11975 | * For TLS this is the identity. |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 11976 | * 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] | 11977 | * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1) |
| 11978 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
| 11979 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11980 | 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] | 11981 | unsigned char ver[2] ) |
| 11982 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11983 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11984 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11985 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11986 | if( minor == MBEDTLS_SSL_MINOR_VERSION_2 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11987 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 11988 | |
| 11989 | ver[0] = (unsigned char)( 255 - ( major - 2 ) ); |
| 11990 | ver[1] = (unsigned char)( 255 - ( minor - 1 ) ); |
| 11991 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 11992 | else |
| 11993 | #else |
| 11994 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11995 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 11996 | { |
| 11997 | ver[0] = (unsigned char) major; |
| 11998 | ver[1] = (unsigned char) minor; |
| 11999 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12000 | } |
| 12001 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12002 | 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] | 12003 | const unsigned char ver[2] ) |
| 12004 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12005 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 12006 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12007 | { |
| 12008 | *major = 255 - ver[0] + 2; |
| 12009 | *minor = 255 - ver[1] + 1; |
| 12010 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12011 | if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12012 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 12013 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 12014 | else |
| 12015 | #else |
| 12016 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12017 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 12018 | { |
| 12019 | *major = ver[0]; |
| 12020 | *minor = ver[1]; |
| 12021 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 12022 | } |
| 12023 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12024 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 12025 | { |
| 12026 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 12027 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 12028 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 12029 | |
| 12030 | switch( md ) |
| 12031 | { |
| 12032 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 12033 | #if defined(MBEDTLS_MD5_C) |
| 12034 | case MBEDTLS_SSL_HASH_MD5: |
Janos Follath | 182013f | 2016-10-25 10:50:22 +0100 | [diff] [blame] | 12035 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 12036 | #endif |
| 12037 | #if defined(MBEDTLS_SHA1_C) |
| 12038 | case MBEDTLS_SSL_HASH_SHA1: |
| 12039 | ssl->handshake->calc_verify = ssl_calc_verify_tls; |
| 12040 | break; |
| 12041 | #endif |
| 12042 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 12043 | #if defined(MBEDTLS_SHA512_C) |
| 12044 | case MBEDTLS_SSL_HASH_SHA384: |
| 12045 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 12046 | break; |
| 12047 | #endif |
| 12048 | #if defined(MBEDTLS_SHA256_C) |
| 12049 | case MBEDTLS_SSL_HASH_SHA256: |
| 12050 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 12051 | break; |
| 12052 | #endif |
| 12053 | default: |
| 12054 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 12055 | } |
| 12056 | |
| 12057 | return 0; |
| 12058 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 12059 | (void) ssl; |
| 12060 | (void) md; |
| 12061 | |
| 12062 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 12063 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 12064 | } |
| 12065 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12066 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 12067 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 12068 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, |
| 12069 | unsigned char *output, |
| 12070 | unsigned char *data, size_t data_len ) |
| 12071 | { |
| 12072 | int ret = 0; |
| 12073 | mbedtls_md5_context mbedtls_md5; |
| 12074 | mbedtls_sha1_context mbedtls_sha1; |
| 12075 | |
| 12076 | mbedtls_md5_init( &mbedtls_md5 ); |
| 12077 | mbedtls_sha1_init( &mbedtls_sha1 ); |
| 12078 | |
| 12079 | /* |
| 12080 | * digitally-signed struct { |
| 12081 | * opaque md5_hash[16]; |
| 12082 | * opaque sha_hash[20]; |
| 12083 | * }; |
| 12084 | * |
| 12085 | * md5_hash |
| 12086 | * MD5(ClientHello.random + ServerHello.random |
| 12087 | * + ServerParams); |
| 12088 | * sha_hash |
| 12089 | * SHA(ClientHello.random + ServerHello.random |
| 12090 | * + ServerParams); |
| 12091 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12092 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12093 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12094 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12095 | goto exit; |
| 12096 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12097 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12098 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 12099 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12100 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12101 | goto exit; |
| 12102 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12103 | 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] | 12104 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12105 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12106 | goto exit; |
| 12107 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12108 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12109 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12110 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12111 | goto exit; |
| 12112 | } |
| 12113 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12114 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12115 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12116 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12117 | goto exit; |
| 12118 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12119 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12120 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 12121 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12122 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12123 | goto exit; |
| 12124 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12125 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12126 | data_len ) ) != 0 ) |
| 12127 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12128 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12129 | goto exit; |
| 12130 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12131 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12132 | output + 16 ) ) != 0 ) |
| 12133 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12134 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12135 | goto exit; |
| 12136 | } |
| 12137 | |
| 12138 | exit: |
| 12139 | mbedtls_md5_free( &mbedtls_md5 ); |
| 12140 | mbedtls_sha1_free( &mbedtls_sha1 ); |
| 12141 | |
| 12142 | if( ret != 0 ) |
| 12143 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 12144 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 12145 | |
| 12146 | return( ret ); |
| 12147 | |
| 12148 | } |
| 12149 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 12150 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 12151 | |
| 12152 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 12153 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12154 | |
| 12155 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 12156 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 12157 | unsigned char *hash, size_t *hashlen, |
| 12158 | unsigned char *data, size_t data_len, |
| 12159 | mbedtls_md_type_t md_alg ) |
| 12160 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12161 | psa_status_t status; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 12162 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12163 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 12164 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 12165 | 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] | 12166 | |
| 12167 | if( ( status = psa_hash_setup( &hash_operation, |
| 12168 | hash_alg ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12169 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12170 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12171 | goto exit; |
| 12172 | } |
| 12173 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12174 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 12175 | 64 ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12176 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12177 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12178 | goto exit; |
| 12179 | } |
| 12180 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12181 | if( ( status = psa_hash_update( &hash_operation, |
| 12182 | data, data_len ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12183 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12184 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12185 | goto exit; |
| 12186 | } |
| 12187 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12188 | if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE, |
| 12189 | hashlen ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12190 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12191 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12192 | goto exit; |
| 12193 | } |
| 12194 | |
| 12195 | exit: |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12196 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12197 | { |
| 12198 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 12199 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12200 | switch( status ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12201 | { |
| 12202 | case PSA_ERROR_NOT_SUPPORTED: |
| 12203 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 12204 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12205 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 12206 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 12207 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 12208 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 12209 | default: |
| 12210 | return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED ); |
| 12211 | } |
| 12212 | } |
| 12213 | return( 0 ); |
| 12214 | } |
| 12215 | |
| 12216 | #else |
| 12217 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12218 | 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] | 12219 | unsigned char *hash, size_t *hashlen, |
| 12220 | unsigned char *data, size_t data_len, |
| 12221 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12222 | { |
| 12223 | int ret = 0; |
| 12224 | mbedtls_md_context_t ctx; |
| 12225 | 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] | 12226 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12227 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 12228 | 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] | 12229 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12230 | mbedtls_md_init( &ctx ); |
| 12231 | |
| 12232 | /* |
| 12233 | * digitally-signed struct { |
| 12234 | * opaque client_random[32]; |
| 12235 | * opaque server_random[32]; |
| 12236 | * ServerDHParams params; |
| 12237 | * }; |
| 12238 | */ |
| 12239 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 12240 | { |
| 12241 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 12242 | goto exit; |
| 12243 | } |
| 12244 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 12245 | { |
| 12246 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 12247 | goto exit; |
| 12248 | } |
| 12249 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 12250 | { |
| 12251 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 12252 | goto exit; |
| 12253 | } |
| 12254 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 12255 | { |
| 12256 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 12257 | goto exit; |
| 12258 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 12259 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12260 | { |
| 12261 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 12262 | goto exit; |
| 12263 | } |
| 12264 | |
| 12265 | exit: |
| 12266 | mbedtls_md_free( &ctx ); |
| 12267 | |
| 12268 | if( ret != 0 ) |
| 12269 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 12270 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 12271 | |
| 12272 | return( ret ); |
| 12273 | } |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 12274 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 12275 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12276 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 12277 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 12278 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12279 | #endif /* MBEDTLS_SSL_TLS_C */ |