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" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 50 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 51 | #include <string.h> |
| 52 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 53 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 54 | #include "mbedtls/psa_util.h" |
| 55 | #include "psa/crypto.h" |
| 56 | #endif |
| 57 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 58 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 59 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 60 | #endif |
| 61 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 62 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 63 | 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] | 64 | |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 65 | /* Length of the "epoch" field in the record header */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | 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] | 67 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 69 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 70 | return( 2 ); |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 71 | #else |
| 72 | ((void) ssl); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 73 | #endif |
| 74 | return( 0 ); |
| 75 | } |
| 76 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 77 | /* |
| 78 | * Start a timer. |
| 79 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 80 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | 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] | 82 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 83 | if( ssl->f_set_timer == NULL ) |
| 84 | return; |
| 85 | |
| 86 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); |
| 87 | ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Return -1 is timer is expired, 0 if it isn't. |
| 92 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 93 | static int ssl_check_timer( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 94 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 95 | if( ssl->f_get_timer == NULL ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 96 | return( 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 97 | |
| 98 | if( ssl->f_get_timer( ssl->p_timer ) == 2 ) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 99 | { |
| 100 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 101 | return( -1 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 102 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 103 | |
| 104 | return( 0 ); |
| 105 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 106 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 107 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 108 | mbedtls_ssl_transform *transform ); |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 109 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 110 | |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 111 | #if defined(MBEDTLS_SSL_RECORD_CHECKING) |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 112 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, |
| 113 | unsigned char *buf, |
| 114 | size_t len, |
| 115 | mbedtls_record *rec ); |
| 116 | |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 117 | int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl, |
| 118 | unsigned char *buf, |
| 119 | size_t buflen ) |
| 120 | { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 121 | int ret = 0; |
| 122 | mbedtls_record rec; |
| 123 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) ); |
| 124 | MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen ); |
| 125 | |
| 126 | /* We don't support record checking in TLS because |
| 127 | * (a) there doesn't seem to be a usecase for it, and |
| 128 | * (b) In SSLv3 and TLS 1.0, CBC record decryption has state |
| 129 | * and we'd need to backup the transform here. |
| 130 | */ |
| 131 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM ) |
| 132 | { |
| 133 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 134 | goto exit; |
| 135 | } |
| 136 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 137 | else |
| 138 | { |
| 139 | ret = ssl_parse_record_header( ssl, buf, buflen, &rec ); |
| 140 | if( ret != 0 ) |
| 141 | { |
| 142 | MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret ); |
| 143 | goto exit; |
| 144 | } |
| 145 | |
| 146 | if( ssl->transform_in != NULL ) |
| 147 | { |
| 148 | ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec ); |
| 149 | if( ret != 0 ) |
| 150 | { |
| 151 | MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret ); |
| 152 | goto exit; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 157 | |
| 158 | exit: |
| 159 | /* On success, we have decrypted the buffer in-place, so make |
| 160 | * sure we don't leak any plaintext data. */ |
| 161 | mbedtls_platform_zeroize( buf, buflen ); |
| 162 | |
| 163 | /* For the purpose of this API, treat messages with unexpected CID |
| 164 | * as well as such from future epochs as unexpected. */ |
| 165 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID || |
| 166 | ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 167 | { |
| 168 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 169 | } |
| 170 | |
| 171 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) ); |
| 172 | return( ret ); |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 173 | } |
| 174 | #endif /* MBEDTLS_SSL_RECORD_CHECKING */ |
| 175 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 176 | #define SSL_DONT_FORCE_FLUSH 0 |
| 177 | #define SSL_FORCE_FLUSH 1 |
| 178 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 179 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 180 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 181 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 182 | /* Top-level Connection ID API */ |
| 183 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 184 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 185 | size_t len, |
| 186 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 187 | { |
| 188 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 189 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 190 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 191 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 192 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 193 | { |
| 194 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 195 | } |
| 196 | |
| 197 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 198 | conf->cid_len = len; |
| 199 | return( 0 ); |
| 200 | } |
| 201 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 202 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 203 | int enable, |
| 204 | unsigned char const *own_cid, |
| 205 | size_t own_cid_len ) |
| 206 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 207 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 208 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 209 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 210 | ssl->negotiate_cid = enable; |
| 211 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 212 | { |
| 213 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 214 | return( 0 ); |
| 215 | } |
| 216 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 217 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 218 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 219 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 220 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 221 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 222 | (unsigned) own_cid_len, |
| 223 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 224 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 225 | } |
| 226 | |
| 227 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 228 | /* Truncation is not an issue here because |
| 229 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 230 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 231 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 232 | return( 0 ); |
| 233 | } |
| 234 | |
| 235 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 236 | int *enabled, |
| 237 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 238 | size_t *peer_cid_len ) |
| 239 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 240 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 241 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 242 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 243 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 244 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 245 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 246 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 247 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 248 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 249 | * were used, but client and server requested the empty CID. |
| 250 | * This is indistinguishable from not using the CID extension |
| 251 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 252 | if( ssl->transform_in->in_cid_len == 0 && |
| 253 | ssl->transform_in->out_cid_len == 0 ) |
| 254 | { |
| 255 | return( 0 ); |
| 256 | } |
| 257 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 258 | if( peer_cid_len != NULL ) |
| 259 | { |
| 260 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 261 | if( peer_cid != NULL ) |
| 262 | { |
| 263 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 264 | ssl->transform_in->out_cid_len ); |
| 265 | } |
| 266 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 267 | |
| 268 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 269 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 270 | return( 0 ); |
| 271 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 272 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 273 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 274 | /* Forward declarations for functions related to message buffering. */ |
| 275 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ); |
| 276 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 277 | uint8_t slot ); |
| 278 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ); |
| 279 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ); |
| 280 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ); |
| 281 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 282 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, |
| 283 | mbedtls_record const *rec ); |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 284 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 285 | |
Hanno Becker | a67dee2 | 2018-08-22 10:05:20 +0100 | [diff] [blame] | 286 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ); |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 287 | 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] | 288 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 289 | size_t mtu = ssl_get_current_mtu( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 290 | |
| 291 | if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN ) |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 292 | return( mtu ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 293 | |
| 294 | return( MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 295 | } |
| 296 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 297 | static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl ) |
| 298 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 299 | size_t const bytes_written = ssl->out_left; |
| 300 | size_t const mtu = ssl_get_maximum_datagram_size( ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 301 | |
| 302 | /* Double-check that the write-index hasn't gone |
| 303 | * past what we can transmit in a single datagram. */ |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 304 | if( bytes_written > mtu ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 305 | { |
| 306 | /* Should never happen... */ |
| 307 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 308 | } |
| 309 | |
| 310 | return( (int) ( mtu - bytes_written ) ); |
| 311 | } |
| 312 | |
| 313 | static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl ) |
| 314 | { |
| 315 | int ret; |
| 316 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 317 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 318 | |
| 319 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 320 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 321 | |
| 322 | if( max_len > mfl ) |
| 323 | max_len = mfl; |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 324 | |
| 325 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 326 | * only limits the maximum record payload size, so in theory |
| 327 | * we would be allowed to pack multiple records of payload size |
| 328 | * MFL into a single datagram. However, this would mean that there's |
| 329 | * no way to explicitly communicate MTU restrictions to the peer. |
| 330 | * |
| 331 | * The following reduction of max_len makes sure that we never |
| 332 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 333 | */ |
| 334 | if( max_len <= ssl->out_left ) |
| 335 | return( 0 ); |
| 336 | |
| 337 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 338 | #endif |
| 339 | |
| 340 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
| 341 | if( ret < 0 ) |
| 342 | return( ret ); |
| 343 | remaining = (size_t) ret; |
| 344 | |
| 345 | ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 346 | if( ret < 0 ) |
| 347 | return( ret ); |
| 348 | expansion = (size_t) ret; |
| 349 | |
| 350 | if( remaining <= expansion ) |
| 351 | return( 0 ); |
| 352 | |
| 353 | remaining -= expansion; |
| 354 | if( remaining >= max_len ) |
| 355 | remaining = max_len; |
| 356 | |
| 357 | return( (int) remaining ); |
| 358 | } |
| 359 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 360 | /* |
| 361 | * Double the retransmit timeout value, within the allowed range, |
| 362 | * returning -1 if the maximum value has already been reached. |
| 363 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 364 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 365 | { |
| 366 | uint32_t new_timeout; |
| 367 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 368 | if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 369 | return( -1 ); |
| 370 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 371 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 372 | * in the following way: after the initial transmission and a first |
| 373 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 374 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 375 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 376 | * on most non-IP stacks too. */ |
| 377 | if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min ) |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 378 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 379 | ssl->handshake->mtu = 508; |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 380 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) ); |
| 381 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 382 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 383 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 384 | |
| 385 | /* Avoid arithmetic overflow and range overflow */ |
| 386 | if( new_timeout < ssl->handshake->retransmit_timeout || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 387 | new_timeout > ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 388 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 389 | new_timeout = ssl->conf->hs_timeout_max; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | ssl->handshake->retransmit_timeout = new_timeout; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 393 | 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] | 394 | ssl->handshake->retransmit_timeout ) ); |
| 395 | |
| 396 | return( 0 ); |
| 397 | } |
| 398 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 399 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 400 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 401 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 402 | 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] | 403 | ssl->handshake->retransmit_timeout ) ); |
| 404 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 405 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 406 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 407 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 408 | /* |
| 409 | * Convert max_fragment_length codes to length. |
| 410 | * RFC 6066 says: |
| 411 | * enum{ |
| 412 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 413 | * } MaxFragmentLength; |
| 414 | * and we add 0 -> extension unused |
| 415 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 416 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 417 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 418 | switch( mfl ) |
| 419 | { |
| 420 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 421 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 422 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 423 | return 512; |
| 424 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 425 | return 1024; |
| 426 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 427 | return 2048; |
| 428 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 429 | return 4096; |
| 430 | default: |
| 431 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 432 | } |
| 433 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 434 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 435 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 436 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 437 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 438 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 439 | mbedtls_ssl_session_free( dst ); |
| 440 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 441 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 442 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 443 | |
| 444 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 445 | if( src->peer_cert != NULL ) |
| 446 | { |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 447 | int ret; |
| 448 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 449 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 450 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 451 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 452 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 453 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 454 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 455 | 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] | 456 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 457 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 458 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 459 | dst->peer_cert = NULL; |
| 460 | return( ret ); |
| 461 | } |
| 462 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 463 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 464 | if( src->peer_cert_digest != NULL ) |
| 465 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 466 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 467 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 468 | if( dst->peer_cert_digest == NULL ) |
| 469 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 470 | |
| 471 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 472 | src->peer_cert_digest_len ); |
| 473 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 474 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 475 | } |
| 476 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 477 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 478 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 479 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 480 | #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] | 481 | if( src->ticket != NULL ) |
| 482 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 483 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 484 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 485 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 486 | |
| 487 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 488 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 489 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 490 | |
| 491 | return( 0 ); |
| 492 | } |
| 493 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 494 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 495 | int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl, |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 496 | const unsigned char *key_enc, const unsigned char *key_dec, |
| 497 | size_t keylen, |
| 498 | const unsigned char *iv_enc, const unsigned char *iv_dec, |
| 499 | size_t ivlen, |
| 500 | const unsigned char *mac_enc, const unsigned char *mac_dec, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 501 | size_t maclen ) = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 502 | int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL; |
| 503 | int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL; |
| 504 | int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL; |
| 505 | int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL; |
| 506 | int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL; |
| 507 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 508 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 509 | /* |
| 510 | * Key material generation |
| 511 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 512 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 513 | static int ssl3_prf( const unsigned char *secret, size_t slen, |
| 514 | const char *label, |
| 515 | const unsigned char *random, size_t rlen, |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 516 | unsigned char *dstbuf, size_t dlen ) |
| 517 | { |
Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 518 | int ret = 0; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 519 | size_t i; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 520 | mbedtls_md5_context md5; |
| 521 | mbedtls_sha1_context sha1; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 522 | unsigned char padding[16]; |
| 523 | unsigned char sha1sum[20]; |
| 524 | ((void)label); |
| 525 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 526 | mbedtls_md5_init( &md5 ); |
| 527 | mbedtls_sha1_init( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 528 | |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 529 | /* |
| 530 | * SSLv3: |
| 531 | * block = |
| 532 | * MD5( secret + SHA1( 'A' + secret + random ) ) + |
| 533 | * MD5( secret + SHA1( 'BB' + secret + random ) ) + |
| 534 | * MD5( secret + SHA1( 'CCC' + secret + random ) ) + |
| 535 | * ... |
| 536 | */ |
| 537 | for( i = 0; i < dlen / 16; i++ ) |
| 538 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 539 | memset( padding, (unsigned char) ('A' + i), 1 + i ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 540 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 541 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 542 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 543 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 544 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 545 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 546 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 547 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 548 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 549 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 550 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 551 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 552 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 553 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 554 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 555 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 556 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 557 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 558 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 559 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 562 | exit: |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 563 | mbedtls_md5_free( &md5 ); |
| 564 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 565 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 566 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); |
| 567 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 568 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 569 | return( ret ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 570 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 571 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 572 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 573 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 574 | static int tls1_prf( const unsigned char *secret, size_t slen, |
| 575 | const char *label, |
| 576 | const unsigned char *random, size_t rlen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 577 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 578 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 579 | size_t nb, hs; |
| 580 | size_t i, j, k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 581 | const unsigned char *S1, *S2; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 582 | unsigned char *tmp; |
| 583 | size_t tmp_len = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 584 | unsigned char h_i[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 585 | const mbedtls_md_info_t *md_info; |
| 586 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 587 | int ret; |
| 588 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 589 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 590 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 591 | tmp_len = 20 + strlen( label ) + rlen; |
| 592 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 593 | if( tmp == NULL ) |
| 594 | { |
| 595 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 596 | goto exit; |
| 597 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 598 | |
| 599 | hs = ( slen + 1 ) / 2; |
| 600 | S1 = secret; |
| 601 | S2 = secret + slen - hs; |
| 602 | |
| 603 | nb = strlen( label ); |
| 604 | memcpy( tmp + 20, label, nb ); |
| 605 | memcpy( tmp + 20 + nb, random, rlen ); |
| 606 | nb += rlen; |
| 607 | |
| 608 | /* |
| 609 | * First compute P_md5(secret,label+random)[0..dlen] |
| 610 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 611 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 612 | { |
| 613 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 614 | goto exit; |
| 615 | } |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 616 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 617 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 618 | { |
| 619 | goto exit; |
| 620 | } |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 621 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 622 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); |
| 623 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 624 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 625 | |
| 626 | for( i = 0; i < dlen; i += 16 ) |
| 627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 629 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); |
| 630 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 631 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 632 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 633 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); |
| 634 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 635 | |
| 636 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; |
| 637 | |
| 638 | for( j = 0; j < k; j++ ) |
| 639 | dstbuf[i + j] = h_i[j]; |
| 640 | } |
| 641 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 642 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 643 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 644 | /* |
| 645 | * XOR out with P_sha1(secret,label+random)[0..dlen] |
| 646 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 647 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 648 | { |
| 649 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 650 | goto exit; |
| 651 | } |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 652 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 653 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 654 | { |
| 655 | goto exit; |
| 656 | } |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 657 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 658 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); |
| 659 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 660 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 661 | |
| 662 | for( i = 0; i < dlen; i += 20 ) |
| 663 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 664 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 665 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); |
| 666 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 667 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 668 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 669 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); |
| 670 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 671 | |
| 672 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; |
| 673 | |
| 674 | for( j = 0; j < k; j++ ) |
| 675 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); |
| 676 | } |
| 677 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 678 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 679 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 680 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 681 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 682 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 683 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 684 | mbedtls_free( tmp ); |
| 685 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 686 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 687 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 689 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 690 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 691 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 692 | const unsigned char *secret, size_t slen, |
| 693 | const char *label, |
| 694 | const unsigned char *random, size_t rlen, |
| 695 | unsigned char *dstbuf, size_t dlen ) |
| 696 | { |
| 697 | psa_status_t status; |
| 698 | psa_algorithm_t alg; |
Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 699 | psa_key_attributes_t key_attributes; |
Andrzej Kurek | ac5dc34 | 2019-01-23 06:57:34 -0500 | [diff] [blame] | 700 | psa_key_handle_t master_slot; |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 701 | psa_key_derivation_operation_t derivation = |
Janos Follath | 8dee877 | 2019-07-30 12:53:32 +0100 | [diff] [blame] | 702 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 703 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 704 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 705 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 706 | else |
| 707 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 708 | |
Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 709 | key_attributes = psa_key_attributes_init(); |
| 710 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 711 | psa_set_key_algorithm( &key_attributes, alg ); |
| 712 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 713 | |
Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 714 | status = psa_import_key( &key_attributes, secret, slen, &master_slot ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 715 | if( status != PSA_SUCCESS ) |
| 716 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 717 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 718 | status = psa_key_derivation( &derivation, |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 719 | master_slot, alg, |
| 720 | random, rlen, |
| 721 | (unsigned char const *) label, |
| 722 | (size_t) strlen( label ), |
| 723 | dlen ); |
| 724 | if( status != PSA_SUCCESS ) |
| 725 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 726 | psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 727 | psa_destroy_key( master_slot ); |
| 728 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 729 | } |
| 730 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 731 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 732 | if( status != PSA_SUCCESS ) |
| 733 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 734 | psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 735 | psa_destroy_key( master_slot ); |
| 736 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 737 | } |
| 738 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 739 | status = psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 740 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 741 | { |
| 742 | psa_destroy_key( master_slot ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 743 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 744 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 745 | |
| 746 | status = psa_destroy_key( master_slot ); |
| 747 | if( status != PSA_SUCCESS ) |
| 748 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 749 | |
Andrzej Kurek | 3317126 | 2019-01-15 03:25:18 -0500 | [diff] [blame] | 750 | return( 0 ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 754 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 755 | 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] | 756 | const unsigned char *secret, size_t slen, |
| 757 | const char *label, |
| 758 | const unsigned char *random, size_t rlen, |
| 759 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 760 | { |
| 761 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 762 | size_t i, j, k, md_len; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 763 | unsigned char *tmp; |
| 764 | size_t tmp_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 765 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 766 | const mbedtls_md_info_t *md_info; |
| 767 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 768 | int ret; |
| 769 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 770 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 772 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 773 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 774 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 775 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 776 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 777 | tmp_len = md_len + strlen( label ) + rlen; |
| 778 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 779 | if( tmp == NULL ) |
| 780 | { |
| 781 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 782 | goto exit; |
| 783 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 784 | |
| 785 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 786 | memcpy( tmp + md_len, label, nb ); |
| 787 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 788 | nb += rlen; |
| 789 | |
| 790 | /* |
| 791 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 792 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 793 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 794 | goto exit; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 795 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 796 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 797 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 798 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 799 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 800 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 801 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 802 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 803 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 804 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 805 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 806 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 807 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 808 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 809 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 810 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 811 | |
| 812 | for( j = 0; j < k; j++ ) |
| 813 | dstbuf[i + j] = h_i[j]; |
| 814 | } |
| 815 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 816 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 817 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 818 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 819 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 820 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 821 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 822 | mbedtls_free( tmp ); |
| 823 | |
| 824 | return( ret ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 825 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 826 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 827 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 828 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 829 | const char *label, |
| 830 | const unsigned char *random, size_t rlen, |
| 831 | unsigned char *dstbuf, size_t dlen ) |
| 832 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 833 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 834 | label, random, rlen, dstbuf, dlen ) ); |
| 835 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 836 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 837 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 838 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 839 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 840 | const char *label, |
| 841 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 842 | unsigned char *dstbuf, size_t dlen ) |
| 843 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 844 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 845 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 846 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 847 | #endif /* MBEDTLS_SHA512_C */ |
| 848 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 849 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 850 | 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] | 851 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 852 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 853 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 854 | 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] | 855 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 856 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 857 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 858 | 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] | 859 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 860 | #endif |
| 861 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 862 | #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] | 863 | 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] | 864 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 865 | #endif |
| 866 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 867 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 868 | #if defined(MBEDTLS_SHA256_C) |
| 869 | 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] | 870 | 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] | 871 | 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] | 872 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 873 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 874 | #if defined(MBEDTLS_SHA512_C) |
| 875 | 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] | 876 | 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] | 877 | 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] | 878 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 879 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 880 | |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 881 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 882 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 883 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) |
| 884 | { |
| 885 | if( ssl->conf->f_psk != NULL ) |
| 886 | { |
| 887 | /* If we've used a callback to select the PSK, |
| 888 | * the static configuration is irrelevant. */ |
| 889 | if( ssl->handshake->psk_opaque != 0 ) |
| 890 | return( 1 ); |
| 891 | |
| 892 | return( 0 ); |
| 893 | } |
| 894 | |
| 895 | if( ssl->conf->psk_opaque != 0 ) |
| 896 | return( 1 ); |
| 897 | |
| 898 | return( 0 ); |
| 899 | } |
| 900 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 901 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 902 | |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 903 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 904 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 905 | { |
| 906 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 907 | if( tls_prf == ssl3_prf ) |
| 908 | { |
Ron Eldor | 0810f0b | 2019-05-15 12:32:32 +0300 | [diff] [blame] | 909 | return( MBEDTLS_SSL_TLS_PRF_SSL3 ); |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 910 | } |
| 911 | else |
| 912 | #endif |
| 913 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 914 | if( tls_prf == tls1_prf ) |
| 915 | { |
| 916 | return( MBEDTLS_SSL_TLS_PRF_TLS1 ); |
| 917 | } |
| 918 | else |
| 919 | #endif |
| 920 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 921 | #if defined(MBEDTLS_SHA512_C) |
| 922 | if( tls_prf == tls_prf_sha384 ) |
| 923 | { |
| 924 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 925 | } |
| 926 | else |
| 927 | #endif |
| 928 | #if defined(MBEDTLS_SHA256_C) |
| 929 | if( tls_prf == tls_prf_sha256 ) |
| 930 | { |
| 931 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 932 | } |
| 933 | else |
| 934 | #endif |
| 935 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 936 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 937 | } |
| 938 | #endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
| 939 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 940 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 941 | const unsigned char *secret, size_t slen, |
| 942 | const char *label, |
| 943 | const unsigned char *random, size_t rlen, |
| 944 | unsigned char *dstbuf, size_t dlen ) |
| 945 | { |
| 946 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 947 | |
| 948 | switch( prf ) |
| 949 | { |
| 950 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 951 | case MBEDTLS_SSL_TLS_PRF_SSL3: |
| 952 | tls_prf = ssl3_prf; |
| 953 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 954 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 955 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 956 | case MBEDTLS_SSL_TLS_PRF_TLS1: |
| 957 | tls_prf = tls1_prf; |
| 958 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 959 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 960 | |
| 961 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 962 | #if defined(MBEDTLS_SHA512_C) |
| 963 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 964 | tls_prf = tls_prf_sha384; |
| 965 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 966 | #endif /* MBEDTLS_SHA512_C */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 967 | #if defined(MBEDTLS_SHA256_C) |
| 968 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 969 | tls_prf = tls_prf_sha256; |
| 970 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 971 | #endif /* MBEDTLS_SHA256_C */ |
| 972 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 973 | default: |
| 974 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 975 | } |
| 976 | |
| 977 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 978 | } |
| 979 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 980 | /* |
| 981 | * This function will ultimetaly only be responsible for populating a |
| 982 | * transform structure from data passed as explicit parameters. |
| 983 | * |
| 984 | * For now however it's doing rather more in a rather less explicit way. |
| 985 | */ |
| 986 | static int ssl_populate_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 987 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 988 | int ret = 0; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 989 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 990 | int psa_fallthrough; |
| 991 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 992 | unsigned char keyblk[256]; |
| 993 | unsigned char *key1; |
| 994 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 995 | unsigned char *mac_enc; |
| 996 | unsigned char *mac_dec; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 997 | size_t mac_key_len; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 998 | size_t iv_copy_len; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 999 | unsigned keylen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1000 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1001 | const mbedtls_cipher_info_t *cipher_info; |
| 1002 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1003 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1004 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 1005 | mbedtls_ssl_transform *transform = ssl->transform_negotiate; |
| 1006 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1007 | |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1008 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 1009 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1010 | transform->encrypt_then_mac = session->encrypt_then_mac; |
| 1011 | #endif |
| 1012 | transform->minor_ver = ssl->minor_ver; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1013 | |
| 1014 | ciphersuite_info = handshake->ciphersuite_info; |
| 1015 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1016 | if( cipher_info == NULL ) |
| 1017 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1018 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1019 | ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1020 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1021 | } |
| 1022 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1023 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1024 | if( md_info == NULL ) |
| 1025 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1026 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1027 | ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1028 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1029 | } |
| 1030 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1031 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1032 | /* Copy own and peer's CID if the use of the CID |
| 1033 | * extension has been negotiated. */ |
| 1034 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 1035 | { |
| 1036 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | 8a7f972 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 1037 | |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 1038 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 1039 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 1c1f046 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 1040 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1041 | transform->in_cid_len ); |
Hanno Becker | d1f2035 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 1042 | |
| 1043 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 1044 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 1045 | ssl->handshake->peer_cid_len ); |
| 1046 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 1047 | transform->out_cid_len ); |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1048 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1049 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1050 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1051 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1052 | * SSLv3: |
| 1053 | * key block = |
| 1054 | * MD5( master + SHA1( 'A' + master + randbytes ) ) + |
| 1055 | * MD5( master + SHA1( 'BB' + master + randbytes ) ) + |
| 1056 | * MD5( master + SHA1( 'CCC' + master + randbytes ) ) + |
| 1057 | * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) + |
| 1058 | * ... |
| 1059 | * |
| 1060 | * TLSv1: |
| 1061 | * key block = PRF( master, "key expansion", randbytes ) |
| 1062 | */ |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1063 | ret = handshake->tls_prf( session->master, 48, "key expansion", |
| 1064 | handshake->randbytes, 64, keyblk, 256 ); |
| 1065 | if( ret != 0 ) |
| 1066 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1067 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1068 | return( ret ); |
| 1069 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1070 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1071 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 1072 | mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) ); |
| 1073 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 ); |
| 1074 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 ); |
| 1075 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1076 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1077 | /* |
| 1078 | * Determine the appropriate key, IV and MAC length. |
| 1079 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1080 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1081 | keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1082 | |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1083 | #if defined(MBEDTLS_GCM_C) || \ |
| 1084 | defined(MBEDTLS_CCM_C) || \ |
| 1085 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1086 | if( cipher_info->mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1087 | cipher_info->mode == MBEDTLS_MODE_CCM || |
| 1088 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1089 | { |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1090 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1091 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1092 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1093 | mac_key_len = 0; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1094 | transform->taglen = |
| 1095 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1096 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1097 | /* All modes haves 96-bit IVs; |
| 1098 | * GCM and CCM has 4 implicit and 8 explicit bytes |
| 1099 | * ChachaPoly has all 12 bytes implicit |
| 1100 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1101 | transform->ivlen = 12; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1102 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1103 | transform->fixed_ivlen = 12; |
| 1104 | else |
| 1105 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1106 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1107 | /* Minimum length of encrypted record */ |
| 1108 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1109 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1110 | } |
| 1111 | else |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1112 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 1113 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 1114 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 1115 | cipher_info->mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1116 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1117 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1118 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 1119 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1120 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1121 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1122 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1123 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1124 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1125 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1126 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 1127 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1128 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1129 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1130 | /* |
| 1131 | * If HMAC is to be truncated, we shall keep the leftmost bytes, |
| 1132 | * (rfc 6066 page 13 or rfc 2104 section 4), |
| 1133 | * so we only need to adjust the length here. |
| 1134 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1135 | if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1136 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1137 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1138 | |
| 1139 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) |
| 1140 | /* Fall back to old, non-compliant version of the truncated |
Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 1141 | * HMAC implementation which also truncates the key |
| 1142 | * (Mbed TLS versions from 1.3 to 2.6.0) */ |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1143 | mac_key_len = transform->maclen; |
| 1144 | #endif |
| 1145 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1146 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1147 | |
| 1148 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1149 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1150 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1151 | /* Minimum length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1152 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1153 | transform->minlen = transform->maclen; |
| 1154 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1155 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1156 | /* |
| 1157 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1158 | * 1. if EtM is in use: one block plus MAC |
| 1159 | * otherwise: * first multiple of blocklen greater than maclen |
| 1160 | * 2. IV except for SSL3 and TLS 1.0 |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1161 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1162 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1163 | if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1164 | { |
| 1165 | transform->minlen = transform->maclen |
| 1166 | + cipher_info->block_size; |
| 1167 | } |
| 1168 | else |
| 1169 | #endif |
| 1170 | { |
| 1171 | transform->minlen = transform->maclen |
| 1172 | + cipher_info->block_size |
| 1173 | - transform->maclen % cipher_info->block_size; |
| 1174 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1176 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
| 1177 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 1178 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1179 | ; /* No need to adjust minlen */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1180 | else |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1181 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1182 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1183 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || |
| 1184 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1185 | { |
| 1186 | transform->minlen += transform->ivlen; |
| 1187 | } |
| 1188 | else |
| 1189 | #endif |
| 1190 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1191 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1192 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1193 | goto end; |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1194 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1195 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1196 | } |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1197 | else |
| 1198 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1199 | { |
| 1200 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1201 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1202 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1203 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1204 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 1205 | (unsigned) keylen, |
| 1206 | (unsigned) transform->minlen, |
| 1207 | (unsigned) transform->ivlen, |
| 1208 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1209 | |
| 1210 | /* |
| 1211 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 1212 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1213 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1214 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1215 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1216 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1217 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1218 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1219 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1220 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1221 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1222 | /* |
| 1223 | * This is not used in TLS v1.1. |
| 1224 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1225 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1226 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1227 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 1228 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1229 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1230 | } |
| 1231 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1232 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1233 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1234 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1235 | { |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1236 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1237 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1238 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1239 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1240 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1241 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1242 | /* |
| 1243 | * This is not used in TLS v1.1. |
| 1244 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1245 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1246 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1247 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 1248 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1249 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1250 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1251 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1252 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1253 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1254 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1255 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1256 | goto end; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1257 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1258 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1259 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1260 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1261 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1262 | { |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1263 | if( mac_key_len > sizeof( transform->mac_enc ) ) |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1264 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1265 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1266 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1267 | goto end; |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1268 | } |
| 1269 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1270 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); |
| 1271 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1272 | } |
| 1273 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1274 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1275 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1276 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1277 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1278 | { |
Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1279 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 1280 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 1281 | if( mac_key_len != 0 ) |
| 1282 | { |
| 1283 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 1284 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 1285 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1286 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1287 | else |
| 1288 | #endif |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1289 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1290 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1291 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1292 | goto end; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1293 | } |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1294 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1295 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1296 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 1297 | if( mbedtls_ssl_hw_record_init != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1298 | { |
| 1299 | int ret = 0; |
| 1300 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1301 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1302 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1303 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1304 | transform->iv_enc, transform->iv_dec, |
| 1305 | iv_copy_len, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1306 | mac_enc, mac_dec, |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1307 | mac_key_len ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1308 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1309 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1310 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1311 | goto end; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1312 | } |
| 1313 | } |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1314 | #else |
| 1315 | ((void) mac_dec); |
| 1316 | ((void) mac_enc); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1317 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1318 | |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1319 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 1320 | if( ssl->conf->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1321 | { |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1322 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, |
| 1323 | session->master, keyblk, |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1324 | mac_key_len, keylen, |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1325 | iv_copy_len ); |
| 1326 | } |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1327 | |
| 1328 | if( ssl->conf->f_export_keys_ext != NULL ) |
| 1329 | { |
| 1330 | ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys, |
| 1331 | session->master, keyblk, |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1332 | mac_key_len, keylen, |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1333 | iv_copy_len, |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1334 | handshake->randbytes + 32, |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1335 | handshake->randbytes, |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 1336 | tls_prf_get_type( handshake->tls_prf ) ); |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1337 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1338 | #endif |
| 1339 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1340 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1341 | |
| 1342 | /* Only use PSA-based ciphers for TLS-1.2. |
| 1343 | * That's relevant at least for TLS-1.0, where |
| 1344 | * we assume that mbedtls_cipher_crypt() updates |
| 1345 | * the structure field for the IV, which the PSA-based |
| 1346 | * implementation currently doesn't. */ |
| 1347 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1348 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1349 | { |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1350 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc, |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1351 | cipher_info, transform->taglen ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1352 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 1353 | { |
| 1354 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1355 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | if( ret == 0 ) |
| 1359 | { |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1360 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1361 | psa_fallthrough = 0; |
| 1362 | } |
| 1363 | else |
| 1364 | { |
| 1365 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) ); |
| 1366 | psa_fallthrough = 1; |
| 1367 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1368 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1369 | else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1370 | psa_fallthrough = 1; |
| 1371 | #else |
| 1372 | psa_fallthrough = 1; |
| 1373 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1374 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1375 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1376 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1377 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1378 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1379 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1380 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1381 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1382 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1383 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1384 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1385 | /* Only use PSA-based ciphers for TLS-1.2. |
| 1386 | * That's relevant at least for TLS-1.0, where |
| 1387 | * we assume that mbedtls_cipher_crypt() updates |
| 1388 | * the structure field for the IV, which the PSA-based |
| 1389 | * implementation currently doesn't. */ |
| 1390 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1391 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1392 | { |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1393 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec, |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1394 | cipher_info, transform->taglen ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1395 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 1396 | { |
| 1397 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1398 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | if( ret == 0 ) |
| 1402 | { |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1403 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1404 | psa_fallthrough = 0; |
| 1405 | } |
| 1406 | else |
| 1407 | { |
| 1408 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) ); |
| 1409 | psa_fallthrough = 1; |
| 1410 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1411 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1412 | else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1413 | psa_fallthrough = 1; |
| 1414 | #else |
| 1415 | psa_fallthrough = 1; |
| 1416 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1417 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1418 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1419 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1420 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1421 | cipher_info ) ) != 0 ) |
| 1422 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1423 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1424 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1425 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1426 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1427 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1428 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1429 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1430 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1431 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1432 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1433 | } |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1434 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1435 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1436 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1437 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1438 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1439 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1440 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1441 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1442 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1443 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1444 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1445 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1446 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1447 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1448 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1449 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1450 | goto end; |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1451 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1452 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1453 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1454 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1455 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1456 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1457 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1458 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1459 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1460 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1461 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1462 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1463 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1464 | // Initialize compression |
| 1465 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1466 | if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1467 | { |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1468 | if( ssl->compress_buf == NULL ) |
| 1469 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1470 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1471 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1472 | if( ssl->compress_buf == NULL ) |
| 1473 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 1474 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1475 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1476 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1477 | goto end; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1478 | } |
| 1479 | } |
| 1480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1481 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1482 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1483 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); |
| 1484 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1485 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1486 | if( deflateInit( &transform->ctx_deflate, |
| 1487 | Z_DEFAULT_COMPRESSION ) != Z_OK || |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1488 | inflateInit( &transform->ctx_inflate ) != Z_OK ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1489 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1490 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1491 | ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED; |
| 1492 | goto end; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1493 | } |
| 1494 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1495 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1496 | |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1497 | end: |
Ron Eldor | a9f9a73 | 2019-05-07 18:29:02 +0300 | [diff] [blame] | 1498 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1499 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1502 | /* |
| 1503 | * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions |
| 1504 | * |
| 1505 | * Inputs: |
| 1506 | * - SSL/TLS minor version |
| 1507 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1508 | * |
| 1509 | * Ouputs: |
| 1510 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1511 | */ |
| 1512 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1513 | int minor_ver, |
| 1514 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1515 | { |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1516 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) |
| 1517 | (void) hash; |
| 1518 | #endif |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1519 | |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1520 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1521 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1522 | { |
| 1523 | handshake->tls_prf = ssl3_prf; |
| 1524 | handshake->calc_verify = ssl_calc_verify_ssl; |
| 1525 | handshake->calc_finished = ssl_calc_finished_ssl; |
| 1526 | } |
| 1527 | else |
| 1528 | #endif |
| 1529 | #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] | 1530 | if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1531 | { |
| 1532 | handshake->tls_prf = tls1_prf; |
| 1533 | handshake->calc_verify = ssl_calc_verify_tls; |
| 1534 | handshake->calc_finished = ssl_calc_finished_tls; |
| 1535 | } |
| 1536 | else |
| 1537 | #endif |
| 1538 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1539 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1540 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1541 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1542 | { |
| 1543 | handshake->tls_prf = tls_prf_sha384; |
| 1544 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1545 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1546 | } |
| 1547 | else |
| 1548 | #endif |
| 1549 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1550 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1551 | { |
| 1552 | handshake->tls_prf = tls_prf_sha256; |
| 1553 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1554 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1555 | } |
| 1556 | else |
| 1557 | #endif |
| 1558 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1559 | { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1560 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1561 | } |
| 1562 | |
| 1563 | return( 0 ); |
| 1564 | } |
| 1565 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1566 | /* |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1567 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1568 | * |
| 1569 | * Parameters: |
| 1570 | * [in/out] handshake |
| 1571 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1572 | * (PSA-PSK) ciphersuite_info, psk_opaque |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1573 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1574 | * [out] master |
| 1575 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 1576 | * debug: conf->f_dbg, conf->p_dbg |
| 1577 | * EMS: passed to calc_verify (debug + (SSL3) session_negotiate) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1578 | * PSA-PSA: minor_ver, conf |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1579 | */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1580 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1581 | unsigned char *master, |
Manuel Pégourié-Gonnard | 0d56aaa | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1582 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1583 | { |
| 1584 | int ret; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1585 | |
| 1586 | /* cf. RFC 5246, Section 8.1: |
| 1587 | * "The master secret is always exactly 48 bytes in length." */ |
| 1588 | size_t const master_secret_len = 48; |
| 1589 | |
| 1590 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1591 | unsigned char session_hash[48]; |
| 1592 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 1593 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1594 | /* The label for the KDF used for key expansion. |
| 1595 | * This is either "master secret" or "extended master secret" |
| 1596 | * depending on whether the Extended Master Secret extension |
| 1597 | * is used. */ |
| 1598 | char const *lbl = "master secret"; |
| 1599 | |
| 1600 | /* The salt for the KDF used for key expansion. |
| 1601 | * - If the Extended Master Secret extension is not used, |
| 1602 | * this is ClientHello.Random + ServerHello.Random |
| 1603 | * (see Sect. 8.1 in RFC 5246). |
| 1604 | * - If the Extended Master Secret extension is used, |
| 1605 | * this is the transcript of the handshake so far. |
| 1606 | * (see Sect. 4 in RFC 7627). */ |
| 1607 | unsigned char const *salt = handshake->randbytes; |
| 1608 | size_t salt_len = 64; |
| 1609 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1610 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 1611 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 1612 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1613 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 1614 | (void) ssl; |
| 1615 | #endif |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1616 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1617 | if( handshake->resume != 0 ) |
| 1618 | { |
| 1619 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1620 | return( 0 ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1621 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1622 | |
| 1623 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1624 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1625 | { |
| 1626 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1627 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1628 | lbl = "extended master secret"; |
| 1629 | salt = session_hash; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1630 | handshake->calc_verify( ssl, session_hash, &salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1631 | |
| 1632 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len ); |
| 1633 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1634 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
| 1635 | |
| 1636 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1637 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1638 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1639 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1640 | ssl_use_opaque_psk( ssl ) == 1 ) |
| 1641 | { |
| 1642 | /* Perform PSK-to-MS expansion in a single step. */ |
| 1643 | psa_status_t status; |
| 1644 | psa_algorithm_t alg; |
| 1645 | psa_key_handle_t psk; |
| 1646 | psa_key_derivation_operation_t derivation = |
| 1647 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1648 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1649 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1650 | 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] | 1651 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1652 | psk = ssl->conf->psk_opaque; |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1653 | if( handshake->psk_opaque != 0 ) |
| 1654 | psk = handshake->psk_opaque; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1655 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1656 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1657 | 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] | 1658 | else |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1659 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1660 | |
| 1661 | status = psa_key_derivation( &derivation, psk, alg, |
| 1662 | salt, salt_len, |
| 1663 | (unsigned char const *) lbl, |
| 1664 | (size_t) strlen( lbl ), |
| 1665 | master_secret_len ); |
| 1666 | if( status != PSA_SUCCESS ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1667 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1668 | psa_key_derivation_abort( &derivation ); |
| 1669 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1670 | } |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1671 | |
| 1672 | status = psa_key_derivation_output_bytes( &derivation, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1673 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1674 | master_secret_len ); |
| 1675 | if( status != PSA_SUCCESS ) |
| 1676 | { |
| 1677 | psa_key_derivation_abort( &derivation ); |
| 1678 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1679 | } |
| 1680 | |
| 1681 | status = psa_key_derivation_abort( &derivation ); |
| 1682 | if( status != PSA_SUCCESS ) |
| 1683 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1684 | } |
| 1685 | else |
| 1686 | #endif |
| 1687 | { |
| 1688 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1689 | lbl, salt, salt_len, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1690 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1691 | master_secret_len ); |
| 1692 | if( ret != 0 ) |
| 1693 | { |
| 1694 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1695 | return( ret ); |
| 1696 | } |
| 1697 | |
| 1698 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 1699 | handshake->premaster, |
| 1700 | handshake->pmslen ); |
| 1701 | |
| 1702 | mbedtls_platform_zeroize( handshake->premaster, |
| 1703 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | return( 0 ); |
| 1707 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1708 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1709 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1710 | { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1711 | int ret; |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1712 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 1713 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1714 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame^] | 1715 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1716 | |
| 1717 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1718 | ret = ssl_set_handshake_prfs( ssl->handshake, |
| 1719 | ssl->minor_ver, |
| 1720 | ciphersuite_info->mac ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1721 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1722 | { |
| 1723 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1724 | return( ret ); |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1725 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1726 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame^] | 1727 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1728 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1729 | ssl->session_negotiate->master, |
| 1730 | ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1731 | if( ret != 0 ) |
| 1732 | { |
| 1733 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1734 | return( ret ); |
| 1735 | } |
| 1736 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame^] | 1737 | /* Swap the client and server random values: |
| 1738 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1739 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1740 | { |
| 1741 | unsigned char tmp[64]; |
| 1742 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1743 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1744 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1745 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1746 | } |
| 1747 | |
| 1748 | /* Populate transform structure */ |
| 1749 | ret = ssl_populate_transform( ssl ); |
| 1750 | if( ret != 0 ) |
| 1751 | { |
| 1752 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret ); |
| 1753 | return( ret ); |
| 1754 | } |
| 1755 | |
| 1756 | /* We no longer need Server/ClientHello.random values */ |
| 1757 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1758 | sizeof( ssl->handshake->randbytes ) ); |
| 1759 | |
| 1760 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1761 | |
| 1762 | return( 0 ); |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1763 | } |
| 1764 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1765 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1766 | void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl, |
| 1767 | unsigned char hash[36], |
| 1768 | size_t *hlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1769 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1770 | mbedtls_md5_context md5; |
| 1771 | mbedtls_sha1_context sha1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1772 | unsigned char pad_1[48]; |
| 1773 | unsigned char pad_2[48]; |
| 1774 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1775 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1776 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1777 | mbedtls_md5_init( &md5 ); |
| 1778 | mbedtls_sha1_init( &sha1 ); |
| 1779 | |
| 1780 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1781 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1782 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1783 | memset( pad_1, 0x36, 48 ); |
| 1784 | memset( pad_2, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1785 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1786 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1787 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); |
| 1788 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1789 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1790 | mbedtls_md5_starts_ret( &md5 ); |
| 1791 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1792 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); |
| 1793 | mbedtls_md5_update_ret( &md5, hash, 16 ); |
| 1794 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1795 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1796 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1797 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); |
| 1798 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1799 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1800 | mbedtls_sha1_starts_ret( &sha1 ); |
| 1801 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1802 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); |
| 1803 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); |
| 1804 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1805 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1806 | *hlen = 36; |
| 1807 | |
| 1808 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1809 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1810 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1811 | mbedtls_md5_free( &md5 ); |
| 1812 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1813 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1814 | return; |
| 1815 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1816 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1817 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1818 | #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] | 1819 | void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl, |
| 1820 | unsigned char hash[36], |
| 1821 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1822 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1823 | mbedtls_md5_context md5; |
| 1824 | mbedtls_sha1_context sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1825 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1826 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1827 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1828 | mbedtls_md5_init( &md5 ); |
| 1829 | mbedtls_sha1_init( &sha1 ); |
| 1830 | |
| 1831 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1832 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1833 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1834 | mbedtls_md5_finish_ret( &md5, hash ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1835 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1836 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1837 | *hlen = 36; |
| 1838 | |
| 1839 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1840 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1841 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1842 | mbedtls_md5_free( &md5 ); |
| 1843 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1844 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1845 | return; |
| 1846 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1847 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1848 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1849 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1850 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1851 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 1852 | unsigned char hash[32], |
| 1853 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1854 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1855 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1856 | size_t hash_size; |
| 1857 | psa_status_t status; |
| 1858 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 1859 | |
| 1860 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 1861 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 1862 | if( status != PSA_SUCCESS ) |
| 1863 | { |
| 1864 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1865 | return; |
| 1866 | } |
| 1867 | |
| 1868 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 1869 | if( status != PSA_SUCCESS ) |
| 1870 | { |
| 1871 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1872 | return; |
| 1873 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1874 | |
| 1875 | *hlen = 32; |
| 1876 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1877 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1878 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1879 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1880 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1881 | mbedtls_sha256_init( &sha256 ); |
| 1882 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1883 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1884 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1885 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1886 | mbedtls_sha256_finish_ret( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1887 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1888 | *hlen = 32; |
| 1889 | |
| 1890 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1891 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1892 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1893 | mbedtls_sha256_free( &sha256 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1894 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1895 | return; |
| 1896 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1897 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1898 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1899 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1900 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 1901 | unsigned char hash[48], |
| 1902 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1903 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1904 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1905 | size_t hash_size; |
| 1906 | psa_status_t status; |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1907 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1908 | |
| 1909 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1910 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1911 | if( status != PSA_SUCCESS ) |
| 1912 | { |
| 1913 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1914 | return; |
| 1915 | } |
| 1916 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1917 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1918 | if( status != PSA_SUCCESS ) |
| 1919 | { |
| 1920 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1921 | return; |
| 1922 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1923 | |
| 1924 | *hlen = 48; |
| 1925 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1926 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1927 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1928 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1929 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1930 | mbedtls_sha512_init( &sha512 ); |
| 1931 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1932 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1933 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1934 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1935 | mbedtls_sha512_finish_ret( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1936 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1937 | *hlen = 48; |
| 1938 | |
| 1939 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1940 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1941 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1942 | mbedtls_sha512_free( &sha512 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1943 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1944 | return; |
| 1945 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1946 | #endif /* MBEDTLS_SHA512_C */ |
| 1947 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1948 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1949 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 1950 | 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] | 1951 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1952 | unsigned char *p = ssl->handshake->premaster; |
| 1953 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1954 | const unsigned char *psk = ssl->conf->psk; |
| 1955 | size_t psk_len = ssl->conf->psk_len; |
| 1956 | |
| 1957 | /* If the psk callback was called, use its result */ |
| 1958 | if( ssl->handshake->psk != NULL ) |
| 1959 | { |
| 1960 | psk = ssl->handshake->psk; |
| 1961 | psk_len = ssl->handshake->psk_len; |
| 1962 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1963 | |
| 1964 | /* |
| 1965 | * PMS = struct { |
| 1966 | * opaque other_secret<0..2^16-1>; |
| 1967 | * opaque psk<0..2^16-1>; |
| 1968 | * }; |
| 1969 | * with "other_secret" depending on the particular key exchange |
| 1970 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1971 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1972 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1973 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1974 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1975 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1976 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1977 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1978 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1979 | |
| 1980 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1981 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1982 | |
| 1983 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1984 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1985 | } |
| 1986 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1987 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1988 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1989 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1990 | { |
| 1991 | /* |
| 1992 | * other_secret already set by the ClientKeyExchange message, |
| 1993 | * and is 48 bytes long |
| 1994 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1995 | if( end - p < 2 ) |
| 1996 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1997 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1998 | *p++ = 0; |
| 1999 | *p++ = 48; |
| 2000 | p += 48; |
| 2001 | } |
| 2002 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2003 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 2004 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 2005 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2006 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 2007 | int ret; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 2008 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2009 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 2010 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2011 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 2012 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2013 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2014 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2015 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2016 | return( ret ); |
| 2017 | } |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 2018 | *(p++) = (unsigned char)( len >> 8 ); |
| 2019 | *(p++) = (unsigned char)( len ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2020 | p += len; |
| 2021 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2022 | 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] | 2023 | } |
| 2024 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2025 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 2026 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 2027 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2028 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 2029 | int ret; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2030 | size_t zlen; |
| 2031 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2032 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2033 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2034 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2035 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2036 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2037 | return( ret ); |
| 2038 | } |
| 2039 | |
| 2040 | *(p++) = (unsigned char)( zlen >> 8 ); |
| 2041 | *(p++) = (unsigned char)( zlen ); |
| 2042 | p += zlen; |
| 2043 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2044 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 2045 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2046 | } |
| 2047 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2048 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2049 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2050 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2051 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2055 | if( end - p < 2 ) |
| 2056 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 2057 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2058 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 2059 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2060 | |
| 2061 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 2062 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2063 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2064 | memcpy( p, psk, psk_len ); |
| 2065 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2066 | |
| 2067 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 2068 | |
| 2069 | return( 0 ); |
| 2070 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2071 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2072 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2073 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2074 | /* |
| 2075 | * SSLv3.0 MAC functions |
| 2076 | */ |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2077 | #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] | 2078 | static void ssl_mac( mbedtls_md_context_t *md_ctx, |
| 2079 | const unsigned char *secret, |
| 2080 | const unsigned char *buf, size_t len, |
| 2081 | const unsigned char *ctr, int type, |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2082 | unsigned char out[SSL_MAC_MAX_BYTES] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2083 | { |
| 2084 | unsigned char header[11]; |
| 2085 | unsigned char padding[48]; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 2086 | int padlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2087 | int md_size = mbedtls_md_get_size( md_ctx->md_info ); |
| 2088 | int md_type = mbedtls_md_get_type( md_ctx->md_info ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2089 | |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 2090 | /* Only MD5 and SHA-1 supported */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2091 | if( md_type == MBEDTLS_MD_MD5 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2092 | padlen = 48; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 2093 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2094 | padlen = 40; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2095 | |
| 2096 | memcpy( header, ctr, 8 ); |
| 2097 | header[ 8] = (unsigned char) type; |
| 2098 | header[ 9] = (unsigned char)( len >> 8 ); |
| 2099 | header[10] = (unsigned char)( len ); |
| 2100 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2101 | memset( padding, 0x36, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2102 | mbedtls_md_starts( md_ctx ); |
| 2103 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 2104 | mbedtls_md_update( md_ctx, padding, padlen ); |
| 2105 | mbedtls_md_update( md_ctx, header, 11 ); |
| 2106 | mbedtls_md_update( md_ctx, buf, len ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 2107 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2108 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2109 | memset( padding, 0x5C, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2110 | mbedtls_md_starts( md_ctx ); |
| 2111 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 2112 | mbedtls_md_update( md_ctx, padding, padlen ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 2113 | mbedtls_md_update( md_ctx, out, md_size ); |
| 2114 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 2115 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2116 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 2117 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2118 | /* 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] | 2119 | * 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] | 2120 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \ |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2121 | ( defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2122 | defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2123 | defined(MBEDTLS_SSL_PROTO_TLS1_2) ) |
| 2124 | /* This function makes sure every byte in the memory region is accessed |
| 2125 | * (in ascending addresses order) */ |
| 2126 | static void ssl_read_memory( unsigned char *p, size_t len ) |
| 2127 | { |
| 2128 | unsigned char acc = 0; |
| 2129 | volatile unsigned char force; |
| 2130 | |
| 2131 | for( ; len != 0; p++, len-- ) |
| 2132 | acc ^= *p; |
| 2133 | |
| 2134 | force = acc; |
| 2135 | (void) force; |
| 2136 | } |
| 2137 | #endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */ |
| 2138 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2139 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2140 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2141 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2142 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2143 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d3f8c79 | 2019-05-20 15:06:12 +0100 | [diff] [blame] | 2144 | /* This functions transforms a DTLS plaintext fragment and a record content |
| 2145 | * type into an instance of the DTLSInnerPlaintext structure: |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2146 | * |
| 2147 | * struct { |
| 2148 | * opaque content[DTLSPlaintext.length]; |
| 2149 | * ContentType real_type; |
| 2150 | * uint8 zeros[length_of_padding]; |
| 2151 | * } DTLSInnerPlaintext; |
| 2152 | * |
| 2153 | * Input: |
| 2154 | * - `content`: The beginning of the buffer holding the |
| 2155 | * plaintext to be wrapped. |
| 2156 | * - `*content_size`: The length of the plaintext in Bytes. |
| 2157 | * - `max_len`: The number of Bytes available starting from |
| 2158 | * `content`. This must be `>= *content_size`. |
| 2159 | * - `rec_type`: The desired record content type. |
| 2160 | * |
| 2161 | * Output: |
| 2162 | * - `content`: The beginning of the resulting DTLSInnerPlaintext structure. |
| 2163 | * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure. |
| 2164 | * |
| 2165 | * Returns: |
| 2166 | * - `0` on success. |
| 2167 | * - A negative error code if `max_len` didn't offer enough space |
| 2168 | * for the expansion. |
| 2169 | */ |
| 2170 | static int ssl_cid_build_inner_plaintext( unsigned char *content, |
| 2171 | size_t *content_size, |
| 2172 | size_t remaining, |
| 2173 | uint8_t rec_type ) |
| 2174 | { |
| 2175 | size_t len = *content_size; |
Hanno Becker | b9ec44f | 2019-05-13 15:31:17 +0100 | [diff] [blame] | 2176 | size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY - |
| 2177 | ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) % |
| 2178 | MBEDTLS_SSL_CID_PADDING_GRANULARITY; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2179 | |
| 2180 | /* Write real content type */ |
| 2181 | if( remaining == 0 ) |
| 2182 | return( -1 ); |
| 2183 | content[ len ] = rec_type; |
| 2184 | len++; |
| 2185 | remaining--; |
| 2186 | |
| 2187 | if( remaining < pad ) |
| 2188 | return( -1 ); |
| 2189 | memset( content + len, 0, pad ); |
| 2190 | len += pad; |
| 2191 | remaining -= pad; |
| 2192 | |
| 2193 | *content_size = len; |
| 2194 | return( 0 ); |
| 2195 | } |
| 2196 | |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2197 | /* This function parses a DTLSInnerPlaintext structure. |
| 2198 | * See ssl_cid_build_inner_plaintext() for details. */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2199 | static int ssl_cid_parse_inner_plaintext( unsigned char const *content, |
| 2200 | size_t *content_size, |
| 2201 | uint8_t *rec_type ) |
| 2202 | { |
| 2203 | size_t remaining = *content_size; |
| 2204 | |
| 2205 | /* Determine length of padding by skipping zeroes from the back. */ |
| 2206 | do |
| 2207 | { |
| 2208 | if( remaining == 0 ) |
| 2209 | return( -1 ); |
| 2210 | remaining--; |
| 2211 | } while( content[ remaining ] == 0 ); |
| 2212 | |
| 2213 | *content_size = remaining; |
| 2214 | *rec_type = content[ remaining ]; |
| 2215 | |
| 2216 | return( 0 ); |
| 2217 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2218 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2219 | |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2220 | /* `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] | 2221 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2222 | static void ssl_extract_add_data_from_record( unsigned char* add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2223 | size_t *add_data_len, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2224 | mbedtls_record *rec ) |
| 2225 | { |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2226 | /* Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2227 | * |
| 2228 | * additional_data = seq_num + TLSCompressed.type + |
| 2229 | * TLSCompressed.version + TLSCompressed.length; |
| 2230 | * |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2231 | * For the CID extension, this is extended as follows |
| 2232 | * (quoting draft-ietf-tls-dtls-connection-id-05, |
| 2233 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2234 | * |
| 2235 | * additional_data = seq_num + DTLSPlaintext.type + |
| 2236 | * DTLSPlaintext.version + |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2237 | * cid + |
| 2238 | * cid_length + |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2239 | * length_of_DTLSInnerPlaintext; |
| 2240 | */ |
| 2241 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2242 | memcpy( add_data, rec->ctr, sizeof( rec->ctr ) ); |
| 2243 | add_data[8] = rec->type; |
Hanno Becker | edb24f8 | 2019-05-20 15:01:46 +0100 | [diff] [blame] | 2244 | memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) ); |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2245 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2246 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2247 | if( rec->cid_len != 0 ) |
| 2248 | { |
| 2249 | memcpy( add_data + 11, rec->cid, rec->cid_len ); |
| 2250 | add_data[11 + rec->cid_len + 0] = rec->cid_len; |
| 2251 | add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF; |
| 2252 | add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF; |
| 2253 | *add_data_len = 13 + 1 + rec->cid_len; |
| 2254 | } |
| 2255 | else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2256 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2257 | { |
| 2258 | add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF; |
| 2259 | add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF; |
| 2260 | *add_data_len = 13; |
| 2261 | } |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2264 | int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl, |
| 2265 | mbedtls_ssl_transform *transform, |
| 2266 | mbedtls_record *rec, |
| 2267 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2268 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2269 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2270 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2271 | int auth_done = 0; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2272 | unsigned char * data; |
Hanno Becker | 92fb4fa | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2273 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2274 | size_t add_data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2275 | size_t post_avail; |
| 2276 | |
| 2277 | /* The SSL context is only used for debugging purposes! */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2278 | #if !defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2279 | ((void) ssl); |
| 2280 | #endif |
| 2281 | |
| 2282 | /* The PRNG is used for dynamic IV generation that's used |
| 2283 | * for CBC transformations in TLS 1.1 and TLS 1.2. */ |
| 2284 | #if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
| 2285 | ( defined(MBEDTLS_AES_C) || \ |
| 2286 | defined(MBEDTLS_ARIA_C) || \ |
| 2287 | defined(MBEDTLS_CAMELLIA_C) ) && \ |
| 2288 | ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) ) |
| 2289 | ((void) f_rng); |
| 2290 | ((void) p_rng); |
| 2291 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2292 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2293 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2294 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2295 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2296 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2297 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) ); |
| 2298 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2299 | } |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2300 | if( rec == NULL |
| 2301 | || rec->buf == NULL |
| 2302 | || rec->buf_len < rec->data_offset |
| 2303 | || rec->buf_len - rec->data_offset < rec->data_len |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2304 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2305 | || rec->cid_len != 0 |
| 2306 | #endif |
| 2307 | ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2308 | { |
| 2309 | 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] | 2310 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2311 | } |
| 2312 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2313 | data = rec->buf + rec->data_offset; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2314 | 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] | 2315 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2316 | data, rec->data_len ); |
| 2317 | |
| 2318 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ); |
| 2319 | |
| 2320 | if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 2321 | { |
| 2322 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d", |
| 2323 | (unsigned) rec->data_len, |
| 2324 | MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 2325 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2326 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 2327 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2328 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2329 | /* |
| 2330 | * Add CID information |
| 2331 | */ |
| 2332 | rec->cid_len = transform->out_cid_len; |
| 2333 | memcpy( rec->cid, transform->out_cid, transform->out_cid_len ); |
| 2334 | MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len ); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2335 | |
| 2336 | if( rec->cid_len != 0 ) |
| 2337 | { |
| 2338 | /* |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2339 | * Wrap plaintext into DTLSInnerPlaintext structure. |
| 2340 | * See ssl_cid_build_inner_plaintext() for more information. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2341 | * |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2342 | * Note that this changes `rec->data_len`, and hence |
| 2343 | * `post_avail` needs to be recalculated afterwards. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2344 | */ |
| 2345 | if( ssl_cid_build_inner_plaintext( data, |
| 2346 | &rec->data_len, |
| 2347 | post_avail, |
| 2348 | rec->type ) != 0 ) |
| 2349 | { |
| 2350 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2351 | } |
| 2352 | |
| 2353 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 2354 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2355 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2356 | |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2357 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); |
| 2358 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2359 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2360 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2361 | */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2362 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2363 | if( mode == MBEDTLS_MODE_STREAM || |
| 2364 | ( mode == MBEDTLS_MODE_CBC |
| 2365 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2366 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2367 | #endif |
| 2368 | ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2369 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2370 | if( post_avail < transform->maclen ) |
| 2371 | { |
| 2372 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2373 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2374 | } |
| 2375 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2376 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2377 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2378 | { |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2379 | unsigned char mac[SSL_MAC_MAX_BYTES]; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2380 | ssl_mac( &transform->md_ctx_enc, transform->mac_enc, |
| 2381 | data, rec->data_len, rec->ctr, rec->type, mac ); |
| 2382 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2383 | } |
| 2384 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2385 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2386 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2387 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2388 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2389 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2390 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2391 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2392 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2393 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2394 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2395 | add_data_len ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2396 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2397 | data, rec->data_len ); |
| 2398 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2399 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
| 2400 | |
| 2401 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2402 | } |
| 2403 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2404 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2405 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2406 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2407 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2408 | } |
| 2409 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2410 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len, |
| 2411 | transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2412 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2413 | rec->data_len += transform->maclen; |
| 2414 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2415 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2416 | } |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2417 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2418 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2419 | /* |
| 2420 | * Encrypt |
| 2421 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2422 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2423 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2424 | { |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2425 | int ret; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2426 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2427 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2428 | "including %d bytes of padding", |
| 2429 | rec->data_len, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2430 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2431 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2432 | transform->iv_enc, transform->ivlen, |
| 2433 | data, rec->data_len, |
| 2434 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2435 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2436 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2437 | return( ret ); |
| 2438 | } |
| 2439 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2440 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2441 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2442 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2443 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2444 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2445 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2446 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2447 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2448 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2449 | #if defined(MBEDTLS_GCM_C) || \ |
| 2450 | defined(MBEDTLS_CCM_C) || \ |
| 2451 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2452 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2453 | mode == MBEDTLS_MODE_CCM || |
| 2454 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2455 | { |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 2456 | int ret; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2457 | unsigned char iv[12]; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2458 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2459 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2460 | /* Check that there's space for both the authentication tag |
| 2461 | * and the explicit IV before and after the record content. */ |
| 2462 | if( post_avail < transform->taglen || |
| 2463 | rec->data_offset < explicit_iv_len ) |
| 2464 | { |
| 2465 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2466 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2467 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2468 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2469 | /* |
| 2470 | * Generate IV |
| 2471 | */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2472 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2473 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2474 | /* GCM and CCM: fixed || explicit (=seqnum) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2475 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2476 | memcpy( iv + transform->fixed_ivlen, rec->ctr, |
| 2477 | explicit_iv_len ); |
| 2478 | /* Prefix record content with explicit IV. */ |
| 2479 | memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2480 | } |
| 2481 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 2482 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2483 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2484 | unsigned char i; |
| 2485 | |
| 2486 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
| 2487 | |
| 2488 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2489 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2490 | } |
| 2491 | else |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2492 | { |
| 2493 | /* 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] | 2494 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2495 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2496 | } |
| 2497 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2498 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2499 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2500 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)", |
| 2501 | iv, transform->ivlen ); |
| 2502 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2503 | data - explicit_iv_len, explicit_iv_len ); |
| 2504 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2505 | add_data, add_data_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2506 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2507 | "including 0 bytes of padding", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2508 | rec->data_len ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2509 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2510 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2511 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2512 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2513 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2514 | if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2515 | iv, transform->ivlen, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2516 | add_data, add_data_len, /* add data */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2517 | data, rec->data_len, /* source */ |
| 2518 | data, &rec->data_len, /* destination */ |
| 2519 | data + rec->data_len, transform->taglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2520 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2521 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2522 | return( ret ); |
| 2523 | } |
| 2524 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2525 | MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", |
| 2526 | data + rec->data_len, transform->taglen ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2527 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2528 | rec->data_len += transform->taglen + explicit_iv_len; |
| 2529 | rec->data_offset -= explicit_iv_len; |
| 2530 | post_avail -= transform->taglen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2531 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2532 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2533 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2534 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2535 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2536 | ( 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] | 2537 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2538 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2539 | int ret; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2540 | size_t padlen, i; |
| 2541 | size_t olen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2542 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2543 | /* Currently we're always using minimal padding |
| 2544 | * (up to 255 bytes would be allowed). */ |
| 2545 | padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen; |
| 2546 | if( padlen == transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2547 | padlen = 0; |
| 2548 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2549 | /* Check there's enough space in the buffer for the padding. */ |
| 2550 | if( post_avail < padlen + 1 ) |
| 2551 | { |
| 2552 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2553 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2554 | } |
| 2555 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2556 | for( i = 0; i <= padlen; i++ ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2557 | data[rec->data_len + i] = (unsigned char) padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2558 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2559 | rec->data_len += padlen + 1; |
| 2560 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2561 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2562 | #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] | 2563 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2564 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per |
| 2565 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2566 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2567 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2568 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2569 | if( f_rng == NULL ) |
| 2570 | { |
| 2571 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) ); |
| 2572 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2573 | } |
| 2574 | |
| 2575 | if( rec->data_offset < transform->ivlen ) |
| 2576 | { |
| 2577 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2578 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2579 | } |
| 2580 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2581 | /* |
| 2582 | * Generate IV |
| 2583 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2584 | ret = f_rng( p_rng, transform->iv_enc, transform->ivlen ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2585 | if( ret != 0 ) |
| 2586 | return( ret ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2587 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2588 | memcpy( data - transform->ivlen, transform->iv_enc, |
| 2589 | transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2590 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2591 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2592 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2593 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2594 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2595 | "including %d bytes of IV and %d bytes of padding", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2596 | rec->data_len, transform->ivlen, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2597 | padlen + 1 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2598 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2599 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2600 | transform->iv_enc, |
| 2601 | transform->ivlen, |
| 2602 | data, rec->data_len, |
| 2603 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2604 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2605 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2606 | return( ret ); |
| 2607 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2608 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2609 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2610 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2611 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2612 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2613 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2614 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2615 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2616 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2617 | { |
| 2618 | /* |
| 2619 | * Save IV in SSL3 and TLS1 |
| 2620 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2621 | memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv, |
| 2622 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2623 | } |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2624 | else |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2625 | #endif |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2626 | { |
| 2627 | data -= transform->ivlen; |
| 2628 | rec->data_offset -= transform->ivlen; |
| 2629 | rec->data_len += transform->ivlen; |
| 2630 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2631 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2632 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2633 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2634 | { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 2635 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2636 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2637 | /* |
| 2638 | * MAC(MAC_write_key, seq_num + |
| 2639 | * TLSCipherText.type + |
| 2640 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2641 | * length_of( (IV +) ENC(...) ) + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2642 | * IV + // except for TLS 1.0 |
| 2643 | * ENC(content + padding + padding_length)); |
| 2644 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2645 | |
| 2646 | if( post_avail < transform->maclen) |
| 2647 | { |
| 2648 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2649 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2650 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2651 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2652 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2654 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2655 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2656 | add_data_len ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2657 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2658 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2659 | add_data_len ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2660 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2661 | data, rec->data_len ); |
| 2662 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2663 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2664 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2665 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2666 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2667 | rec->data_len += transform->maclen; |
| 2668 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2669 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2670 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2671 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2672 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2673 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2674 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2675 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2676 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2677 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2678 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2679 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2680 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2681 | /* Make extra sure authentication was performed, exactly once */ |
| 2682 | if( auth_done != 1 ) |
| 2683 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2684 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2685 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2686 | } |
| 2687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2688 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2689 | |
| 2690 | return( 0 ); |
| 2691 | } |
| 2692 | |
Hanno Becker | 605949f | 2019-07-12 08:23:59 +0100 | [diff] [blame] | 2693 | int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl, |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2694 | mbedtls_ssl_transform *transform, |
| 2695 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2696 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2697 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2698 | mbedtls_cipher_mode_t mode; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2699 | int ret, auth_done = 0; |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2700 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2701 | size_t padlen = 0, correct = 1; |
| 2702 | #endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2703 | unsigned char* data; |
Hanno Becker | 92fb4fa | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2704 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2705 | size_t add_data_len; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2706 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2707 | #if !defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2708 | ((void) ssl); |
| 2709 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2710 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2711 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2712 | if( rec == NULL || |
| 2713 | rec->buf == NULL || |
| 2714 | rec->buf_len < rec->data_offset || |
| 2715 | rec->buf_len - rec->data_offset < rec->data_len ) |
| 2716 | { |
| 2717 | 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] | 2718 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2719 | } |
| 2720 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2721 | data = rec->buf + rec->data_offset; |
| 2722 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2723 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2724 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2725 | /* |
| 2726 | * Match record's CID with incoming CID. |
| 2727 | */ |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2728 | if( rec->cid_len != transform->in_cid_len || |
| 2729 | memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) |
| 2730 | { |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 2731 | return( MBEDTLS_ERR_SSL_UNEXPECTED_CID ); |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2732 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2733 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2735 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2736 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2737 | { |
| 2738 | padlen = 0; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2739 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2740 | transform->iv_dec, |
| 2741 | transform->ivlen, |
| 2742 | data, rec->data_len, |
| 2743 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2744 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2745 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2746 | return( ret ); |
| 2747 | } |
| 2748 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2749 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2750 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2751 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2752 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2753 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2754 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2755 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2756 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2757 | #if defined(MBEDTLS_GCM_C) || \ |
| 2758 | defined(MBEDTLS_CCM_C) || \ |
| 2759 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2760 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2761 | mode == MBEDTLS_MODE_CCM || |
| 2762 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2763 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2764 | unsigned char iv[12]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2765 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2766 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2767 | /* |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2768 | * Prepare IV from explicit and implicit data. |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2769 | */ |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2770 | |
| 2771 | /* Check that there's enough space for the explicit IV |
| 2772 | * (at the beginning of the record) and the MAC (at the |
| 2773 | * end of the record). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2774 | if( rec->data_len < explicit_iv_len + transform->taglen ) |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2775 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2776 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2777 | "+ taglen (%d)", rec->data_len, |
| 2778 | explicit_iv_len, transform->taglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2779 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2780 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2781 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2782 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2783 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2784 | { |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2785 | /* GCM and CCM: fixed || explicit */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2786 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2787 | /* Fixed */ |
| 2788 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2789 | /* Explicit */ |
| 2790 | memcpy( iv + transform->fixed_ivlen, data, 8 ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2791 | } |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2792 | else |
| 2793 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2794 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 2795 | if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2796 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2797 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2798 | unsigned char i; |
| 2799 | |
| 2800 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2801 | |
| 2802 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2803 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2804 | } |
| 2805 | else |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2806 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2807 | { |
| 2808 | /* Reminder if we ever add an AEAD mode with a different size */ |
| 2809 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2810 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2811 | } |
| 2812 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2813 | /* Group changes to data, data_len, and add_data, because |
| 2814 | * add_data depends on data_len. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2815 | data += explicit_iv_len; |
| 2816 | rec->data_offset += explicit_iv_len; |
| 2817 | rec->data_len -= explicit_iv_len + transform->taglen; |
| 2818 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2819 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2820 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2821 | add_data, add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2822 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2823 | /* Because of the check above, we know that there are |
| 2824 | * explicit_iv_len Bytes preceeding data, and taglen |
| 2825 | * bytes following data + data_len. This justifies |
Hanno Becker | 2001665 | 2019-07-10 11:44:13 +0100 | [diff] [blame] | 2826 | * the debug message and the invocation of |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2827 | * mbedtls_cipher_auth_decrypt() below. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2828 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2829 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2830 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len, |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2831 | transform->taglen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2832 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2833 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2834 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2835 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2836 | if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec, |
| 2837 | iv, transform->ivlen, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2838 | add_data, add_data_len, |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2839 | data, rec->data_len, |
| 2840 | data, &olen, |
| 2841 | data + rec->data_len, |
| 2842 | transform->taglen ) ) != 0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2843 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2844 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2845 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2846 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 2847 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2848 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2849 | return( ret ); |
| 2850 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2851 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2852 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2853 | /* Double-check that AEAD decryption doesn't change content length. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2854 | if( olen != rec->data_len ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2855 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2856 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2857 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2858 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2859 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2860 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2861 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2862 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2863 | ( 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] | 2864 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2865 | { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2866 | size_t minlen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2867 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2868 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2869 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2870 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2871 | #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] | 2872 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 2873 | { |
| 2874 | /* The ciphertext is prefixed with the CBC IV. */ |
| 2875 | minlen += transform->ivlen; |
| 2876 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2877 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2878 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2879 | /* Size considerations: |
| 2880 | * |
| 2881 | * - The CBC cipher text must not be empty and hence |
| 2882 | * at least of size transform->ivlen. |
| 2883 | * |
| 2884 | * Together with the potential IV-prefix, this explains |
| 2885 | * the first of the two checks below. |
| 2886 | * |
| 2887 | * - The record must contain a MAC, either in plain or |
| 2888 | * encrypted, depending on whether Encrypt-then-MAC |
| 2889 | * is used or not. |
| 2890 | * - If it is, the message contains the IV-prefix, |
| 2891 | * the CBC ciphertext, and the MAC. |
| 2892 | * - If it is not, the padded plaintext, and hence |
| 2893 | * the CBC ciphertext, has at least length maclen + 1 |
| 2894 | * because there is at least the padding length byte. |
| 2895 | * |
| 2896 | * As the CBC ciphertext is not empty, both cases give the |
| 2897 | * lower bound minlen + maclen + 1 on the record size, which |
| 2898 | * we test for in the second check below. |
| 2899 | */ |
| 2900 | if( rec->data_len < minlen + transform->ivlen || |
| 2901 | rec->data_len < minlen + transform->maclen + 1 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2902 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2903 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2904 | "+ 1 ) ( + expl IV )", rec->data_len, |
| 2905 | transform->ivlen, |
| 2906 | transform->maclen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2907 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2908 | } |
| 2909 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2910 | /* |
| 2911 | * Authenticate before decrypt if enabled |
| 2912 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2913 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2914 | if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2915 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2916 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2917 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2918 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2919 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2920 | /* Update data_len in tandem with add_data. |
| 2921 | * |
| 2922 | * The subtraction is safe because of the previous check |
| 2923 | * data_len >= minlen + maclen + 1. |
| 2924 | * |
| 2925 | * Afterwards, we know that data + data_len is followed by at |
| 2926 | * least maclen Bytes, which justifies the call to |
| 2927 | * mbedtls_ssl_safer_memcmp() below. |
| 2928 | * |
| 2929 | * Further, we still know that data_len > minlen */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2930 | rec->data_len -= transform->maclen; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2931 | 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] | 2932 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2933 | /* Calculate expected MAC. */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2934 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
| 2935 | add_data_len ); |
| 2936 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 2937 | add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2938 | mbedtls_md_hmac_update( &transform->md_ctx_dec, |
| 2939 | data, rec->data_len ); |
| 2940 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
| 2941 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2942 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2943 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, |
| 2944 | transform->maclen ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2945 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2946 | transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2947 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2948 | /* Compare expected MAC with MAC at the end of the record. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2949 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 2950 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2951 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2952 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2953 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2954 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2955 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2956 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2957 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2958 | |
| 2959 | /* |
| 2960 | * Check length sanity |
| 2961 | */ |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2962 | |
| 2963 | /* We know from above that data_len > minlen >= 0, |
| 2964 | * so the following check in particular implies that |
| 2965 | * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2966 | if( rec->data_len % transform->ivlen != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2967 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2968 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2969 | rec->data_len, transform->ivlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2970 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2971 | } |
| 2972 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2973 | #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] | 2974 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2975 | * 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] | 2976 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2977 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2978 | { |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2979 | /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2980 | memcpy( transform->iv_dec, data, transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2981 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2982 | data += transform->ivlen; |
| 2983 | rec->data_offset += transform->ivlen; |
| 2984 | rec->data_len -= transform->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2985 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2986 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2987 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2988 | /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */ |
| 2989 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2990 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2991 | transform->iv_dec, transform->ivlen, |
| 2992 | data, rec->data_len, data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2993 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2994 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2995 | return( ret ); |
| 2996 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2997 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 2998 | /* Double-check that length hasn't changed during decryption. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2999 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3000 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3001 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3002 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3003 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 3004 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3005 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3006 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3007 | { |
| 3008 | /* |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3009 | * Save IV in SSL3 and TLS1, where CBC decryption of consecutive |
| 3010 | * records is equivalent to CBC decryption of the concatenation |
| 3011 | * of the records; in other words, IVs are maintained across |
| 3012 | * record decryptions. |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3013 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3014 | memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv, |
| 3015 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3016 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3017 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3018 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3019 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 3020 | * subtracted at most minlen and maclen up to this point, |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3021 | * data_len > 0 (because of data_len % ivlen == 0, it's actually |
| 3022 | * >= ivlen ). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3023 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3024 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3025 | if( auth_done == 1 ) |
| 3026 | { |
| 3027 | correct *= ( rec->data_len >= padlen + 1 ); |
| 3028 | padlen *= ( rec->data_len >= padlen + 1 ); |
| 3029 | } |
| 3030 | else |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3031 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3032 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3033 | if( rec->data_len < transform->maclen + padlen + 1 ) |
| 3034 | { |
| 3035 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", |
| 3036 | rec->data_len, |
| 3037 | transform->maclen, |
| 3038 | padlen + 1 ) ); |
| 3039 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 3040 | #endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3041 | |
| 3042 | correct *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
| 3043 | padlen *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3044 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3045 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3046 | padlen++; |
| 3047 | |
| 3048 | /* Regardless of the validity of the padding, |
| 3049 | * we have data_len >= padlen here. */ |
| 3050 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3051 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3052 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3053 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3054 | if( padlen > transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3055 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3056 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 3057 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3058 | "should be no more than %d", |
| 3059 | padlen, transform->ivlen ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 3060 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3061 | correct = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3062 | } |
| 3063 | } |
| 3064 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3065 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 3066 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3067 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3068 | if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3069 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3070 | /* The padding check involves a series of up to 256 |
| 3071 | * consecutive memory reads at the end of the record |
| 3072 | * plaintext buffer. In order to hide the length and |
| 3073 | * validity of the padding, always perform exactly |
| 3074 | * `min(256,plaintext_len)` reads (but take into account |
| 3075 | * only the last `padlen` bytes for the padding check). */ |
| 3076 | size_t pad_count = 0; |
| 3077 | size_t real_count = 0; |
| 3078 | volatile unsigned char* const check = data; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3079 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3080 | /* Index of first padding byte; it has been ensured above |
| 3081 | * that the subtraction is safe. */ |
| 3082 | size_t const padding_idx = rec->data_len - padlen; |
| 3083 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 3084 | size_t const start_idx = rec->data_len - num_checks; |
| 3085 | size_t idx; |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 3086 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3087 | for( idx = start_idx; idx < rec->data_len; idx++ ) |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 3088 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3089 | real_count |= ( idx >= padding_idx ); |
| 3090 | pad_count += real_count * ( check[idx] == padlen - 1 ); |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 3091 | } |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3092 | correct &= ( pad_count == padlen ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3093 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3094 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 3095 | if( padlen > 0 && correct == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3096 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 3097 | #endif |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3098 | padlen &= correct * 0x1FF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3099 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3100 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3101 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3102 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3103 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3104 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3105 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3106 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3107 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3108 | /* If the padding was found to be invalid, padlen == 0 |
| 3109 | * and the subtraction is safe. If the padding was found valid, |
| 3110 | * padlen hasn't been changed and the previous assertion |
| 3111 | * data_len >= padlen still holds. */ |
| 3112 | rec->data_len -= padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3113 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3114 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3115 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 3116 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3117 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3118 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3119 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3120 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3121 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 3122 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3123 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3124 | data, rec->data_len ); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 3125 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3126 | |
| 3127 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3128 | * Authenticate if not done yet. |
| 3129 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3130 | */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 3131 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3132 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3133 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 3134 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 3135 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3136 | /* If the initial value of padlen was such that |
| 3137 | * data_len < maclen + padlen + 1, then padlen |
| 3138 | * got reset to 1, and the initial check |
| 3139 | * data_len >= minlen + maclen + 1 |
| 3140 | * guarantees that at this point we still |
| 3141 | * have at least data_len >= maclen. |
| 3142 | * |
| 3143 | * If the initial value of padlen was such that |
| 3144 | * data_len >= maclen + padlen + 1, then we have |
| 3145 | * subtracted either padlen + 1 (if the padding was correct) |
| 3146 | * or 0 (if the padding was incorrect) since then, |
| 3147 | * hence data_len >= maclen in any case. |
| 3148 | */ |
| 3149 | rec->data_len -= transform->maclen; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3150 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3151 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3152 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3153 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3154 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3155 | ssl_mac( &transform->md_ctx_dec, |
| 3156 | transform->mac_dec, |
| 3157 | data, rec->data_len, |
| 3158 | rec->ctr, rec->type, |
| 3159 | mac_expect ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3160 | } |
| 3161 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3162 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 3163 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3164 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3165 | if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3166 | { |
| 3167 | /* |
| 3168 | * Process MAC and always update for padlen afterwards to make |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3169 | * total time independent of padlen. |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3170 | * |
| 3171 | * Known timing attacks: |
| 3172 | * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf) |
| 3173 | * |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3174 | * To compensate for different timings for the MAC calculation |
| 3175 | * depending on how much padding was removed (which is determined |
| 3176 | * by padlen), process extra_run more blocks through the hash |
| 3177 | * function. |
| 3178 | * |
| 3179 | * The formula in the paper is |
| 3180 | * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 ) |
| 3181 | * where L1 is the size of the header plus the decrypted message |
| 3182 | * plus CBC padding and L2 is the size of the header plus the |
| 3183 | * decrypted message. This is for an underlying hash function |
| 3184 | * with 64-byte blocks. |
| 3185 | * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values |
| 3186 | * correctly. We round down instead of up, so -56 is the correct |
| 3187 | * value for our calculations instead of -55. |
| 3188 | * |
Gilles Peskine | 1bd9d58 | 2018-06-04 11:58:44 +0200 | [diff] [blame] | 3189 | * Repeat the formula rather than defining a block_size variable. |
| 3190 | * This avoids requiring division by a variable at runtime |
| 3191 | * (which would be marginally less efficient and would require |
| 3192 | * linking an extra division function in some builds). |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3193 | */ |
| 3194 | size_t j, extra_run = 0; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3195 | unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE]; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3196 | |
| 3197 | /* |
| 3198 | * The next two sizes are the minimum and maximum values of |
| 3199 | * in_msglen over all padlen values. |
| 3200 | * |
| 3201 | * They're independent of padlen, since we previously did |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3202 | * data_len -= padlen. |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3203 | * |
| 3204 | * Note that max_len + maclen is never more than the buffer |
| 3205 | * length, as we previously did in_msglen -= maclen too. |
| 3206 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3207 | const size_t max_len = rec->data_len + padlen; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3208 | const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0; |
| 3209 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3210 | memset( tmp, 0, sizeof( tmp ) ); |
| 3211 | |
| 3212 | switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) ) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3213 | { |
Gilles Peskine | d0e55a4 | 2018-06-04 12:03:30 +0200 | [diff] [blame] | 3214 | #if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \ |
| 3215 | defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3216 | case MBEDTLS_MD_MD5: |
| 3217 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3218 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3219 | /* 8 bytes of message size, 64-byte compression blocks */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3220 | extra_run = |
| 3221 | ( add_data_len + rec->data_len + padlen + 8 ) / 64 - |
| 3222 | ( add_data_len + rec->data_len + 8 ) / 64; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3223 | break; |
| 3224 | #endif |
Gilles Peskine | a7fe25d | 2018-06-04 12:01:18 +0200 | [diff] [blame] | 3225 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3226 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3227 | /* 16 bytes of message size, 128-byte compression blocks */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3228 | extra_run = |
| 3229 | ( add_data_len + rec->data_len + padlen + 16 ) / 128 - |
| 3230 | ( add_data_len + rec->data_len + 16 ) / 128; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3231 | break; |
| 3232 | #endif |
| 3233 | default: |
Gilles Peskine | 5c38984 | 2018-06-04 12:02:43 +0200 | [diff] [blame] | 3234 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3235 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3236 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3237 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3238 | extra_run &= correct * 0xFF; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3239 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3240 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 3241 | add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3242 | mbedtls_md_hmac_update( &transform->md_ctx_dec, data, |
| 3243 | rec->data_len ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3244 | /* Make sure we access everything even when padlen > 0. This |
| 3245 | * makes the synchronisation requirements for just-in-time |
| 3246 | * Prime+Probe attacks much tighter and hopefully impractical. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3247 | ssl_read_memory( data + rec->data_len, padlen ); |
| 3248 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3249 | |
| 3250 | /* Call mbedtls_md_process at least once due to cache attacks |
| 3251 | * that observe whether md_process() was called of not */ |
Manuel Pégourié-Gonnard | 47fede0 | 2015-04-29 01:35:48 +0200 | [diff] [blame] | 3252 | for( j = 0; j < extra_run + 1; j++ ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3253 | mbedtls_md_process( &transform->md_ctx_dec, tmp ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3254 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3255 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3256 | |
| 3257 | /* Make sure we access all the memory that could contain the MAC, |
| 3258 | * before we check it in the next code block. This makes the |
| 3259 | * synchronisation requirements for just-in-time Prime+Probe |
| 3260 | * attacks much tighter and hopefully impractical. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3261 | ssl_read_memory( data + min_len, |
| 3262 | max_len - min_len + transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3263 | } |
| 3264 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3265 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3266 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3267 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3268 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3269 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3270 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3271 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3272 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3273 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen ); |
| 3274 | 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] | 3275 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3276 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3277 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 3278 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3279 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3280 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 3281 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3282 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3283 | correct = 0; |
| 3284 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3285 | auth_done++; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3286 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 3287 | |
| 3288 | /* |
| 3289 | * Finally check the correct flag |
| 3290 | */ |
| 3291 | if( correct == 0 ) |
| 3292 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 3293 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3294 | |
| 3295 | /* Make extra sure authentication was performed, exactly once */ |
| 3296 | if( auth_done != 1 ) |
| 3297 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3298 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3299 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3300 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3301 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3302 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3303 | if( rec->cid_len != 0 ) |
| 3304 | { |
| 3305 | ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len, |
| 3306 | &rec->type ); |
| 3307 | if( ret != 0 ) |
| 3308 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3309 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3310 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3311 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3312 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3313 | |
| 3314 | return( 0 ); |
| 3315 | } |
| 3316 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 3317 | #undef MAC_NONE |
| 3318 | #undef MAC_PLAINTEXT |
| 3319 | #undef MAC_CIPHERTEXT |
| 3320 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3321 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3322 | /* |
| 3323 | * Compression/decompression functions |
| 3324 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3325 | static int ssl_compress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3326 | { |
| 3327 | int ret; |
| 3328 | unsigned char *msg_post = ssl->out_msg; |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3329 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3330 | size_t len_pre = ssl->out_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3331 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3332 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3333 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3334 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3335 | if( len_pre == 0 ) |
| 3336 | return( 0 ); |
| 3337 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3338 | memcpy( msg_pre, ssl->out_msg, len_pre ); |
| 3339 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3340 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3341 | ssl->out_msglen ) ); |
| 3342 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3343 | MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3344 | ssl->out_msg, ssl->out_msglen ); |
| 3345 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3346 | ssl->transform_out->ctx_deflate.next_in = msg_pre; |
| 3347 | ssl->transform_out->ctx_deflate.avail_in = len_pre; |
| 3348 | ssl->transform_out->ctx_deflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3349 | 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] | 3350 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3351 | ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3352 | if( ret != Z_OK ) |
| 3353 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3354 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) ); |
| 3355 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3358 | ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN - |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3359 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3360 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3361 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3362 | ssl->out_msglen ) ); |
| 3363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3364 | MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3365 | ssl->out_msg, ssl->out_msglen ); |
| 3366 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3367 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3368 | |
| 3369 | return( 0 ); |
| 3370 | } |
| 3371 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3372 | static int ssl_decompress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3373 | { |
| 3374 | int ret; |
| 3375 | unsigned char *msg_post = ssl->in_msg; |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3376 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3377 | size_t len_pre = ssl->in_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3378 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3379 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3380 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3381 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3382 | if( len_pre == 0 ) |
| 3383 | return( 0 ); |
| 3384 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3385 | memcpy( msg_pre, ssl->in_msg, len_pre ); |
| 3386 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3387 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3388 | ssl->in_msglen ) ); |
| 3389 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3390 | MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3391 | ssl->in_msg, ssl->in_msglen ); |
| 3392 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3393 | ssl->transform_in->ctx_inflate.next_in = msg_pre; |
| 3394 | ssl->transform_in->ctx_inflate.avail_in = len_pre; |
| 3395 | ssl->transform_in->ctx_inflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3396 | ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3397 | header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3398 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3399 | ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3400 | if( ret != Z_OK ) |
| 3401 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3402 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) ); |
| 3403 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3404 | } |
| 3405 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3406 | ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3407 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3408 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3409 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3410 | ssl->in_msglen ) ); |
| 3411 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3412 | MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3413 | ssl->in_msg, ssl->in_msglen ); |
| 3414 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3415 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3416 | |
| 3417 | return( 0 ); |
| 3418 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3419 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3420 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3421 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3422 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3424 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3425 | static int ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3426 | { |
| 3427 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 3428 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3429 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3430 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3431 | 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] | 3432 | unsigned char doublings = 1; |
| 3433 | |
| 3434 | while( ratio != 0 ) |
| 3435 | { |
| 3436 | ++doublings; |
| 3437 | ratio >>= 1; |
| 3438 | } |
| 3439 | |
| 3440 | if( ++ssl->renego_records_seen > doublings ) |
| 3441 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 3442 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3443 | return( 0 ); |
| 3444 | } |
| 3445 | } |
| 3446 | |
| 3447 | return( ssl_write_hello_request( ssl ) ); |
| 3448 | } |
| 3449 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3450 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3451 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3452 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3453 | * Fill the input message buffer by appending data to it. |
| 3454 | * 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] | 3455 | * |
| 3456 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 3457 | * available (from this read and/or a previous one). Otherwise, an error code |
| 3458 | * is returned (possibly EOF or WANT_READ). |
| 3459 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3460 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 3461 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 3462 | * since we always read a whole datagram at once. |
| 3463 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3464 | * 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] | 3465 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3466 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3467 | 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] | 3468 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3469 | int ret; |
| 3470 | size_t len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3471 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3472 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3473 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3474 | if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL ) |
| 3475 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3476 | 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] | 3477 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3478 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3479 | } |
| 3480 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3481 | 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] | 3482 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3483 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); |
| 3484 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3485 | } |
| 3486 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3487 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3488 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3489 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3490 | uint32_t timeout; |
| 3491 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3492 | /* Just to be sure */ |
| 3493 | if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) |
| 3494 | { |
| 3495 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3496 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3497 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3498 | } |
| 3499 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3500 | /* |
| 3501 | * The point is, we need to always read a full datagram at once, so we |
| 3502 | * sometimes read more then requested, and handle the additional data. |
| 3503 | * It could be the rest of the current record (while fetching the |
| 3504 | * header) and/or some other records in the same datagram. |
| 3505 | */ |
| 3506 | |
| 3507 | /* |
| 3508 | * Move to the next record in the already read datagram if applicable |
| 3509 | */ |
| 3510 | if( ssl->next_record_offset != 0 ) |
| 3511 | { |
| 3512 | if( ssl->in_left < ssl->next_record_offset ) |
| 3513 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3514 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3515 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | ssl->in_left -= ssl->next_record_offset; |
| 3519 | |
| 3520 | if( ssl->in_left != 0 ) |
| 3521 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3522 | 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] | 3523 | ssl->next_record_offset ) ); |
| 3524 | memmove( ssl->in_hdr, |
| 3525 | ssl->in_hdr + ssl->next_record_offset, |
| 3526 | ssl->in_left ); |
| 3527 | } |
| 3528 | |
| 3529 | ssl->next_record_offset = 0; |
| 3530 | } |
| 3531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3532 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3533 | ssl->in_left, nb_want ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3534 | |
| 3535 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3536 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3537 | */ |
| 3538 | if( nb_want <= ssl->in_left) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3539 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3540 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3541 | return( 0 ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3542 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3543 | |
| 3544 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 3545 | * 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] | 3546 | * are not at the beginning of a new record, the caller did something |
| 3547 | * wrong. |
| 3548 | */ |
| 3549 | if( ssl->in_left != 0 ) |
| 3550 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3551 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3552 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3553 | } |
| 3554 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3555 | /* |
| 3556 | * Don't even try to read if time's out already. |
| 3557 | * This avoids by-passing the timer when repeatedly receiving messages |
| 3558 | * that will end up being dropped. |
| 3559 | */ |
| 3560 | if( ssl_check_timer( ssl ) != 0 ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3561 | { |
| 3562 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3563 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3564 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3565 | else |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3566 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3567 | 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] | 3568 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3569 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3570 | timeout = ssl->handshake->retransmit_timeout; |
| 3571 | else |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3572 | timeout = ssl->conf->read_timeout; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3573 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3574 | 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] | 3575 | |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3576 | if( ssl->f_recv_timeout != NULL ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3577 | ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, |
| 3578 | timeout ); |
| 3579 | else |
| 3580 | ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len ); |
| 3581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3582 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3583 | |
| 3584 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3585 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3586 | } |
| 3587 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3588 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3589 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3590 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3591 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3592 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3593 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3594 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3595 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) |
| 3596 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3597 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3598 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3599 | } |
| 3600 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3601 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3602 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3603 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3604 | return( ret ); |
| 3605 | } |
| 3606 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3607 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3608 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3609 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3610 | else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3611 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3612 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3613 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3614 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3615 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3616 | return( ret ); |
| 3617 | } |
| 3618 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3619 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3620 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3621 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3622 | } |
| 3623 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3624 | if( ret < 0 ) |
| 3625 | return( ret ); |
| 3626 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3627 | ssl->in_left = ret; |
| 3628 | } |
| 3629 | else |
| 3630 | #endif |
| 3631 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3632 | 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] | 3633 | ssl->in_left, nb_want ) ); |
| 3634 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3635 | while( ssl->in_left < nb_want ) |
| 3636 | { |
| 3637 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3638 | |
| 3639 | if( ssl_check_timer( ssl ) != 0 ) |
| 3640 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
| 3641 | else |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3642 | { |
| 3643 | if( ssl->f_recv_timeout != NULL ) |
| 3644 | { |
| 3645 | ret = ssl->f_recv_timeout( ssl->p_bio, |
| 3646 | ssl->in_hdr + ssl->in_left, len, |
| 3647 | ssl->conf->read_timeout ); |
| 3648 | } |
| 3649 | else |
| 3650 | { |
| 3651 | ret = ssl->f_recv( ssl->p_bio, |
| 3652 | ssl->in_hdr + ssl->in_left, len ); |
| 3653 | } |
| 3654 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3655 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3656 | 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] | 3657 | ssl->in_left, nb_want ) ); |
| 3658 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3659 | |
| 3660 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3661 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3662 | |
| 3663 | if( ret < 0 ) |
| 3664 | return( ret ); |
| 3665 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3666 | if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3667 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3668 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3669 | ( "f_recv returned %d bytes but only %lu were requested", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3670 | ret, (unsigned long)len ) ); |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3671 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3672 | } |
| 3673 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3674 | ssl->in_left += ret; |
| 3675 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3678 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3679 | |
| 3680 | return( 0 ); |
| 3681 | } |
| 3682 | |
| 3683 | /* |
| 3684 | * Flush any data not yet written |
| 3685 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3686 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3687 | { |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 3688 | int ret; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3689 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3691 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3692 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3693 | if( ssl->f_send == NULL ) |
| 3694 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3695 | 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] | 3696 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3697 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3698 | } |
| 3699 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3700 | /* Avoid incrementing counter if data is flushed */ |
| 3701 | if( ssl->out_left == 0 ) |
| 3702 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3703 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3704 | return( 0 ); |
| 3705 | } |
| 3706 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3707 | while( ssl->out_left > 0 ) |
| 3708 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3709 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d", |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 3710 | mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3711 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3712 | buf = ssl->out_hdr - ssl->out_left; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3713 | ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left ); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 3714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3715 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3716 | |
| 3717 | if( ret <= 0 ) |
| 3718 | return( ret ); |
| 3719 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3720 | 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] | 3721 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3722 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3723 | ( "f_send returned %d bytes but only %lu bytes were sent", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3724 | ret, (unsigned long)ssl->out_left ) ); |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 3725 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3726 | } |
| 3727 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3728 | ssl->out_left -= ret; |
| 3729 | } |
| 3730 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3731 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3732 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3733 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3734 | ssl->out_hdr = ssl->out_buf; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3735 | } |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3736 | else |
| 3737 | #endif |
| 3738 | { |
| 3739 | ssl->out_hdr = ssl->out_buf + 8; |
| 3740 | } |
| 3741 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3742 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3743 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3744 | |
| 3745 | return( 0 ); |
| 3746 | } |
| 3747 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3748 | /* |
| 3749 | * Functions to handle the DTLS retransmission state machine |
| 3750 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3751 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3752 | /* |
| 3753 | * Append current handshake message to current outgoing flight |
| 3754 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3755 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3756 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3757 | mbedtls_ssl_flight_item *msg; |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3758 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) ); |
| 3759 | MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight", |
| 3760 | ssl->out_msg, ssl->out_msglen ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3761 | |
| 3762 | /* Allocate space for current message */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3763 | 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] | 3764 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3765 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3766 | sizeof( mbedtls_ssl_flight_item ) ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3767 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3768 | } |
| 3769 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3770 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3771 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3772 | 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] | 3773 | mbedtls_free( msg ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3774 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | /* Copy current handshake message with headers */ |
| 3778 | memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); |
| 3779 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3780 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3781 | msg->next = NULL; |
| 3782 | |
| 3783 | /* Append to the current flight */ |
| 3784 | if( ssl->handshake->flight == NULL ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3785 | ssl->handshake->flight = msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3786 | else |
| 3787 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3788 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3789 | while( cur->next != NULL ) |
| 3790 | cur = cur->next; |
| 3791 | cur->next = msg; |
| 3792 | } |
| 3793 | |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3794 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3795 | return( 0 ); |
| 3796 | } |
| 3797 | |
| 3798 | /* |
| 3799 | * Free the current flight of handshake messages |
| 3800 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3801 | static void ssl_flight_free( mbedtls_ssl_flight_item *flight ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3802 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3803 | mbedtls_ssl_flight_item *cur = flight; |
| 3804 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3805 | |
| 3806 | while( cur != NULL ) |
| 3807 | { |
| 3808 | next = cur->next; |
| 3809 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3810 | mbedtls_free( cur->p ); |
| 3811 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3812 | |
| 3813 | cur = next; |
| 3814 | } |
| 3815 | } |
| 3816 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3817 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3818 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3819 | #endif |
| 3820 | |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3821 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3822 | * Swap transform_out and out_ctr with the alternative ones |
| 3823 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3824 | static void ssl_swap_epochs( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3825 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3826 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3827 | unsigned char tmp_out_ctr[8]; |
| 3828 | |
| 3829 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) |
| 3830 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3831 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3832 | return; |
| 3833 | } |
| 3834 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3835 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3836 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3837 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3838 | tmp_transform = ssl->transform_out; |
| 3839 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 3840 | ssl->handshake->alt_transform_out = tmp_transform; |
| 3841 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3842 | /* Swap epoch + sequence_number */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3843 | memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 ); |
| 3844 | 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] | 3845 | memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3846 | |
| 3847 | /* Adjust to the newly activated transform */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 3848 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3849 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3850 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3851 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3852 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3853 | 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] | 3854 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3855 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 3856 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3857 | } |
| 3858 | } |
| 3859 | #endif |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | /* |
| 3863 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3864 | */ |
| 3865 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) |
| 3866 | { |
| 3867 | int ret = 0; |
| 3868 | |
| 3869 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); |
| 3870 | |
| 3871 | ret = mbedtls_ssl_flight_transmit( ssl ); |
| 3872 | |
| 3873 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); |
| 3874 | |
| 3875 | return( ret ); |
| 3876 | } |
| 3877 | |
| 3878 | /* |
| 3879 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3880 | * |
| 3881 | * Need to remember the current message in case flush_output returns |
| 3882 | * 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] | 3883 | * 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] | 3884 | */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3885 | int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3886 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3887 | int ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3888 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3889 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3890 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3891 | { |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3892 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3893 | |
| 3894 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3895 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3896 | ssl_swap_epochs( ssl ); |
| 3897 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3898 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3899 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3900 | |
| 3901 | while( ssl->handshake->cur_msg != NULL ) |
| 3902 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3903 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3904 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3905 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3906 | int const is_finished = |
| 3907 | ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3908 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ); |
| 3909 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3910 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
| 3911 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
| 3912 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3913 | /* Swap epochs before sending Finished: we can't do it after |
| 3914 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 3915 | * Must be done before copying, may change out_msg pointer */ |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3916 | 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] | 3917 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3918 | 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] | 3919 | ssl_swap_epochs( ssl ); |
| 3920 | } |
| 3921 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3922 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 3923 | if( ret < 0 ) |
| 3924 | return( ret ); |
| 3925 | max_frag_len = (size_t) ret; |
| 3926 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3927 | /* CCS is copied as is, while HS messages may need fragmentation */ |
| 3928 | if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3929 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3930 | if( max_frag_len == 0 ) |
| 3931 | { |
| 3932 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3933 | return( ret ); |
| 3934 | |
| 3935 | continue; |
| 3936 | } |
| 3937 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3938 | memcpy( ssl->out_msg, cur->p, cur->len ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3939 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3940 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3941 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3942 | /* Update position inside current message */ |
| 3943 | ssl->handshake->cur_msg_p += cur->len; |
| 3944 | } |
| 3945 | else |
| 3946 | { |
| 3947 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 3948 | const size_t hs_len = cur->len - 12; |
| 3949 | const size_t frag_off = p - ( cur->p + 12 ); |
| 3950 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3951 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3952 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3953 | 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] | 3954 | { |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3955 | if( is_finished ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3956 | ssl_swap_epochs( ssl ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3957 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3958 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3959 | return( ret ); |
| 3960 | |
| 3961 | continue; |
| 3962 | } |
| 3963 | max_hs_frag_len = max_frag_len - 12; |
| 3964 | |
| 3965 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
| 3966 | max_hs_frag_len : rem_len; |
| 3967 | |
| 3968 | if( frag_off == 0 && cur_hs_frag_len != hs_len ) |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3969 | { |
| 3970 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)", |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3971 | (unsigned) cur_hs_frag_len, |
| 3972 | (unsigned) max_hs_frag_len ) ); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3973 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 3974 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3975 | /* Messages are stored with handshake headers as if not fragmented, |
| 3976 | * copy beginning of headers then fill fragmentation fields. |
| 3977 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
| 3978 | memcpy( ssl->out_msg, cur->p, 6 ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3979 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3980 | ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); |
| 3981 | ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff ); |
| 3982 | ssl->out_msg[8] = ( ( frag_off ) & 0xff ); |
| 3983 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3984 | ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); |
| 3985 | ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff ); |
| 3986 | ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff ); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3987 | |
| 3988 | MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); |
| 3989 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 3990 | /* Copy the handshake message content and set records fields */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3991 | memcpy( ssl->out_msg + 12, p, cur_hs_frag_len ); |
| 3992 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3993 | ssl->out_msgtype = cur->type; |
| 3994 | |
| 3995 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3996 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3997 | } |
| 3998 | |
| 3999 | /* If done with the current message move to the next one if any */ |
| 4000 | if( ssl->handshake->cur_msg_p >= cur->p + cur->len ) |
| 4001 | { |
| 4002 | if( cur->next != NULL ) |
| 4003 | { |
| 4004 | ssl->handshake->cur_msg = cur->next; |
| 4005 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
| 4006 | } |
| 4007 | else |
| 4008 | { |
| 4009 | ssl->handshake->cur_msg = NULL; |
| 4010 | ssl->handshake->cur_msg_p = NULL; |
| 4011 | } |
| 4012 | } |
| 4013 | |
| 4014 | /* Actually send the message out */ |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 4015 | if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4016 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4017 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4018 | return( ret ); |
| 4019 | } |
| 4020 | } |
| 4021 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4022 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 4023 | return( ret ); |
| 4024 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4025 | /* Update state and set timer */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4026 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 4027 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 4028 | else |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 4029 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4030 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 4031 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
| 4032 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4033 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4034 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4035 | |
| 4036 | return( 0 ); |
| 4037 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4038 | |
| 4039 | /* |
| 4040 | * To be called when the last message of an incoming flight is received. |
| 4041 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4042 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4043 | { |
| 4044 | /* We won't need to resend that one any more */ |
| 4045 | ssl_flight_free( ssl->handshake->flight ); |
| 4046 | ssl->handshake->flight = NULL; |
| 4047 | ssl->handshake->cur_msg = NULL; |
| 4048 | |
| 4049 | /* The next incoming flight will start with this msg_seq */ |
| 4050 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 4051 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4052 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 4053 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4054 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4055 | /* Clear future message buffering structure. */ |
| 4056 | ssl_buffering_free( ssl ); |
| 4057 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 4058 | /* Cancel timer */ |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4059 | ssl_set_timer( ssl, 0 ); |
| 4060 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4061 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4062 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4063 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4064 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4065 | } |
| 4066 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4067 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4068 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4069 | |
| 4070 | /* |
| 4071 | * To be called when the last message of an outgoing flight is send. |
| 4072 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4073 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4074 | { |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 4075 | ssl_reset_retransmit_timeout( ssl ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 4076 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4077 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4078 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4079 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4080 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4081 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4082 | } |
| 4083 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4084 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4085 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4086 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4087 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4088 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4089 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4090 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4091 | |
| 4092 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4093 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4094 | * |
| 4095 | * - fill in handshake headers |
| 4096 | * - update handshake checksum |
| 4097 | * - DTLS: save message for resending |
| 4098 | * - then pass to the record layer |
| 4099 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4100 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 4101 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4102 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4103 | * Inputs: |
| 4104 | * - ssl->out_msglen: 4 + actual handshake message len |
| 4105 | * (4 is the size of handshake headers for TLS) |
| 4106 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 4107 | * - ssl->out_msg + 4: the handshake message body |
| 4108 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 4109 | * 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] | 4110 | * - ssl->out_msglen: the length of the record contents |
| 4111 | * (including handshake headers but excluding record headers) |
| 4112 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4113 | */ |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4114 | int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4115 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4116 | int ret; |
| 4117 | const size_t hs_len = ssl->out_msglen - 4; |
| 4118 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4119 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4120 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) ); |
| 4121 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4122 | /* |
| 4123 | * Sanity checks |
| 4124 | */ |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4125 | if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4126 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 4127 | { |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4128 | /* In SSLv3, the client might send a NoCertificate alert. */ |
| 4129 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
| 4130 | if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 4131 | ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 4132 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ) |
| 4133 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 4134 | { |
| 4135 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4136 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4137 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4138 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4139 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 4140 | /* Whenever we send anything different from a |
| 4141 | * HelloRequest we should be in a handshake - double check. */ |
| 4142 | if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4143 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) && |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4144 | ssl->handshake == NULL ) |
| 4145 | { |
| 4146 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4147 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4148 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4149 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4150 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4151 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4152 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4153 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4154 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4155 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4156 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4157 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4158 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4159 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 4160 | /* Double-check that we did not exceed the bounds |
| 4161 | * of the outgoing record buffer. |
| 4162 | * This should never fail as the various message |
| 4163 | * writing functions must obey the bounds of the |
| 4164 | * outgoing record buffer, but better be safe. |
| 4165 | * |
| 4166 | * Note: We deliberately do not check for the MTU or MFL here. |
| 4167 | */ |
| 4168 | if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 4169 | { |
| 4170 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: " |
| 4171 | "size %u, maximum %u", |
| 4172 | (unsigned) ssl->out_msglen, |
| 4173 | (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 4174 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4175 | } |
| 4176 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4177 | /* |
| 4178 | * Fill handshake headers |
| 4179 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4180 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4181 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4182 | ssl->out_msg[1] = (unsigned char)( hs_len >> 16 ); |
| 4183 | ssl->out_msg[2] = (unsigned char)( hs_len >> 8 ); |
| 4184 | ssl->out_msg[3] = (unsigned char)( hs_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4185 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4186 | /* |
| 4187 | * DTLS has additional fields in the Handshake layer, |
| 4188 | * between the length field and the actual payload: |
| 4189 | * uint16 message_seq; |
| 4190 | * uint24 fragment_offset; |
| 4191 | * uint24 fragment_length; |
| 4192 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4193 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4194 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4195 | { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4196 | /* Make room for the additional DTLS fields */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4197 | if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 ) |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4198 | { |
| 4199 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: " |
| 4200 | "size %u, maximum %u", |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4201 | (unsigned) ( hs_len ), |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4202 | (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) ); |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4203 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4204 | } |
| 4205 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4206 | 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] | 4207 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4208 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4209 | /* Write message_seq and update it, except for HelloRequest */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4210 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4211 | { |
Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 4212 | ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; |
| 4213 | ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF; |
| 4214 | ++( ssl->handshake->out_msg_seq ); |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4215 | } |
| 4216 | else |
| 4217 | { |
| 4218 | ssl->out_msg[4] = 0; |
| 4219 | ssl->out_msg[5] = 0; |
| 4220 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4221 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4222 | /* Handshake hashes are computed without fragmentation, |
| 4223 | * 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] | 4224 | memset( ssl->out_msg + 6, 0x00, 3 ); |
| 4225 | memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4226 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4227 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4228 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 4229 | /* Update running hashes of handshake messages seen */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4230 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
| 4231 | ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4234 | /* 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] | 4235 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4236 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 4237 | ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4238 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4239 | { |
| 4240 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) |
| 4241 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4242 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4243 | return( ret ); |
| 4244 | } |
| 4245 | } |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4246 | else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4247 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4248 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4249 | 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] | 4250 | { |
| 4251 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 4252 | return( ret ); |
| 4253 | } |
| 4254 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4255 | |
| 4256 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) ); |
| 4257 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4258 | return( 0 ); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | /* |
| 4262 | * Record layer functions |
| 4263 | */ |
| 4264 | |
| 4265 | /* |
| 4266 | * Write current record. |
| 4267 | * |
| 4268 | * Uses: |
| 4269 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 4270 | * - ssl->out_msglen: length of the record content (excl headers) |
| 4271 | * - ssl->out_msg: record content |
| 4272 | */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4273 | 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] | 4274 | { |
| 4275 | int ret, done = 0; |
| 4276 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4277 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4278 | |
| 4279 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4281 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4282 | if( ssl->transform_out != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4283 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4284 | { |
| 4285 | if( ( ret = ssl_compress_buf( ssl ) ) != 0 ) |
| 4286 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4287 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4288 | return( ret ); |
| 4289 | } |
| 4290 | |
| 4291 | len = ssl->out_msglen; |
| 4292 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4293 | #endif /*MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4294 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4295 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 4296 | if( mbedtls_ssl_hw_record_write != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4297 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4298 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4300 | ret = mbedtls_ssl_hw_record_write( ssl ); |
| 4301 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4302 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4303 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret ); |
| 4304 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4305 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 4306 | |
| 4307 | if( ret == 0 ) |
| 4308 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4309 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4310 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4311 | if( !done ) |
| 4312 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4313 | unsigned i; |
| 4314 | size_t protected_record_size; |
| 4315 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4316 | /* Skip writing the record content type to after the encryption, |
| 4317 | * as it may change when using the CID extension. */ |
| 4318 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4319 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4320 | ssl->conf->transport, ssl->out_hdr + 1 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4321 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 4322 | memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4323 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 4324 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4325 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4326 | if( ssl->transform_out != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4327 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4328 | mbedtls_record rec; |
| 4329 | |
| 4330 | rec.buf = ssl->out_iv; |
| 4331 | rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - |
| 4332 | ( ssl->out_iv - ssl->out_buf ); |
| 4333 | rec.data_len = ssl->out_msglen; |
| 4334 | rec.data_offset = ssl->out_msg - rec.buf; |
| 4335 | |
| 4336 | memcpy( &rec.ctr[0], ssl->out_ctr, 8 ); |
| 4337 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
| 4338 | ssl->conf->transport, rec.ver ); |
| 4339 | rec.type = ssl->out_msgtype; |
| 4340 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4341 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 4342 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4343 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4344 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4345 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 4346 | if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4347 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4348 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4349 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4350 | return( ret ); |
| 4351 | } |
| 4352 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4353 | if( rec.data_offset != 0 ) |
| 4354 | { |
| 4355 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4356 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4357 | } |
| 4358 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4359 | /* Update the record content type and CID. */ |
| 4360 | ssl->out_msgtype = rec.type; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4361 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4362 | memcpy( ssl->out_cid, rec.cid, rec.cid_len ); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4363 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 78f839d | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 4364 | ssl->out_msglen = len = rec.data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4365 | ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 4366 | ssl->out_len[1] = (unsigned char)( rec.data_len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 4369 | protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4370 | |
| 4371 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4372 | /* In case of DTLS, double-check that we don't exceed |
| 4373 | * the remaining space in the datagram. */ |
| 4374 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4375 | { |
Hanno Becker | 554b0af | 2018-08-22 20:33:41 +0100 | [diff] [blame] | 4376 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4377 | if( ret < 0 ) |
| 4378 | return( ret ); |
| 4379 | |
| 4380 | if( protected_record_size > (size_t) ret ) |
| 4381 | { |
| 4382 | /* Should never happen */ |
| 4383 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4384 | } |
| 4385 | } |
| 4386 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4387 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4388 | /* Now write the potentially updated record content type. */ |
| 4389 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
| 4390 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4391 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, " |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4392 | "version = [%d:%d], msglen = %d", |
| 4393 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 4394 | ssl->out_hdr[2], len ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4395 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4396 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4397 | ssl->out_hdr, protected_record_size ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4398 | |
| 4399 | ssl->out_left += protected_record_size; |
| 4400 | ssl->out_hdr += protected_record_size; |
| 4401 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
| 4402 | |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 4403 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 4404 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 4405 | break; |
| 4406 | |
| 4407 | /* The loop goes to its end iff the counter is wrapping */ |
| 4408 | if( i == ssl_ep_len( ssl ) ) |
| 4409 | { |
| 4410 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); |
| 4411 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 4412 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4415 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 47db877 | 2018-08-21 13:32:13 +0100 | [diff] [blame] | 4416 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4417 | flush == SSL_DONT_FORCE_FLUSH ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4418 | { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 4419 | size_t remaining; |
| 4420 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 4421 | if( ret < 0 ) |
| 4422 | { |
| 4423 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram", |
| 4424 | ret ); |
| 4425 | return( ret ); |
| 4426 | } |
| 4427 | |
| 4428 | remaining = (size_t) ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4429 | if( remaining == 0 ) |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4430 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4431 | flush = SSL_FORCE_FLUSH; |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4432 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4433 | else |
| 4434 | { |
Hanno Becker | 513815a | 2018-08-20 11:56:09 +0100 | [diff] [blame] | 4435 | 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] | 4436 | } |
| 4437 | } |
| 4438 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4439 | |
| 4440 | if( ( flush == SSL_FORCE_FLUSH ) && |
| 4441 | ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4442 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4443 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4444 | return( ret ); |
| 4445 | } |
| 4446 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4447 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4448 | |
| 4449 | return( 0 ); |
| 4450 | } |
| 4451 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4452 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4453 | |
| 4454 | static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl ) |
| 4455 | { |
| 4456 | if( ssl->in_msglen < ssl->in_hslen || |
| 4457 | memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 || |
| 4458 | memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ) |
| 4459 | { |
| 4460 | return( 1 ); |
| 4461 | } |
| 4462 | return( 0 ); |
| 4463 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4464 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4465 | 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] | 4466 | { |
| 4467 | return( ( ssl->in_msg[9] << 16 ) | |
| 4468 | ( ssl->in_msg[10] << 8 ) | |
| 4469 | ssl->in_msg[11] ); |
| 4470 | } |
| 4471 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4472 | 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] | 4473 | { |
| 4474 | return( ( ssl->in_msg[6] << 16 ) | |
| 4475 | ( ssl->in_msg[7] << 8 ) | |
| 4476 | ssl->in_msg[8] ); |
| 4477 | } |
| 4478 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4479 | static int ssl_check_hs_header( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4480 | { |
| 4481 | uint32_t msg_len, frag_off, frag_len; |
| 4482 | |
| 4483 | msg_len = ssl_get_hs_total_len( ssl ); |
| 4484 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 4485 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 4486 | |
| 4487 | if( frag_off > msg_len ) |
| 4488 | return( -1 ); |
| 4489 | |
| 4490 | if( frag_len > msg_len - frag_off ) |
| 4491 | return( -1 ); |
| 4492 | |
| 4493 | if( frag_len + 12 > ssl->in_msglen ) |
| 4494 | return( -1 ); |
| 4495 | |
| 4496 | return( 0 ); |
| 4497 | } |
| 4498 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4499 | /* |
| 4500 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 4501 | */ |
| 4502 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) |
| 4503 | { |
| 4504 | unsigned int start_bits, end_bits; |
| 4505 | |
| 4506 | start_bits = 8 - ( offset % 8 ); |
| 4507 | if( start_bits != 8 ) |
| 4508 | { |
| 4509 | size_t first_byte_idx = offset / 8; |
| 4510 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 4511 | /* Special case */ |
| 4512 | if( len <= start_bits ) |
| 4513 | { |
| 4514 | for( ; len != 0; len-- ) |
| 4515 | mask[first_byte_idx] |= 1 << ( start_bits - len ); |
| 4516 | |
| 4517 | /* Avoid potential issues with offset or len becoming invalid */ |
| 4518 | return; |
| 4519 | } |
| 4520 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4521 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 4522 | len -= start_bits; |
| 4523 | |
| 4524 | for( ; start_bits != 0; start_bits-- ) |
| 4525 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); |
| 4526 | } |
| 4527 | |
| 4528 | end_bits = len % 8; |
| 4529 | if( end_bits != 0 ) |
| 4530 | { |
| 4531 | size_t last_byte_idx = ( offset + len ) / 8; |
| 4532 | |
| 4533 | len -= end_bits; /* Now len % 8 == 0 */ |
| 4534 | |
| 4535 | for( ; end_bits != 0; end_bits-- ) |
| 4536 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); |
| 4537 | } |
| 4538 | |
| 4539 | memset( mask + offset / 8, 0xFF, len / 8 ); |
| 4540 | } |
| 4541 | |
| 4542 | /* |
| 4543 | * Check that bitmask is full |
| 4544 | */ |
| 4545 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) |
| 4546 | { |
| 4547 | size_t i; |
| 4548 | |
| 4549 | for( i = 0; i < len / 8; i++ ) |
| 4550 | if( mask[i] != 0xFF ) |
| 4551 | return( -1 ); |
| 4552 | |
| 4553 | for( i = 0; i < len % 8; i++ ) |
| 4554 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) |
| 4555 | return( -1 ); |
| 4556 | |
| 4557 | return( 0 ); |
| 4558 | } |
| 4559 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4560 | /* msg_len does not include the handshake header */ |
Hanno Becker | 65dc885 | 2018-08-23 09:40:49 +0100 | [diff] [blame] | 4561 | static size_t ssl_get_reassembly_buffer_size( size_t msg_len, |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4562 | unsigned add_bitmap ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4563 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4564 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4565 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4566 | alloc_len = 12; /* Handshake header */ |
| 4567 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4568 | |
Hanno Becker | d07df86 | 2018-08-16 09:14:58 +0100 | [diff] [blame] | 4569 | if( add_bitmap ) |
| 4570 | alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4571 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4572 | return( alloc_len ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4573 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4574 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4575 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4576 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4577 | 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] | 4578 | { |
| 4579 | return( ( ssl->in_msg[1] << 16 ) | |
| 4580 | ( ssl->in_msg[2] << 8 ) | |
| 4581 | ssl->in_msg[3] ); |
| 4582 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4583 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4584 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4585 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4586 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4587 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4588 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d", |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4589 | ssl->in_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4590 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4591 | } |
| 4592 | |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 4593 | 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] | 4594 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4595 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4596 | " %d, type = %d, hslen = %d", |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4597 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4598 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4599 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4600 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4601 | { |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4602 | int ret; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4603 | 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] | 4604 | |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4605 | if( ssl_check_hs_header( ssl ) != 0 ) |
| 4606 | { |
| 4607 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) ); |
| 4608 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4609 | } |
| 4610 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4611 | if( ssl->handshake != NULL && |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4612 | ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4613 | recv_msg_seq != ssl->handshake->in_msg_seq ) || |
| 4614 | ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4615 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4616 | { |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 4617 | if( recv_msg_seq > ssl->handshake->in_msg_seq ) |
| 4618 | { |
| 4619 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)", |
| 4620 | recv_msg_seq, |
| 4621 | ssl->handshake->in_msg_seq ) ); |
| 4622 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4623 | } |
| 4624 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 4625 | /* Retransmit only on last message from previous flight, to avoid |
| 4626 | * too many retransmissions. |
| 4627 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
| 4628 | 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] | 4629 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4630 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4631 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4632 | "message_seq = %d, start_of_flight = %d", |
| 4633 | recv_msg_seq, |
| 4634 | ssl->handshake->in_flight_start_seq ) ); |
| 4635 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4636 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4637 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4638 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4639 | return( ret ); |
| 4640 | } |
| 4641 | } |
| 4642 | else |
| 4643 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4644 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4645 | "message_seq = %d, expected = %d", |
| 4646 | recv_msg_seq, |
| 4647 | ssl->handshake->in_msg_seq ) ); |
| 4648 | } |
| 4649 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4650 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4651 | } |
| 4652 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4653 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4654 | /* Message reassembly is handled alongside buffering of future |
| 4655 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 4656 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4657 | * handshake logic layer. */ |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4658 | if( ssl_hs_is_proper_fragment( ssl ) == 1 ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4659 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4660 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4661 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4662 | } |
| 4663 | } |
| 4664 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4665 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4666 | /* With TLS we don't handle fragmentation (for now) */ |
| 4667 | if( ssl->in_msglen < ssl->in_hslen ) |
| 4668 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4669 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); |
| 4670 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4671 | } |
| 4672 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4673 | return( 0 ); |
| 4674 | } |
| 4675 | |
| 4676 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ) |
| 4677 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4678 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4679 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4680 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL ) |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 4681 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4682 | 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] | 4683 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4684 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4685 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4686 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4687 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4688 | ssl->handshake != NULL ) |
| 4689 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4690 | unsigned offset; |
| 4691 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4692 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4693 | /* Increment handshake sequence number */ |
| 4694 | hs->in_msg_seq++; |
| 4695 | |
| 4696 | /* |
| 4697 | * Clear up handshake buffering and reassembly structure. |
| 4698 | */ |
| 4699 | |
| 4700 | /* Free first entry */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4701 | ssl_buffering_free_slot( ssl, 0 ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4702 | |
| 4703 | /* Shift all other entries */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4704 | for( offset = 0, hs_buf = &hs->buffering.hs[0]; |
| 4705 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4706 | offset++, hs_buf++ ) |
| 4707 | { |
| 4708 | *hs_buf = *(hs_buf + 1); |
| 4709 | } |
| 4710 | |
| 4711 | /* Create a fresh last entry */ |
| 4712 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4713 | } |
| 4714 | #endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4715 | } |
| 4716 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4717 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4718 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 4719 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4720 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 4721 | * Bit n is set iff record number in_window_top - n has been seen. |
| 4722 | * |
| 4723 | * Usually, in_window_top is the last record number seen and the lsb of |
| 4724 | * in_window is set. The only exception is the initial state (record number 0 |
| 4725 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4726 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4727 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4728 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4729 | { |
| 4730 | ssl->in_window_top = 0; |
| 4731 | ssl->in_window = 0; |
| 4732 | } |
| 4733 | |
| 4734 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) |
| 4735 | { |
| 4736 | return( ( (uint64_t) buf[0] << 40 ) | |
| 4737 | ( (uint64_t) buf[1] << 32 ) | |
| 4738 | ( (uint64_t) buf[2] << 24 ) | |
| 4739 | ( (uint64_t) buf[3] << 16 ) | |
| 4740 | ( (uint64_t) buf[4] << 8 ) | |
| 4741 | ( (uint64_t) buf[5] ) ); |
| 4742 | } |
| 4743 | |
| 4744 | /* |
| 4745 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 4746 | */ |
Hanno Becker | 0183d69 | 2019-07-12 08:50:37 +0100 | [diff] [blame] | 4747 | 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] | 4748 | { |
| 4749 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4750 | uint64_t bit; |
| 4751 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4752 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4753 | return( 0 ); |
| 4754 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4755 | if( rec_seqnum > ssl->in_window_top ) |
| 4756 | return( 0 ); |
| 4757 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4758 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4759 | |
| 4760 | if( bit >= 64 ) |
| 4761 | return( -1 ); |
| 4762 | |
| 4763 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) |
| 4764 | return( -1 ); |
| 4765 | |
| 4766 | return( 0 ); |
| 4767 | } |
| 4768 | |
| 4769 | /* |
| 4770 | * Update replay window on new validated record |
| 4771 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4772 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4773 | { |
| 4774 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4775 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4776 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4777 | return; |
| 4778 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4779 | if( rec_seqnum > ssl->in_window_top ) |
| 4780 | { |
| 4781 | /* Update window_top and the contents of the window */ |
| 4782 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 4783 | |
| 4784 | if( shift >= 64 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4785 | ssl->in_window = 1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4786 | else |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4787 | { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4788 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4789 | ssl->in_window |= 1; |
| 4790 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4791 | |
| 4792 | ssl->in_window_top = rec_seqnum; |
| 4793 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4794 | else |
| 4795 | { |
| 4796 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4797 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4798 | |
| 4799 | if( bit < 64 ) /* Always true, but be extra sure */ |
| 4800 | ssl->in_window |= (uint64_t) 1 << bit; |
| 4801 | } |
| 4802 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4803 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4804 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4805 | #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] | 4806 | /* Forward declaration */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4807 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ); |
| 4808 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4809 | /* |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4810 | * Without any SSL context, check if a datagram looks like a ClientHello with |
| 4811 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 4812 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4813 | * |
| 4814 | * - if cookie is valid, return 0 |
| 4815 | * - if ClientHello looks superficially valid but cookie is not, |
| 4816 | * fill obuf and set olen, then |
| 4817 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 4818 | * - otherwise return a specific error code |
| 4819 | */ |
| 4820 | static int ssl_check_dtls_clihlo_cookie( |
| 4821 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 4822 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
| 4823 | void *p_cookie, |
| 4824 | const unsigned char *cli_id, size_t cli_id_len, |
| 4825 | const unsigned char *in, size_t in_len, |
| 4826 | unsigned char *obuf, size_t buf_len, size_t *olen ) |
| 4827 | { |
| 4828 | size_t sid_len, cookie_len; |
| 4829 | unsigned char *p; |
| 4830 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4831 | /* |
| 4832 | * Structure of ClientHello with record and handshake headers, |
| 4833 | * and expected values. We don't need to check a lot, more checks will be |
| 4834 | * done when actually parsing the ClientHello - skipping those checks |
| 4835 | * avoids code duplication and does not make cookie forging any easier. |
| 4836 | * |
| 4837 | * 0-0 ContentType type; copied, must be handshake |
| 4838 | * 1-2 ProtocolVersion version; copied |
| 4839 | * 3-4 uint16 epoch; copied, must be 0 |
| 4840 | * 5-10 uint48 sequence_number; copied |
| 4841 | * 11-12 uint16 length; (ignored) |
| 4842 | * |
| 4843 | * 13-13 HandshakeType msg_type; (ignored) |
| 4844 | * 14-16 uint24 length; (ignored) |
| 4845 | * 17-18 uint16 message_seq; copied |
| 4846 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 4847 | * 22-24 uint24 fragment_length; (ignored) |
| 4848 | * |
| 4849 | * 25-26 ProtocolVersion client_version; (ignored) |
| 4850 | * 27-58 Random random; (ignored) |
| 4851 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 4852 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 4853 | * ... |
| 4854 | * |
| 4855 | * Minimum length is 61 bytes. |
| 4856 | */ |
| 4857 | if( in_len < 61 || |
| 4858 | in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4859 | in[3] != 0 || in[4] != 0 || |
| 4860 | in[19] != 0 || in[20] != 0 || in[21] != 0 ) |
| 4861 | { |
| 4862 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4863 | } |
| 4864 | |
| 4865 | sid_len = in[59]; |
| 4866 | if( sid_len > in_len - 61 ) |
| 4867 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4868 | |
| 4869 | cookie_len = in[60 + sid_len]; |
| 4870 | if( cookie_len > in_len - 60 ) |
| 4871 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4872 | |
| 4873 | if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len, |
| 4874 | cli_id, cli_id_len ) == 0 ) |
| 4875 | { |
| 4876 | /* Valid cookie */ |
| 4877 | return( 0 ); |
| 4878 | } |
| 4879 | |
| 4880 | /* |
| 4881 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 4882 | * |
| 4883 | * 0-0 ContentType type; copied |
| 4884 | * 1-2 ProtocolVersion version; copied |
| 4885 | * 3-4 uint16 epoch; copied |
| 4886 | * 5-10 uint48 sequence_number; copied |
| 4887 | * 11-12 uint16 length; olen - 13 |
| 4888 | * |
| 4889 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 4890 | * 14-16 uint24 length; olen - 25 |
| 4891 | * 17-18 uint16 message_seq; copied |
| 4892 | * 19-21 uint24 fragment_offset; copied |
| 4893 | * 22-24 uint24 fragment_length; olen - 25 |
| 4894 | * |
| 4895 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 4896 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 4897 | * |
| 4898 | * Minimum length is 28. |
| 4899 | */ |
| 4900 | if( buf_len < 28 ) |
| 4901 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4902 | |
| 4903 | /* Copy most fields and adapt others */ |
| 4904 | memcpy( obuf, in, 25 ); |
| 4905 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 4906 | obuf[25] = 0xfe; |
| 4907 | obuf[26] = 0xff; |
| 4908 | |
| 4909 | /* Generate and write actual cookie */ |
| 4910 | p = obuf + 28; |
| 4911 | if( f_cookie_write( p_cookie, |
| 4912 | &p, obuf + buf_len, cli_id, cli_id_len ) != 0 ) |
| 4913 | { |
| 4914 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4915 | } |
| 4916 | |
| 4917 | *olen = p - obuf; |
| 4918 | |
| 4919 | /* Go back and fill length fields */ |
| 4920 | obuf[27] = (unsigned char)( *olen - 28 ); |
| 4921 | |
| 4922 | obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 ); |
| 4923 | obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 ); |
| 4924 | obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) ); |
| 4925 | |
| 4926 | obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 ); |
| 4927 | obuf[12] = (unsigned char)( ( *olen - 13 ) ); |
| 4928 | |
| 4929 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
| 4930 | } |
| 4931 | |
| 4932 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4933 | * Handle possible client reconnect with the same UDP quadruplet |
| 4934 | * (RFC 6347 Section 4.2.8). |
| 4935 | * |
| 4936 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 4937 | * that looks like a ClientHello. |
| 4938 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4939 | * - if the input looks like a ClientHello without cookies, |
| 4940 | * send back HelloVerifyRequest, then |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4941 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4942 | * - if the input looks like a ClientHello with a valid cookie, |
| 4943 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 4944 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4945 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4946 | * |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4947 | * mbedtls_ssl_read_record() will ignore the record if anything else than |
Simon Butcher | d0bf6a3 | 2015-09-11 17:34:49 +0100 | [diff] [blame] | 4948 | * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function |
| 4949 | * cannot not return 0. |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4950 | */ |
| 4951 | static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) |
| 4952 | { |
| 4953 | int ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4954 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4955 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4956 | if( ssl->conf->f_cookie_write == NULL || |
| 4957 | ssl->conf->f_cookie_check == NULL ) |
| 4958 | { |
| 4959 | /* If we can't use cookies to verify reachability of the peer, |
| 4960 | * drop the record. */ |
| 4961 | return( 0 ); |
| 4962 | } |
| 4963 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4964 | ret = ssl_check_dtls_clihlo_cookie( |
| 4965 | ssl->conf->f_cookie_write, |
| 4966 | ssl->conf->f_cookie_check, |
| 4967 | ssl->conf->p_cookie, |
| 4968 | ssl->cli_id, ssl->cli_id_len, |
| 4969 | ssl->in_buf, ssl->in_left, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4970 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4971 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4972 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret ); |
| 4973 | |
| 4974 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4975 | { |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4976 | /* 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] | 4977 | * If the error is permanent we'll catch it later, |
| 4978 | * if it's not, then hopefully it'll work next time. */ |
| 4979 | (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len ); |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4980 | ret = 0; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4981 | } |
| 4982 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4983 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4984 | { |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4985 | /* Got a valid cookie, partially reset context */ |
| 4986 | if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 ) |
| 4987 | { |
| 4988 | MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret ); |
| 4989 | return( ret ); |
| 4990 | } |
| 4991 | |
| 4992 | return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4993 | } |
| 4994 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4995 | return( ret ); |
| 4996 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4997 | #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] | 4998 | |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 4999 | static int ssl_check_record_type( uint8_t record_type ) |
| 5000 | { |
| 5001 | if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 5002 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 5003 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
| 5004 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 5005 | { |
| 5006 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5007 | } |
| 5008 | |
| 5009 | return( 0 ); |
| 5010 | } |
| 5011 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5012 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5013 | * ContentType type; |
| 5014 | * ProtocolVersion version; |
| 5015 | * uint16 epoch; // DTLS only |
| 5016 | * uint48 sequence_number; // DTLS only |
| 5017 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5018 | * |
| 5019 | * 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] | 5020 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5021 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 5022 | * |
| 5023 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 5024 | * 1. proceed with the record if this function returns 0 |
| 5025 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 5026 | * 3. return CLIENT_RECONNECT if this function return that value |
| 5027 | * 4. drop the whole datagram if this function returns anything else. |
| 5028 | * Point 2 is needed when the peer is resending, and we have already received |
| 5029 | * 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] | 5030 | */ |
Hanno Becker | 331de3d | 2019-07-12 11:10:16 +0100 | [diff] [blame] | 5031 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5032 | unsigned char *buf, |
| 5033 | size_t len, |
| 5034 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5035 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5036 | int major_ver, minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5037 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5038 | size_t const rec_hdr_type_offset = 0; |
| 5039 | size_t const rec_hdr_type_len = 1; |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 5040 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5041 | size_t const rec_hdr_version_offset = rec_hdr_type_offset + |
| 5042 | rec_hdr_type_len; |
| 5043 | size_t const rec_hdr_version_len = 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5044 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5045 | size_t const rec_hdr_ctr_len = 8; |
| 5046 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 5047 | uint32_t rec_epoch; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5048 | size_t const rec_hdr_ctr_offset = rec_hdr_version_offset + |
| 5049 | rec_hdr_version_len; |
| 5050 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5051 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5052 | size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset + |
| 5053 | rec_hdr_ctr_len; |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 5054 | size_t rec_hdr_cid_len = 0; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5055 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5056 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5057 | |
| 5058 | size_t rec_hdr_len_offset; /* To be determined */ |
| 5059 | size_t const rec_hdr_len_len = 2; |
| 5060 | |
| 5061 | /* |
| 5062 | * Check minimum lengths for record header. |
| 5063 | */ |
| 5064 | |
| 5065 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5066 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5067 | { |
| 5068 | rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len; |
| 5069 | } |
| 5070 | else |
| 5071 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5072 | { |
| 5073 | rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len; |
| 5074 | } |
| 5075 | |
| 5076 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) |
| 5077 | { |
| 5078 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u", |
| 5079 | (unsigned) len, |
| 5080 | (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) ); |
| 5081 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5082 | } |
| 5083 | |
| 5084 | /* |
| 5085 | * Parse and validate record content type |
| 5086 | */ |
| 5087 | |
| 5088 | rec->type = buf[ rec_hdr_type_offset ]; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5089 | |
| 5090 | /* Check record content type */ |
| 5091 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5092 | rec->cid_len = 0; |
| 5093 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5094 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5095 | ssl->conf->cid_len != 0 && |
| 5096 | rec->type == MBEDTLS_SSL_MSG_CID ) |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5097 | { |
| 5098 | /* Shift pointers to account for record header including CID |
| 5099 | * struct { |
| 5100 | * ContentType special_type = tls12_cid; |
| 5101 | * ProtocolVersion version; |
| 5102 | * uint16 epoch; |
| 5103 | * uint48 sequence_number; |
Hanno Becker | 8e55b0f | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 5104 | * opaque cid[cid_length]; // Additional field compared to |
| 5105 | * // default DTLS record format |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5106 | * uint16 length; |
| 5107 | * opaque enc_content[DTLSCiphertext.length]; |
| 5108 | * } DTLSCiphertext; |
| 5109 | */ |
| 5110 | |
| 5111 | /* So far, we only support static CID lengths |
| 5112 | * fixed in the configuration. */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5113 | rec_hdr_cid_len = ssl->conf->cid_len; |
| 5114 | rec_hdr_len_offset += rec_hdr_cid_len; |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5115 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5116 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5117 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5118 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u", |
| 5119 | (unsigned) len, |
| 5120 | (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) ); |
Hanno Becker | 59be60e | 2019-07-10 14:53:43 +0100 | [diff] [blame] | 5121 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5122 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5123 | |
Manuel Pégourié-Gonnard | 7e821b5 | 2019-08-02 10:17:15 +0200 | [diff] [blame] | 5124 | /* configured CID len is guaranteed at most 255, see |
| 5125 | * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */ |
| 5126 | rec->cid_len = (uint8_t) rec_hdr_cid_len; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5127 | memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len ); |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5128 | } |
| 5129 | else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5130 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 5131 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5132 | if( ssl_check_record_type( rec->type ) ) |
| 5133 | { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 5134 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u", |
| 5135 | (unsigned) rec->type ) ); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5136 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5137 | } |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 5138 | } |
| 5139 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5140 | /* |
| 5141 | * Parse and validate record version |
| 5142 | */ |
| 5143 | |
Hanno Becker | d0b66d0 | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 5144 | rec->ver[0] = buf[ rec_hdr_version_offset + 0 ]; |
| 5145 | rec->ver[1] = buf[ rec_hdr_version_offset + 1 ]; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5146 | mbedtls_ssl_read_version( &major_ver, &minor_ver, |
| 5147 | ssl->conf->transport, |
Hanno Becker | d0b66d0 | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 5148 | &rec->ver[0] ); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5149 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5150 | if( major_ver != ssl->major_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5151 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5152 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); |
| 5153 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5156 | if( minor_ver > ssl->conf->max_minor_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5157 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5158 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); |
| 5159 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5160 | } |
| 5161 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5162 | /* |
| 5163 | * Parse/Copy record sequence number. |
| 5164 | */ |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5165 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5166 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5167 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 5168 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5169 | /* Copy explicit record sequence number from input buffer. */ |
| 5170 | memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset, |
| 5171 | rec_hdr_ctr_len ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 5172 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5173 | else |
| 5174 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5175 | { |
| 5176 | /* Copy implicit record sequence number from SSL context structure. */ |
| 5177 | memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len ); |
| 5178 | } |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 5179 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5180 | /* |
| 5181 | * Parse record length. |
| 5182 | */ |
| 5183 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5184 | rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len; |
Hanno Becker | 9eca276 | 2019-07-25 10:16:37 +0100 | [diff] [blame] | 5185 | rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) | |
| 5186 | ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 ); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5187 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5188 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5189 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, " |
Hanno Becker | 92d30f5 | 2019-05-23 17:03:44 +0100 | [diff] [blame] | 5190 | "version = [%d:%d], msglen = %d", |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5191 | rec->type, |
| 5192 | major_ver, minor_ver, rec->data_len ) ); |
| 5193 | |
| 5194 | rec->buf = buf; |
| 5195 | rec->buf_len = rec->data_offset + rec->data_len; |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5196 | |
Hanno Becker | d417cc9 | 2019-07-26 08:20:27 +0100 | [diff] [blame] | 5197 | if( rec->data_len == 0 ) |
| 5198 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5199 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5200 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 5201 | * DTLS-related tests. |
| 5202 | * Check epoch before checking length constraint because |
| 5203 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 5204 | * message gets duplicated before the corresponding Finished message, |
| 5205 | * the second ChangeCipherSpec should be discarded because it belongs |
| 5206 | * to an old epoch, but not because its length is shorter than |
| 5207 | * the minimum record length for packets using the new record transform. |
| 5208 | * Note that these two kinds of failures are handled differently, |
| 5209 | * as an unexpected record is silently skipped but an invalid |
| 5210 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5211 | */ |
| 5212 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5213 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5214 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5215 | rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1]; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5216 | |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5217 | /* Check that the datagram is large enough to contain a record |
| 5218 | * of the advertised length. */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5219 | if( len < rec->data_offset + rec->data_len ) |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5220 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5221 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.", |
| 5222 | (unsigned) len, |
| 5223 | (unsigned)( rec->data_offset + rec->data_len ) ) ); |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5224 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5225 | } |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5226 | |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5227 | /* Records from other, non-matching epochs are silently discarded. |
| 5228 | * (The case of same-port Client reconnects must be considered in |
| 5229 | * the caller). */ |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5230 | if( rec_epoch != ssl->in_epoch ) |
| 5231 | { |
| 5232 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " |
| 5233 | "expected %d, received %d", |
| 5234 | ssl->in_epoch, rec_epoch ) ); |
| 5235 | |
Hanno Becker | 552f747 | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 5236 | /* Records from the next epoch are considered for buffering |
| 5237 | * (concretely: early Finished messages). */ |
| 5238 | if( rec_epoch == (unsigned) ssl->in_epoch + 1 ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5239 | { |
Hanno Becker | 552f747 | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 5240 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) ); |
| 5241 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5242 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5243 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5244 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5245 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5246 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5247 | /* For records from the correct epoch, check whether their |
| 5248 | * sequence number has been seen before. */ |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5249 | else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5250 | { |
| 5251 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); |
| 5252 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 5253 | } |
| 5254 | #endif |
| 5255 | } |
| 5256 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5257 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5258 | return( 0 ); |
| 5259 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5260 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5261 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5262 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
| 5263 | static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl ) |
| 5264 | { |
| 5265 | unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1]; |
| 5266 | |
| 5267 | /* |
| 5268 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 5269 | * access the first byte of record content (handshake type), as we |
| 5270 | * have an active transform (possibly iv_len != 0), so use the |
| 5271 | * fact that the record header len is 13 instead. |
| 5272 | */ |
| 5273 | if( rec_epoch == 0 && |
| 5274 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5275 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 5276 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 5277 | ssl->in_left > 13 && |
| 5278 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO ) |
| 5279 | { |
| 5280 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect " |
| 5281 | "from the same port" ) ); |
| 5282 | return( ssl_handle_possible_reconnect( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
| 5285 | return( 0 ); |
| 5286 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5287 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5288 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5289 | /* |
| 5290 | * If applicable, decrypt (and decompress) record content |
| 5291 | */ |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5292 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl, |
| 5293 | mbedtls_record *rec ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5294 | { |
| 5295 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 5296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5297 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5298 | rec->buf, rec->buf_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5300 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 5301 | if( mbedtls_ssl_hw_record_read != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5302 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5303 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5304 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5305 | ret = mbedtls_ssl_hw_record_read( ssl ); |
| 5306 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5307 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5308 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret ); |
| 5309 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5310 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 5311 | |
| 5312 | if( ret == 0 ) |
| 5313 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5314 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5315 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5316 | if( !done && ssl->transform_in != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5317 | { |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5318 | unsigned char const old_msg_type = rec->type; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5319 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 5320 | if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5321 | rec ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5322 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5323 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5324 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5325 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5326 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
| 5327 | ssl->conf->ignore_unexpected_cid |
| 5328 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 5329 | { |
Hanno Becker | e8d6afd | 2019-05-24 10:11:06 +0100 | [diff] [blame] | 5330 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) ); |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5331 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5332 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5333 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5334 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5335 | return( ret ); |
| 5336 | } |
| 5337 | |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5338 | if( old_msg_type != rec->type ) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5339 | { |
| 5340 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d", |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5341 | old_msg_type, rec->type ) ); |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5342 | } |
| 5343 | |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 5344 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5345 | rec->buf + rec->data_offset, rec->data_len ); |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 5346 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5347 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5348 | /* We have already checked the record content type |
| 5349 | * in ssl_parse_record_header(), failing or silently |
| 5350 | * dropping the record in the case of an unknown type. |
| 5351 | * |
| 5352 | * Since with the use of CIDs, the record content type |
| 5353 | * might change during decryption, re-check the record |
| 5354 | * content type, but treat a failure as fatal this time. */ |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5355 | if( ssl_check_record_type( rec->type ) ) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5356 | { |
| 5357 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
| 5358 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5359 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5360 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5361 | |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5362 | if( rec->data_len == 0 ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5363 | { |
| 5364 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5365 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5366 | && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5367 | { |
| 5368 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ |
| 5369 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) ); |
| 5370 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5371 | } |
| 5372 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5373 | |
| 5374 | ssl->nb_zero++; |
| 5375 | |
| 5376 | /* |
| 5377 | * Three or more empty messages may be a DoS attack |
| 5378 | * (excessive CPU consumption). |
| 5379 | */ |
| 5380 | if( ssl->nb_zero > 3 ) |
| 5381 | { |
| 5382 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " |
Hanno Becker | 6e7700d | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 5383 | "messages, possible DoS attack" ) ); |
| 5384 | /* Treat the records as if they were not properly authenticated, |
| 5385 | * thereby failing the connection if we see more than allowed |
| 5386 | * by the configured bad MAC threshold. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5387 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
| 5388 | } |
| 5389 | } |
| 5390 | else |
| 5391 | ssl->nb_zero = 0; |
| 5392 | |
| 5393 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5394 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5395 | { |
| 5396 | ; /* in_ctr read from peer, not maintained internally */ |
| 5397 | } |
| 5398 | else |
| 5399 | #endif |
| 5400 | { |
| 5401 | unsigned i; |
| 5402 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 5403 | if( ++ssl->in_ctr[i - 1] != 0 ) |
| 5404 | break; |
| 5405 | |
| 5406 | /* The loop goes to its end iff the counter is wrapping */ |
| 5407 | if( i == ssl_ep_len( ssl ) ) |
| 5408 | { |
| 5409 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); |
| 5410 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 5411 | } |
| 5412 | } |
| 5413 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5414 | } |
| 5415 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5416 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5417 | if( ssl->transform_in != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5418 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5419 | { |
| 5420 | if( ( ret = ssl_decompress_buf( ssl ) ) != 0 ) |
| 5421 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5422 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5423 | return( ret ); |
| 5424 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5425 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5426 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5427 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5428 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5429 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5430 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5431 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5432 | } |
| 5433 | #endif |
| 5434 | |
Hanno Becker | d96e10b | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 5435 | /* Check actual (decrypted) record content length against |
| 5436 | * configured maximum. */ |
| 5437 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5438 | { |
| 5439 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 5440 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5441 | } |
| 5442 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5443 | return( 0 ); |
| 5444 | } |
| 5445 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5446 | 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] | 5447 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5448 | /* |
| 5449 | * Read a record. |
| 5450 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5451 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 5452 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 5453 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5454 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5455 | |
| 5456 | /* Helper functions for mbedtls_ssl_read_record(). */ |
| 5457 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5458 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ); |
| 5459 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 5460 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5461 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl, |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5462 | unsigned update_hs_digest ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5463 | { |
| 5464 | int ret; |
| 5465 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5466 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5467 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5468 | if( ssl->keep_current_message == 0 ) |
| 5469 | { |
| 5470 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5471 | |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5472 | ret = ssl_consume_current_message( ssl ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5473 | if( ret != 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5474 | return( ret ); |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5475 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5476 | if( ssl_record_is_in_progress( ssl ) == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5477 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5478 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5479 | int have_buffered = 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5480 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5481 | /* We only check for buffered messages if the |
| 5482 | * current datagram is fully consumed. */ |
| 5483 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5484 | ssl_next_record_is_in_datagram( ssl ) == 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5485 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5486 | if( ssl_load_buffered_message( ssl ) == 0 ) |
| 5487 | have_buffered = 1; |
| 5488 | } |
| 5489 | |
| 5490 | if( have_buffered == 0 ) |
| 5491 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5492 | { |
| 5493 | ret = ssl_get_next_record( ssl ); |
| 5494 | if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ) |
| 5495 | continue; |
| 5496 | |
| 5497 | if( ret != 0 ) |
| 5498 | { |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 5499 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5500 | return( ret ); |
| 5501 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5502 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5503 | } |
| 5504 | |
| 5505 | ret = mbedtls_ssl_handle_message_type( ssl ); |
| 5506 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5507 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5508 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 5509 | { |
| 5510 | /* Buffer future message */ |
| 5511 | ret = ssl_buffer_message( ssl ); |
| 5512 | if( ret != 0 ) |
| 5513 | return( ret ); |
| 5514 | |
| 5515 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 5516 | } |
| 5517 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5518 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5519 | } while( MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 5520 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5521 | |
| 5522 | if( 0 != ret ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5523 | { |
Hanno Becker | 05c4fc8 | 2017-11-09 14:34:06 +0000 | [diff] [blame] | 5524 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5525 | return( ret ); |
| 5526 | } |
| 5527 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5528 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5529 | update_hs_digest == 1 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5530 | { |
| 5531 | mbedtls_ssl_update_handshake_status( ssl ); |
| 5532 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5533 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5534 | else |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5535 | { |
Hanno Becker | 02f5907 | 2018-08-15 14:00:24 +0100 | [diff] [blame] | 5536 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5537 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); |
| 5541 | |
| 5542 | return( 0 ); |
| 5543 | } |
| 5544 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5545 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5546 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5547 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5548 | if( ssl->in_left > ssl->next_record_offset ) |
| 5549 | return( 1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5550 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5551 | return( 0 ); |
| 5552 | } |
| 5553 | |
| 5554 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ) |
| 5555 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5556 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5557 | mbedtls_ssl_hs_buffer * hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5558 | int ret = 0; |
| 5559 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5560 | if( hs == NULL ) |
| 5561 | return( -1 ); |
| 5562 | |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 5563 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) ); |
| 5564 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5565 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 5566 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 5567 | { |
| 5568 | /* Check if we have seen a ChangeCipherSpec before. |
| 5569 | * If yes, synthesize a CCS record. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5570 | if( !hs->buffering.seen_ccs ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5571 | { |
| 5572 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) ); |
| 5573 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 5574 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5575 | } |
| 5576 | |
Hanno Becker | 39b8bc9 | 2018-08-28 17:17:13 +0100 | [diff] [blame] | 5577 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5578 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 5579 | ssl->in_msglen = 1; |
| 5580 | ssl->in_msg[0] = 1; |
| 5581 | |
| 5582 | /* As long as they are equal, the exact value doesn't matter. */ |
| 5583 | ssl->in_left = 0; |
| 5584 | ssl->next_record_offset = 0; |
| 5585 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5586 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5587 | goto exit; |
| 5588 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5589 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5590 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5591 | /* Debug only */ |
| 5592 | { |
| 5593 | unsigned offset; |
| 5594 | for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
| 5595 | { |
| 5596 | hs_buf = &hs->buffering.hs[offset]; |
| 5597 | if( hs_buf->is_valid == 1 ) |
| 5598 | { |
| 5599 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.", |
| 5600 | hs->in_msg_seq + offset, |
Hanno Becker | a591c48 | 2018-08-28 17:20:00 +0100 | [diff] [blame] | 5601 | hs_buf->is_complete ? "fully" : "partially" ) ); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5602 | } |
| 5603 | } |
| 5604 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5605 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5606 | |
| 5607 | /* Check if we have buffered and/or fully reassembled the |
| 5608 | * next handshake message. */ |
| 5609 | hs_buf = &hs->buffering.hs[0]; |
| 5610 | if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) ) |
| 5611 | { |
| 5612 | /* Synthesize a record containing the buffered HS message. */ |
| 5613 | size_t msg_len = ( hs_buf->data[1] << 16 ) | |
| 5614 | ( hs_buf->data[2] << 8 ) | |
| 5615 | hs_buf->data[3]; |
| 5616 | |
| 5617 | /* Double-check that we haven't accidentally buffered |
| 5618 | * a message that doesn't fit into the input buffer. */ |
| 5619 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5620 | { |
| 5621 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5622 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5623 | } |
| 5624 | |
| 5625 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) ); |
| 5626 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)", |
| 5627 | hs_buf->data, msg_len + 12 ); |
| 5628 | |
| 5629 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5630 | ssl->in_hslen = msg_len + 12; |
| 5631 | ssl->in_msglen = msg_len + 12; |
| 5632 | memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen ); |
| 5633 | |
| 5634 | ret = 0; |
| 5635 | goto exit; |
| 5636 | } |
| 5637 | else |
| 5638 | { |
| 5639 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered", |
| 5640 | hs->in_msg_seq ) ); |
| 5641 | } |
| 5642 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5643 | ret = -1; |
| 5644 | |
| 5645 | exit: |
| 5646 | |
| 5647 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) ); |
| 5648 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5649 | } |
| 5650 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5651 | static int ssl_buffer_make_space( mbedtls_ssl_context *ssl, |
| 5652 | size_t desired ) |
| 5653 | { |
| 5654 | int offset; |
| 5655 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5656 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available", |
| 5657 | (unsigned) desired ) ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5658 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5659 | /* Get rid of future records epoch first, if such exist. */ |
| 5660 | ssl_free_buffered_record( ssl ); |
| 5661 | |
| 5662 | /* Check if we have enough space available now. */ |
| 5663 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5664 | hs->buffering.total_bytes_buffered ) ) |
| 5665 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5666 | 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] | 5667 | return( 0 ); |
| 5668 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5669 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 5670 | /* We don't have enough space to buffer the next expected handshake |
| 5671 | * message. Remove buffers used for future messages to gain space, |
| 5672 | * starting with the most distant one. */ |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5673 | for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 5674 | offset >= 0; offset-- ) |
| 5675 | { |
| 5676 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message", |
| 5677 | offset ) ); |
| 5678 | |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5679 | ssl_buffering_free_slot( ssl, (uint8_t) offset ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5680 | |
| 5681 | /* Check if we have enough space available now. */ |
| 5682 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5683 | hs->buffering.total_bytes_buffered ) ) |
| 5684 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5685 | 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] | 5686 | return( 0 ); |
| 5687 | } |
| 5688 | } |
| 5689 | |
| 5690 | return( -1 ); |
| 5691 | } |
| 5692 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5693 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ) |
| 5694 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5695 | int ret = 0; |
| 5696 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5697 | |
| 5698 | if( hs == NULL ) |
| 5699 | return( 0 ); |
| 5700 | |
| 5701 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) ); |
| 5702 | |
| 5703 | switch( ssl->in_msgtype ) |
| 5704 | { |
| 5705 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
| 5706 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) ); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5707 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5708 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5709 | break; |
| 5710 | |
| 5711 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5712 | { |
| 5713 | unsigned recv_msg_seq_offset; |
| 5714 | unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
| 5715 | mbedtls_ssl_hs_buffer *hs_buf; |
| 5716 | size_t msg_len = ssl->in_hslen - 12; |
| 5717 | |
| 5718 | /* We should never receive an old handshake |
| 5719 | * message - double-check nonetheless. */ |
| 5720 | if( recv_msg_seq < ssl->handshake->in_msg_seq ) |
| 5721 | { |
| 5722 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5723 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5724 | } |
| 5725 | |
| 5726 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
| 5727 | if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 5728 | { |
| 5729 | /* Silently ignore -- message too far in the future */ |
| 5730 | MBEDTLS_SSL_DEBUG_MSG( 2, |
| 5731 | ( "Ignore future HS message with sequence number %u, " |
| 5732 | "buffering window %u - %u", |
| 5733 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 5734 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) ); |
| 5735 | |
| 5736 | goto exit; |
| 5737 | } |
| 5738 | |
| 5739 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ", |
| 5740 | recv_msg_seq, recv_msg_seq_offset ) ); |
| 5741 | |
| 5742 | hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ]; |
| 5743 | |
| 5744 | /* Check if the buffering for this seq nr has already commenced. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5745 | if( !hs_buf->is_valid ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5746 | { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5747 | size_t reassembly_buf_sz; |
| 5748 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5749 | hs_buf->is_fragmented = |
| 5750 | ( ssl_hs_is_proper_fragment( ssl ) == 1 ); |
| 5751 | |
| 5752 | /* We copy the message back into the input buffer |
| 5753 | * after reassembly, so check that it's not too large. |
| 5754 | * This is an implementation-specific limitation |
| 5755 | * and not one from the standard, hence it is not |
| 5756 | * checked in ssl_check_hs_header(). */ |
Hanno Becker | 96a6c69 | 2018-08-21 15:56:03 +0100 | [diff] [blame] | 5757 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5758 | { |
| 5759 | /* Ignore message */ |
| 5760 | goto exit; |
| 5761 | } |
| 5762 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5763 | /* Check if we have enough space to buffer the message. */ |
| 5764 | if( hs->buffering.total_bytes_buffered > |
| 5765 | MBEDTLS_SSL_DTLS_MAX_BUFFERING ) |
| 5766 | { |
| 5767 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5768 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5769 | } |
| 5770 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5771 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len, |
| 5772 | hs_buf->is_fragmented ); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5773 | |
| 5774 | if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5775 | hs->buffering.total_bytes_buffered ) ) |
| 5776 | { |
| 5777 | if( recv_msg_seq_offset > 0 ) |
| 5778 | { |
| 5779 | /* If we can't buffer a future message because |
| 5780 | * of space limitations -- ignore. */ |
| 5781 | 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", |
| 5782 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5783 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5784 | goto exit; |
| 5785 | } |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 5786 | else |
| 5787 | { |
| 5788 | 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", |
| 5789 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5790 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5791 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5792 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5793 | if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 ) |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5794 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5795 | 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", |
| 5796 | (unsigned) msg_len, |
| 5797 | (unsigned) reassembly_buf_sz, |
| 5798 | MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5799 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5800 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 5801 | goto exit; |
| 5802 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5803 | } |
| 5804 | |
| 5805 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d", |
| 5806 | msg_len ) ); |
| 5807 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5808 | hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz ); |
| 5809 | if( hs_buf->data == NULL ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5810 | { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5811 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5812 | goto exit; |
| 5813 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5814 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5815 | |
| 5816 | /* Prepare final header: copy msg_type, length and message_seq, |
| 5817 | * then add standardised fragment_offset and fragment_length */ |
| 5818 | memcpy( hs_buf->data, ssl->in_msg, 6 ); |
| 5819 | memset( hs_buf->data + 6, 0, 3 ); |
| 5820 | memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 ); |
| 5821 | |
| 5822 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5823 | |
| 5824 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5825 | } |
| 5826 | else |
| 5827 | { |
| 5828 | /* Make sure msg_type and length are consistent */ |
| 5829 | if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) |
| 5830 | { |
| 5831 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) ); |
| 5832 | /* Ignore */ |
| 5833 | goto exit; |
| 5834 | } |
| 5835 | } |
| 5836 | |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5837 | if( !hs_buf->is_complete ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5838 | { |
| 5839 | size_t frag_len, frag_off; |
| 5840 | unsigned char * const msg = hs_buf->data + 12; |
| 5841 | |
| 5842 | /* |
| 5843 | * Check and copy current fragment |
| 5844 | */ |
| 5845 | |
| 5846 | /* Validation of header fields already done in |
| 5847 | * mbedtls_ssl_prepare_handshake_record(). */ |
| 5848 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 5849 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 5850 | |
| 5851 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d", |
| 5852 | frag_off, frag_len ) ); |
| 5853 | memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); |
| 5854 | |
| 5855 | if( hs_buf->is_fragmented ) |
| 5856 | { |
| 5857 | unsigned char * const bitmask = msg + msg_len; |
| 5858 | ssl_bitmask_set( bitmask, frag_off, frag_len ); |
| 5859 | hs_buf->is_complete = ( ssl_bitmask_check( bitmask, |
| 5860 | msg_len ) == 0 ); |
| 5861 | } |
| 5862 | else |
| 5863 | { |
| 5864 | hs_buf->is_complete = 1; |
| 5865 | } |
| 5866 | |
| 5867 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete", |
| 5868 | hs_buf->is_complete ? "" : "not yet " ) ); |
| 5869 | } |
| 5870 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5871 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5872 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5873 | |
| 5874 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 5875 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5876 | break; |
| 5877 | } |
| 5878 | |
| 5879 | exit: |
| 5880 | |
| 5881 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) ); |
| 5882 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5883 | } |
| 5884 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5885 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5886 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5887 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5888 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5889 | * Consume last content-layer message and potentially |
| 5890 | * update in_msglen which keeps track of the contents' |
| 5891 | * consumption state. |
| 5892 | * |
| 5893 | * (1) Handshake messages: |
| 5894 | * Remove last handshake message, move content |
| 5895 | * and adapt in_msglen. |
| 5896 | * |
| 5897 | * (2) Alert messages: |
| 5898 | * Consume whole record content, in_msglen = 0. |
| 5899 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5900 | * (3) Change cipher spec: |
| 5901 | * Consume whole record content, in_msglen = 0. |
| 5902 | * |
| 5903 | * (4) Application data: |
| 5904 | * Don't do anything - the record layer provides |
| 5905 | * the application data as a stream transport |
| 5906 | * and consumes through mbedtls_ssl_read only. |
| 5907 | * |
| 5908 | */ |
| 5909 | |
| 5910 | /* Case (1): Handshake messages */ |
| 5911 | if( ssl->in_hslen != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5912 | { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5913 | /* Hard assertion to be sure that no application data |
| 5914 | * is in flight, as corrupting ssl->in_msglen during |
| 5915 | * ssl->in_offt != NULL is fatal. */ |
| 5916 | if( ssl->in_offt != NULL ) |
| 5917 | { |
| 5918 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5919 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5920 | } |
| 5921 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5922 | /* |
| 5923 | * Get next Handshake message in the current record |
| 5924 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5925 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5926 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5927 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5928 | * current handshake content: If DTLS handshake |
| 5929 | * fragmentation is used, that's the fragment |
| 5930 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5931 | * size here is faulty and should be changed at |
| 5932 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5933 | * (2) While it doesn't seem to cause problems, one |
| 5934 | * has to be very careful not to assume that in_hslen |
| 5935 | * is always <= in_msglen in a sensible communication. |
| 5936 | * Again, it's wrong for DTLS handshake fragmentation. |
| 5937 | * The following check is therefore mandatory, and |
| 5938 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5939 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 5940 | * bounds after handling a DTLS message with an unexpected |
| 5941 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5942 | */ |
| 5943 | if( ssl->in_hslen < ssl->in_msglen ) |
| 5944 | { |
| 5945 | ssl->in_msglen -= ssl->in_hslen; |
| 5946 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 5947 | ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5948 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5949 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", |
| 5950 | ssl->in_msg, ssl->in_msglen ); |
| 5951 | } |
| 5952 | else |
| 5953 | { |
| 5954 | ssl->in_msglen = 0; |
| 5955 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 5956 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5957 | ssl->in_hslen = 0; |
| 5958 | } |
| 5959 | /* Case (4): Application data */ |
| 5960 | else if( ssl->in_offt != NULL ) |
| 5961 | { |
| 5962 | return( 0 ); |
| 5963 | } |
| 5964 | /* Everything else (CCS & Alerts) */ |
| 5965 | else |
| 5966 | { |
| 5967 | ssl->in_msglen = 0; |
| 5968 | } |
| 5969 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5970 | return( 0 ); |
| 5971 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5972 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5973 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ) |
| 5974 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5975 | if( ssl->in_msglen > 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5976 | return( 1 ); |
| 5977 | |
| 5978 | return( 0 ); |
| 5979 | } |
| 5980 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5981 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5982 | |
| 5983 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ) |
| 5984 | { |
| 5985 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5986 | if( hs == NULL ) |
| 5987 | return; |
| 5988 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5989 | if( hs->buffering.future_record.data != NULL ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5990 | { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5991 | hs->buffering.total_bytes_buffered -= |
| 5992 | hs->buffering.future_record.len; |
| 5993 | |
| 5994 | mbedtls_free( hs->buffering.future_record.data ); |
| 5995 | hs->buffering.future_record.data = NULL; |
| 5996 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5997 | } |
| 5998 | |
| 5999 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ) |
| 6000 | { |
| 6001 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 6002 | unsigned char * rec; |
| 6003 | size_t rec_len; |
| 6004 | unsigned rec_epoch; |
| 6005 | |
| 6006 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6007 | return( 0 ); |
| 6008 | |
| 6009 | if( hs == NULL ) |
| 6010 | return( 0 ); |
| 6011 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6012 | rec = hs->buffering.future_record.data; |
| 6013 | rec_len = hs->buffering.future_record.len; |
| 6014 | rec_epoch = hs->buffering.future_record.epoch; |
| 6015 | |
| 6016 | if( rec == NULL ) |
| 6017 | return( 0 ); |
| 6018 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 6019 | /* Only consider loading future records if the |
| 6020 | * input buffer is empty. */ |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 6021 | if( ssl_next_record_is_in_datagram( ssl ) == 1 ) |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 6022 | return( 0 ); |
| 6023 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6024 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) ); |
| 6025 | |
| 6026 | if( rec_epoch != ssl->in_epoch ) |
| 6027 | { |
| 6028 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) ); |
| 6029 | goto exit; |
| 6030 | } |
| 6031 | |
| 6032 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) ); |
| 6033 | |
| 6034 | /* Double-check that the record is not too large */ |
| 6035 | if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN - |
| 6036 | (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
| 6037 | { |
| 6038 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6039 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6040 | } |
| 6041 | |
| 6042 | memcpy( ssl->in_hdr, rec, rec_len ); |
| 6043 | ssl->in_left = rec_len; |
| 6044 | ssl->next_record_offset = 0; |
| 6045 | |
| 6046 | ssl_free_buffered_record( ssl ); |
| 6047 | |
| 6048 | exit: |
| 6049 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) ); |
| 6050 | return( 0 ); |
| 6051 | } |
| 6052 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6053 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, |
| 6054 | mbedtls_record const *rec ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6055 | { |
| 6056 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6057 | |
| 6058 | /* Don't buffer future records outside handshakes. */ |
| 6059 | if( hs == NULL ) |
| 6060 | return( 0 ); |
| 6061 | |
| 6062 | /* Only buffer handshake records (we are only interested |
| 6063 | * in Finished messages). */ |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6064 | if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6065 | return( 0 ); |
| 6066 | |
| 6067 | /* Don't buffer more than one future epoch record. */ |
| 6068 | if( hs->buffering.future_record.data != NULL ) |
| 6069 | return( 0 ); |
| 6070 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6071 | /* Don't buffer record if there's not enough buffering space remaining. */ |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6072 | if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6073 | hs->buffering.total_bytes_buffered ) ) |
| 6074 | { |
| 6075 | 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] | 6076 | (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6077 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6078 | return( 0 ); |
| 6079 | } |
| 6080 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6081 | /* Buffer record */ |
| 6082 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u", |
| 6083 | ssl->in_epoch + 1 ) ); |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6084 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6085 | |
| 6086 | /* ssl_parse_record_header() only considers records |
| 6087 | * of the next epoch as candidates for buffering. */ |
| 6088 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6089 | hs->buffering.future_record.len = rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6090 | |
| 6091 | hs->buffering.future_record.data = |
| 6092 | mbedtls_calloc( 1, hs->buffering.future_record.len ); |
| 6093 | if( hs->buffering.future_record.data == NULL ) |
| 6094 | { |
| 6095 | /* If we run out of RAM trying to buffer a |
| 6096 | * record from the next epoch, just ignore. */ |
| 6097 | return( 0 ); |
| 6098 | } |
| 6099 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6100 | memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6101 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6102 | hs->buffering.total_bytes_buffered += rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6103 | return( 0 ); |
| 6104 | } |
| 6105 | |
| 6106 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6107 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 6108 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6109 | { |
| 6110 | int ret; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 6111 | mbedtls_record rec; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6112 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6113 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6114 | /* We might have buffered a future record; if so, |
| 6115 | * and if the epoch matches now, load it. |
| 6116 | * On success, this call will set ssl->in_left to |
| 6117 | * the length of the buffered record, so that |
| 6118 | * the calls to ssl_fetch_input() below will |
| 6119 | * essentially be no-ops. */ |
| 6120 | ret = ssl_load_buffered_record( ssl ); |
| 6121 | if( ret != 0 ) |
| 6122 | return( ret ); |
| 6123 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6124 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 6125 | /* Ensure that we have enough space available for the default form |
| 6126 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 6127 | * with no space for CIDs counted in). */ |
| 6128 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 6129 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6130 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6131 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6132 | return( ret ); |
| 6133 | } |
| 6134 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 6135 | ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec ); |
| 6136 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6137 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6138 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6139 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6140 | { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6141 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 6142 | { |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6143 | ret = ssl_buffer_future_record( ssl, &rec ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6144 | if( ret != 0 ) |
| 6145 | return( ret ); |
| 6146 | |
| 6147 | /* Fall through to handling of unexpected records */ |
| 6148 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 6149 | } |
| 6150 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6151 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ) |
| 6152 | { |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6153 | #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] | 6154 | /* Reset in pointers to default state for TLS/DTLS records, |
| 6155 | * assuming no CID and no offset between record content and |
| 6156 | * record plaintext. */ |
| 6157 | ssl_update_in_pointers( ssl ); |
| 6158 | |
Hanno Becker | 7ae20e0 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 6159 | /* Setup internal message pointers from record structure. */ |
| 6160 | ssl->in_msgtype = rec.type; |
| 6161 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6162 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 6163 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6164 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
| 6165 | ssl->in_msglen = rec.data_len; |
| 6166 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6167 | ret = ssl_check_client_reconnect( ssl ); |
| 6168 | if( ret != 0 ) |
| 6169 | return( ret ); |
| 6170 | #endif |
| 6171 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6172 | /* Skip unexpected record (but not whole datagram) */ |
Hanno Becker | 4acada3 | 2019-07-11 12:48:53 +0100 | [diff] [blame] | 6173 | ssl->next_record_offset = rec.buf_len; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6174 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6175 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record " |
| 6176 | "(header)" ) ); |
| 6177 | } |
| 6178 | else |
| 6179 | { |
| 6180 | /* Skip invalid record and the rest of the datagram */ |
| 6181 | ssl->next_record_offset = 0; |
| 6182 | ssl->in_left = 0; |
| 6183 | |
| 6184 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record " |
| 6185 | "(header)" ) ); |
| 6186 | } |
| 6187 | |
| 6188 | /* Get next record */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6189 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6190 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6191 | else |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6192 | #endif |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6193 | { |
| 6194 | return( ret ); |
| 6195 | } |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6196 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6198 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6199 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 6200 | { |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6201 | /* Remember offset of next record within datagram. */ |
Hanno Becker | f50da50 | 2019-07-11 12:50:10 +0100 | [diff] [blame] | 6202 | ssl->next_record_offset = rec.buf_len; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 6203 | if( ssl->next_record_offset < ssl->in_left ) |
| 6204 | { |
| 6205 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) ); |
| 6206 | } |
| 6207 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6208 | else |
| 6209 | #endif |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6210 | { |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 6211 | /* |
| 6212 | * Fetch record contents from underlying transport. |
| 6213 | */ |
Hanno Becker | a317566 | 2019-07-11 12:50:29 +0100 | [diff] [blame] | 6214 | ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len ); |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6215 | if( ret != 0 ) |
| 6216 | { |
| 6217 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
| 6218 | return( ret ); |
| 6219 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6220 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6221 | ssl->in_left = 0; |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | /* |
| 6225 | * Decrypt record contents. |
| 6226 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6227 | |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 6228 | if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6229 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6230 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6231 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6232 | { |
| 6233 | /* Silently discard invalid records */ |
Hanno Becker | 82e2a39 | 2019-05-03 16:36:59 +0100 | [diff] [blame] | 6234 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6235 | { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 6236 | /* Except when waiting for Finished as a bad mac here |
| 6237 | * probably means something went wrong in the handshake |
| 6238 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
| 6239 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 6240 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) |
| 6241 | { |
| 6242 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 6243 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 6244 | { |
| 6245 | mbedtls_ssl_send_alert_message( ssl, |
| 6246 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6247 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
| 6248 | } |
| 6249 | #endif |
| 6250 | return( ret ); |
| 6251 | } |
| 6252 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6253 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6254 | if( ssl->conf->badmac_limit != 0 && |
| 6255 | ++ssl->badmac_seen >= ssl->conf->badmac_limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6256 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6257 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); |
| 6258 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6259 | } |
| 6260 | #endif |
| 6261 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6262 | /* As above, invalid records cause |
| 6263 | * dismissal of the whole datagram. */ |
| 6264 | |
| 6265 | ssl->next_record_offset = 0; |
| 6266 | ssl->in_left = 0; |
| 6267 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6268 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6269 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6270 | } |
| 6271 | |
| 6272 | return( ret ); |
| 6273 | } |
| 6274 | else |
| 6275 | #endif |
| 6276 | { |
| 6277 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6278 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 6279 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6280 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6281 | mbedtls_ssl_send_alert_message( ssl, |
| 6282 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6283 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6284 | } |
| 6285 | #endif |
| 6286 | return( ret ); |
| 6287 | } |
| 6288 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6289 | |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6290 | |
| 6291 | /* Reset in pointers to default state for TLS/DTLS records, |
| 6292 | * assuming no CID and no offset between record content and |
| 6293 | * record plaintext. */ |
| 6294 | ssl_update_in_pointers( ssl ); |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6295 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6296 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 6297 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6298 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6299 | |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 6300 | /* The record content type may change during decryption, |
| 6301 | * so re-read it. */ |
| 6302 | ssl->in_msgtype = rec.type; |
| 6303 | /* Also update the input buffer, because unfortunately |
| 6304 | * the server-side ssl_parse_client_hello() reparses the |
| 6305 | * record header when receiving a ClientHello initiating |
| 6306 | * a renegotiation. */ |
| 6307 | ssl->in_hdr[0] = rec.type; |
| 6308 | ssl->in_msg = rec.buf + rec.data_offset; |
| 6309 | ssl->in_msglen = rec.data_len; |
| 6310 | ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 6311 | ssl->in_len[1] = (unsigned char)( rec.data_len ); |
| 6312 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6313 | return( 0 ); |
| 6314 | } |
| 6315 | |
| 6316 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ) |
| 6317 | { |
| 6318 | int ret; |
| 6319 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6320 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6321 | * Handle particular types of records |
| 6322 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6323 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6324 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6325 | if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 ) |
| 6326 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 6327 | return( ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6328 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6329 | } |
| 6330 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6331 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6332 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6333 | if( ssl->in_msglen != 1 ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6334 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6335 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d", |
| 6336 | ssl->in_msglen ) ); |
| 6337 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6338 | } |
| 6339 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6340 | if( ssl->in_msg[0] != 1 ) |
| 6341 | { |
| 6342 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x", |
| 6343 | ssl->in_msg[0] ) ); |
| 6344 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6345 | } |
| 6346 | |
| 6347 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6348 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6349 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
| 6350 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 6351 | { |
| 6352 | if( ssl->handshake == NULL ) |
| 6353 | { |
| 6354 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) ); |
| 6355 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 6356 | } |
| 6357 | |
| 6358 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) ); |
| 6359 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 6360 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6361 | #endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6362 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6364 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6365 | { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 6366 | if( ssl->in_msglen != 2 ) |
| 6367 | { |
| 6368 | /* Note: Standard allows for more than one 2 byte alert |
| 6369 | to be packed in a single message, but Mbed TLS doesn't |
| 6370 | currently support this. */ |
| 6371 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d", |
| 6372 | ssl->in_msglen ) ); |
| 6373 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6374 | } |
| 6375 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6376 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6377 | ssl->in_msg[0], ssl->in_msg[1] ) ); |
| 6378 | |
| 6379 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 6380 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6381 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6382 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6383 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6384 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 6385 | ssl->in_msg[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6386 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6387 | } |
| 6388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6389 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6390 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6391 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6392 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); |
| 6393 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6394 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 6395 | |
| 6396 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
| 6397 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6398 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) |
| 6399 | { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6400 | 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] | 6401 | /* Will be handled when trying to parse ServerHello */ |
| 6402 | return( 0 ); |
| 6403 | } |
| 6404 | #endif |
| 6405 | |
| 6406 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) |
| 6407 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 6408 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 6409 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6410 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 6411 | { |
| 6412 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) ); |
| 6413 | /* Will be handled in mbedtls_ssl_parse_certificate() */ |
| 6414 | return( 0 ); |
| 6415 | } |
| 6416 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 6417 | |
| 6418 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6419 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6420 | } |
| 6421 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6422 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6423 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6424 | { |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6425 | /* Drop unexpected ApplicationData records, |
| 6426 | * except at the beginning of renegotiations */ |
| 6427 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
| 6428 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 6429 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6430 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 6431 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6432 | #endif |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6433 | ) |
| 6434 | { |
| 6435 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); |
| 6436 | return( MBEDTLS_ERR_SSL_NON_FATAL ); |
| 6437 | } |
| 6438 | |
| 6439 | if( ssl->handshake != NULL && |
| 6440 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 6441 | { |
| 6442 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 6443 | } |
| 6444 | } |
Hanno Becker | 4a4af9f | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 6445 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6446 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6447 | return( 0 ); |
| 6448 | } |
| 6449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6450 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6451 | { |
| 6452 | int ret; |
| 6453 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6454 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 6455 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6456 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6457 | { |
| 6458 | return( ret ); |
| 6459 | } |
| 6460 | |
| 6461 | return( 0 ); |
| 6462 | } |
| 6463 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6464 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6465 | unsigned char level, |
| 6466 | unsigned char message ) |
| 6467 | { |
| 6468 | int ret; |
| 6469 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 6470 | if( ssl == NULL || ssl->conf == NULL ) |
| 6471 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6472 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6473 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6474 | 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] | 6475 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6476 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6477 | ssl->out_msglen = 2; |
| 6478 | ssl->out_msg[0] = level; |
| 6479 | ssl->out_msg[1] = message; |
| 6480 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 6481 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6482 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6483 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6484 | return( ret ); |
| 6485 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6486 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6487 | |
| 6488 | return( 0 ); |
| 6489 | } |
| 6490 | |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6491 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6492 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 6493 | { |
| 6494 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6495 | if( session->peer_cert != NULL ) |
| 6496 | { |
| 6497 | mbedtls_x509_crt_free( session->peer_cert ); |
| 6498 | mbedtls_free( session->peer_cert ); |
| 6499 | session->peer_cert = NULL; |
| 6500 | } |
| 6501 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6502 | if( session->peer_cert_digest != NULL ) |
| 6503 | { |
| 6504 | /* Zeroization is not necessary. */ |
| 6505 | mbedtls_free( session->peer_cert_digest ); |
| 6506 | session->peer_cert_digest = NULL; |
| 6507 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 6508 | session->peer_cert_digest_len = 0; |
| 6509 | } |
| 6510 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6511 | } |
| 6512 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 6513 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6514 | /* |
| 6515 | * Handshake functions |
| 6516 | */ |
Hanno Becker | 2148993 | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6517 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6518 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6519 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6520 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6521 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6522 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6523 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6524 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6525 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6526 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6527 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6528 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6529 | ssl->state++; |
| 6530 | return( 0 ); |
| 6531 | } |
| 6532 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6533 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6534 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6535 | } |
| 6536 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6537 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6538 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6539 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6540 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6541 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6542 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6543 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6544 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6545 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6546 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6547 | ssl->state++; |
| 6548 | return( 0 ); |
| 6549 | } |
| 6550 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6551 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6552 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6553 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6554 | |
Hanno Becker | 2148993 | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6555 | #else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6556 | /* Some certificate support -> implement write and parse */ |
| 6557 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6558 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6559 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6560 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6561 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6562 | const mbedtls_x509_crt *crt; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6563 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6564 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6566 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6567 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6568 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6569 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6570 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6571 | ssl->state++; |
| 6572 | return( 0 ); |
| 6573 | } |
| 6574 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6575 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6576 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6577 | { |
| 6578 | if( ssl->client_auth == 0 ) |
| 6579 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6580 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6581 | ssl->state++; |
| 6582 | return( 0 ); |
| 6583 | } |
| 6584 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6585 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6586 | /* |
| 6587 | * If using SSLv3 and got no cert, send an Alert message |
| 6588 | * (otherwise an empty Certificate message will be sent). |
| 6589 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6590 | if( mbedtls_ssl_own_cert( ssl ) == NULL && |
| 6591 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6592 | { |
| 6593 | ssl->out_msglen = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6594 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
| 6595 | ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING; |
| 6596 | ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6597 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6598 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6599 | goto write_msg; |
| 6600 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6601 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6602 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6603 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6604 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6605 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6606 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6607 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6608 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6609 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); |
| 6610 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6611 | } |
| 6612 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6613 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6614 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6615 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6616 | |
| 6617 | /* |
| 6618 | * 0 . 0 handshake type |
| 6619 | * 1 . 3 handshake length |
| 6620 | * 4 . 6 length of all certs |
| 6621 | * 7 . 9 length of cert. 1 |
| 6622 | * 10 . n-1 peer certificate |
| 6623 | * n . n+2 length of cert. 2 |
| 6624 | * n+3 . ... upper level cert, etc. |
| 6625 | */ |
| 6626 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6627 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6628 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 6629 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6630 | { |
| 6631 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6632 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6633 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6634 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6635 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6636 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6637 | } |
| 6638 | |
| 6639 | ssl->out_msg[i ] = (unsigned char)( n >> 16 ); |
| 6640 | ssl->out_msg[i + 1] = (unsigned char)( n >> 8 ); |
| 6641 | ssl->out_msg[i + 2] = (unsigned char)( n ); |
| 6642 | |
| 6643 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6644 | i += n; crt = crt->next; |
| 6645 | } |
| 6646 | |
| 6647 | ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 ); |
| 6648 | ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 ); |
| 6649 | ssl->out_msg[6] = (unsigned char)( ( i - 7 ) ); |
| 6650 | |
| 6651 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6652 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6653 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6654 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 6655 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6656 | write_msg: |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6657 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6658 | |
| 6659 | ssl->state++; |
| 6660 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6661 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6662 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6663 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6664 | return( ret ); |
| 6665 | } |
| 6666 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6667 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6668 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 6669 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6670 | } |
| 6671 | |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6672 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6673 | |
| 6674 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6675 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6676 | unsigned char *crt_buf, |
| 6677 | size_t crt_buf_len ) |
| 6678 | { |
| 6679 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6680 | |
| 6681 | if( peer_crt == NULL ) |
| 6682 | return( -1 ); |
| 6683 | |
| 6684 | if( peer_crt->raw.len != crt_buf_len ) |
| 6685 | return( -1 ); |
| 6686 | |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6687 | return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6688 | } |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6689 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6690 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6691 | unsigned char *crt_buf, |
| 6692 | size_t crt_buf_len ) |
| 6693 | { |
| 6694 | int ret; |
| 6695 | unsigned char const * const peer_cert_digest = |
| 6696 | ssl->session->peer_cert_digest; |
| 6697 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6698 | ssl->session->peer_cert_digest_type; |
| 6699 | mbedtls_md_info_t const * const digest_info = |
| 6700 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6701 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6702 | size_t digest_len; |
| 6703 | |
| 6704 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6705 | return( -1 ); |
| 6706 | |
| 6707 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6708 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6709 | return( -1 ); |
| 6710 | |
| 6711 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6712 | if( ret != 0 ) |
| 6713 | return( -1 ); |
| 6714 | |
| 6715 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6716 | } |
| 6717 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6718 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6719 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6720 | /* |
| 6721 | * Once the certificate message is read, parse it into a cert chain and |
| 6722 | * perform basic checks, but leave actual verification to the caller |
| 6723 | */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6724 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6725 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6726 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6727 | int ret; |
| 6728 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6729 | int crt_cnt=0; |
| 6730 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6731 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 6732 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6733 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6734 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6735 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6736 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6737 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6738 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6739 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6740 | } |
| 6741 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6742 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || |
| 6743 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6744 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6745 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6746 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6747 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6748 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6749 | } |
| 6750 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6751 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6752 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6753 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6754 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6755 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6756 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6757 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6758 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6759 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6760 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6761 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6762 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6763 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6764 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6765 | } |
| 6766 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6767 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6768 | i += 3; |
| 6769 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6770 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6771 | while( i < ssl->in_hslen ) |
| 6772 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6773 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6774 | if ( i + 3 > ssl->in_hslen ) { |
| 6775 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6776 | mbedtls_ssl_send_alert_message( ssl, |
| 6777 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6778 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6779 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 6780 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6781 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6782 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6783 | if( ssl->in_msg[i] != 0 ) |
| 6784 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6785 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6786 | mbedtls_ssl_send_alert_message( ssl, |
| 6787 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6788 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6789 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6790 | } |
| 6791 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6792 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6793 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6794 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6795 | i += 3; |
| 6796 | |
| 6797 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6798 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6799 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6800 | mbedtls_ssl_send_alert_message( ssl, |
| 6801 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6802 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6803 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6804 | } |
| 6805 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6806 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6807 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6808 | if( crt_cnt++ == 0 && |
| 6809 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6810 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6811 | { |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6812 | /* During client-side renegotiation, check that the server's |
| 6813 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6814 | * mitigating the triple handshake attack. On success, reuse |
| 6815 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6816 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6817 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6818 | &ssl->in_msg[i], |
| 6819 | n ) != 0 ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6820 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6821 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6822 | mbedtls_ssl_send_alert_message( ssl, |
| 6823 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6824 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6825 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6826 | } |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6827 | |
| 6828 | /* Now we can safely free the original chain. */ |
| 6829 | ssl_clear_peer_cert( ssl->session ); |
| 6830 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6831 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6832 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6833 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6834 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6835 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6836 | #else |
Hanno Becker | 353a6f0 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 6837 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6838 | * it in-place from the input buffer instead of making a copy. */ |
| 6839 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6840 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6841 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6842 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6843 | case 0: /*ok*/ |
| 6844 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6845 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6846 | prior certificate was already trusted. */ |
| 6847 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6848 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6849 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6850 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6851 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6852 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6853 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6854 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6855 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6856 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6857 | default: |
| 6858 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6859 | crt_parse_der_failed: |
| 6860 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6861 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6862 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6863 | } |
| 6864 | |
| 6865 | i += n; |
| 6866 | } |
| 6867 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6868 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6869 | return( 0 ); |
| 6870 | } |
| 6871 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6872 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6873 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6874 | { |
| 6875 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6876 | return( -1 ); |
| 6877 | |
| 6878 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 6879 | /* |
| 6880 | * Check if the client sent an empty certificate |
| 6881 | */ |
| 6882 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
| 6883 | { |
| 6884 | if( ssl->in_msglen == 2 && |
| 6885 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 6886 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6887 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 6888 | { |
| 6889 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); |
| 6890 | return( 0 ); |
| 6891 | } |
| 6892 | |
| 6893 | return( -1 ); |
| 6894 | } |
| 6895 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 6896 | |
| 6897 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 6898 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6899 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6900 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6901 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6902 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6903 | { |
| 6904 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 6905 | return( 0 ); |
| 6906 | } |
| 6907 | |
| 6908 | return( -1 ); |
| 6909 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 6910 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6911 | } |
| 6912 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6913 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6914 | /* Check if a certificate message is expected. |
| 6915 | * Return either |
| 6916 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6917 | * - SSL_CERTIFICATE_SKIP |
| 6918 | * indicating whether a Certificate message is expected or not. |
| 6919 | */ |
| 6920 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6921 | #define SSL_CERTIFICATE_SKIP 1 |
| 6922 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6923 | int authmode ) |
| 6924 | { |
| 6925 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6926 | ssl->handshake->ciphersuite_info; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6927 | |
| 6928 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6929 | return( SSL_CERTIFICATE_SKIP ); |
| 6930 | |
| 6931 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6932 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6933 | { |
| 6934 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6935 | return( SSL_CERTIFICATE_SKIP ); |
| 6936 | |
| 6937 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6938 | { |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6939 | ssl->session_negotiate->verify_result = |
| 6940 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6941 | return( SSL_CERTIFICATE_SKIP ); |
| 6942 | } |
| 6943 | } |
Hanno Becker | 84d9d27 | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 6944 | #else |
| 6945 | ((void) authmode); |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6946 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6947 | |
| 6948 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6949 | } |
| 6950 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6951 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6952 | int authmode, |
| 6953 | mbedtls_x509_crt *chain, |
| 6954 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6955 | { |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 6956 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6957 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6958 | ssl->handshake->ciphersuite_info; |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 6959 | int have_ca_chain = 0; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6960 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 6961 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 6962 | void *p_vrfy; |
| 6963 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6964 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6965 | return( 0 ); |
| 6966 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 6967 | if( ssl->f_vrfy != NULL ) |
| 6968 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 6969 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 6970 | f_vrfy = ssl->f_vrfy; |
| 6971 | p_vrfy = ssl->p_vrfy; |
| 6972 | } |
| 6973 | else |
| 6974 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 6975 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 6976 | f_vrfy = ssl->conf->f_vrfy; |
| 6977 | p_vrfy = ssl->conf->p_vrfy; |
| 6978 | } |
| 6979 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6980 | /* |
| 6981 | * Main check: verify certificate |
| 6982 | */ |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 6983 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 6984 | if( ssl->conf->f_ca_cb != NULL ) |
| 6985 | { |
| 6986 | ((void) rs_ctx); |
| 6987 | have_ca_chain = 1; |
| 6988 | |
| 6989 | 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] | 6990 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 6991 | chain, |
| 6992 | ssl->conf->f_ca_cb, |
| 6993 | ssl->conf->p_ca_cb, |
| 6994 | ssl->conf->cert_profile, |
| 6995 | ssl->hostname, |
| 6996 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 6997 | f_vrfy, p_vrfy ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 6998 | } |
| 6999 | else |
| 7000 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 7001 | { |
| 7002 | mbedtls_x509_crt *ca_chain; |
| 7003 | mbedtls_x509_crl *ca_crl; |
| 7004 | |
| 7005 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7006 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 7007 | { |
| 7008 | ca_chain = ssl->handshake->sni_ca_chain; |
| 7009 | ca_crl = ssl->handshake->sni_ca_crl; |
| 7010 | } |
| 7011 | else |
| 7012 | #endif |
| 7013 | { |
| 7014 | ca_chain = ssl->conf->ca_chain; |
| 7015 | ca_crl = ssl->conf->ca_crl; |
| 7016 | } |
| 7017 | |
| 7018 | if( ca_chain != NULL ) |
| 7019 | have_ca_chain = 1; |
| 7020 | |
| 7021 | ret = mbedtls_x509_crt_verify_restartable( |
| 7022 | chain, |
| 7023 | ca_chain, ca_crl, |
| 7024 | ssl->conf->cert_profile, |
| 7025 | ssl->hostname, |
| 7026 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 7027 | f_vrfy, p_vrfy, rs_ctx ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7028 | } |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7029 | |
| 7030 | if( ret != 0 ) |
| 7031 | { |
| 7032 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 7033 | } |
| 7034 | |
| 7035 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7036 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 7037 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 7038 | #endif |
| 7039 | |
| 7040 | /* |
| 7041 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 7042 | */ |
| 7043 | |
| 7044 | #if defined(MBEDTLS_ECP_C) |
| 7045 | { |
| 7046 | const mbedtls_pk_context *pk = &chain->pk; |
| 7047 | |
| 7048 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 7049 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 7050 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 7051 | { |
| 7052 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 7053 | |
| 7054 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 7055 | if( ret == 0 ) |
| 7056 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
| 7057 | } |
| 7058 | } |
| 7059 | #endif /* MBEDTLS_ECP_C */ |
| 7060 | |
| 7061 | if( mbedtls_ssl_check_cert_usage( chain, |
| 7062 | ciphersuite_info, |
| 7063 | ! ssl->conf->endpoint, |
| 7064 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 7065 | { |
| 7066 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 7067 | if( ret == 0 ) |
| 7068 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
| 7069 | } |
| 7070 | |
| 7071 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 7072 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 7073 | * with details encoded in the verification flags. All other kinds |
| 7074 | * of error codes, including those from the user provided f_vrfy |
| 7075 | * functions, are treated as fatal and lead to a failure of |
| 7076 | * ssl_parse_certificate even if verification was optional. */ |
| 7077 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 7078 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 7079 | ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) |
| 7080 | { |
| 7081 | ret = 0; |
| 7082 | } |
| 7083 | |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 7084 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7085 | { |
| 7086 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 7087 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 7088 | } |
| 7089 | |
| 7090 | if( ret != 0 ) |
| 7091 | { |
| 7092 | uint8_t alert; |
| 7093 | |
| 7094 | /* The certificate may have been rejected for several reasons. |
| 7095 | Pick one and send the corresponding alert. Which alert to send |
| 7096 | may be a subject of debate in some cases. */ |
| 7097 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 7098 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 7099 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 7100 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 7101 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 7102 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7103 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 7104 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7105 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 7106 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7107 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 7108 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7109 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 7110 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7111 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 7112 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 7113 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 7114 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 7115 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 7116 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 7117 | else |
| 7118 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 7119 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7120 | alert ); |
| 7121 | } |
| 7122 | |
| 7123 | #if defined(MBEDTLS_DEBUG_C) |
| 7124 | if( ssl->session_negotiate->verify_result != 0 ) |
| 7125 | { |
| 7126 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", |
| 7127 | ssl->session_negotiate->verify_result ) ); |
| 7128 | } |
| 7129 | else |
| 7130 | { |
| 7131 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 7132 | } |
| 7133 | #endif /* MBEDTLS_DEBUG_C */ |
| 7134 | |
| 7135 | return( ret ); |
| 7136 | } |
| 7137 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7138 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7139 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 7140 | unsigned char *start, size_t len ) |
| 7141 | { |
| 7142 | int ret; |
| 7143 | /* Remember digest of the peer's end-CRT. */ |
| 7144 | ssl->session_negotiate->peer_cert_digest = |
| 7145 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 7146 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 7147 | { |
| 7148 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7149 | sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) ); |
| 7150 | mbedtls_ssl_send_alert_message( ssl, |
| 7151 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7152 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7153 | |
| 7154 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7155 | } |
| 7156 | |
| 7157 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 7158 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 7159 | start, len, |
| 7160 | ssl->session_negotiate->peer_cert_digest ); |
| 7161 | |
| 7162 | ssl->session_negotiate->peer_cert_digest_type = |
| 7163 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7164 | ssl->session_negotiate->peer_cert_digest_len = |
| 7165 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7166 | |
| 7167 | return( ret ); |
| 7168 | } |
| 7169 | |
| 7170 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 7171 | unsigned char *start, size_t len ) |
| 7172 | { |
| 7173 | unsigned char *end = start + len; |
| 7174 | int ret; |
| 7175 | |
| 7176 | /* Make a copy of the peer's raw public key. */ |
| 7177 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 7178 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 7179 | &ssl->handshake->peer_pubkey ); |
| 7180 | if( ret != 0 ) |
| 7181 | { |
| 7182 | /* We should have parsed the public key before. */ |
| 7183 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7184 | } |
| 7185 | |
| 7186 | return( 0 ); |
| 7187 | } |
| 7188 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7189 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7190 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 7191 | { |
| 7192 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7193 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7194 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7195 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7196 | ? ssl->handshake->sni_authmode |
| 7197 | : ssl->conf->authmode; |
| 7198 | #else |
| 7199 | const int authmode = ssl->conf->authmode; |
| 7200 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7201 | void *rs_ctx = NULL; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7202 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7203 | |
| 7204 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 7205 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7206 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 7207 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7208 | { |
| 7209 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7210 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7211 | } |
| 7212 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7213 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7214 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7215 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7216 | { |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7217 | chain = ssl->handshake->ecrs_peer_cert; |
| 7218 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7219 | goto crt_verify; |
| 7220 | } |
| 7221 | #endif |
| 7222 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 7223 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7224 | { |
| 7225 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7226 | let it decide whether to alert. */ |
| 7227 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7228 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7229 | } |
| 7230 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7231 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7232 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 7233 | { |
| 7234 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7235 | |
| 7236 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7237 | ret = 0; |
| 7238 | else |
| 7239 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7240 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7241 | goto exit; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7242 | } |
| 7243 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7244 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7245 | /* Clear existing peer CRT structure in case we tried to |
| 7246 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7247 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7248 | |
| 7249 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7250 | if( chain == NULL ) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7251 | { |
| 7252 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7253 | sizeof( mbedtls_x509_crt ) ) ); |
| 7254 | mbedtls_ssl_send_alert_message( ssl, |
| 7255 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7256 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7257 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7258 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7259 | goto exit; |
| 7260 | } |
| 7261 | mbedtls_x509_crt_init( chain ); |
| 7262 | |
| 7263 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7264 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7265 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7266 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7267 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7268 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7269 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7270 | |
| 7271 | crt_verify: |
| 7272 | if( ssl->handshake->ecrs_enabled) |
| 7273 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 7274 | #endif |
| 7275 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7276 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7277 | chain, rs_ctx ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7278 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7279 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7280 | |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7281 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7282 | { |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7283 | unsigned char *crt_start, *pk_start; |
| 7284 | size_t crt_len, pk_len; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7285 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7286 | /* We parse the CRT chain without copying, so |
| 7287 | * these pointers point into the input buffer, |
| 7288 | * and are hence still valid after freeing the |
| 7289 | * CRT chain. */ |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7290 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7291 | crt_start = chain->raw.p; |
| 7292 | crt_len = chain->raw.len; |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7293 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7294 | pk_start = chain->pk_raw.p; |
| 7295 | pk_len = chain->pk_raw.len; |
| 7296 | |
| 7297 | /* Free the CRT structures before computing |
| 7298 | * digest and copying the peer's public key. */ |
| 7299 | mbedtls_x509_crt_free( chain ); |
| 7300 | mbedtls_free( chain ); |
| 7301 | chain = NULL; |
| 7302 | |
| 7303 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7304 | if( ret != 0 ) |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7305 | goto exit; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7306 | |
| 7307 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 7308 | if( ret != 0 ) |
| 7309 | goto exit; |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7310 | } |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7311 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7312 | /* Pass ownership to session structure. */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7313 | ssl->session_negotiate->peer_cert = chain; |
| 7314 | chain = NULL; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7315 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7317 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7318 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7319 | exit: |
| 7320 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7321 | if( ret == 0 ) |
| 7322 | ssl->state++; |
| 7323 | |
| 7324 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7325 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7326 | { |
| 7327 | ssl->handshake->ecrs_peer_cert = chain; |
| 7328 | chain = NULL; |
| 7329 | } |
| 7330 | #endif |
| 7331 | |
| 7332 | if( chain != NULL ) |
| 7333 | { |
| 7334 | mbedtls_x509_crt_free( chain ); |
| 7335 | mbedtls_free( chain ); |
| 7336 | } |
| 7337 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7338 | return( ret ); |
| 7339 | } |
Hanno Becker | 2148993 | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 7340 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7341 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7342 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7343 | { |
| 7344 | int ret; |
| 7345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7346 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7347 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7348 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7349 | ssl->out_msglen = 1; |
| 7350 | ssl->out_msg[0] = 1; |
| 7351 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7352 | ssl->state++; |
| 7353 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7354 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7355 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7356 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7357 | return( ret ); |
| 7358 | } |
| 7359 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7360 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7361 | |
| 7362 | return( 0 ); |
| 7363 | } |
| 7364 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7365 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7366 | { |
| 7367 | int ret; |
| 7368 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7369 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7370 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 7371 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7372 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7373 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7374 | return( ret ); |
| 7375 | } |
| 7376 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7377 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7378 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7379 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7380 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7381 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7382 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7383 | } |
| 7384 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 7385 | /* CCS records are only accepted if they have length 1 and content '1', |
| 7386 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7387 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7388 | /* |
| 7389 | * Switch to our negotiated transform and session parameters for inbound |
| 7390 | * data. |
| 7391 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7392 | 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] | 7393 | ssl->transform_in = ssl->transform_negotiate; |
| 7394 | ssl->session_in = ssl->session_negotiate; |
| 7395 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7396 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7397 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7398 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7399 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7400 | ssl_dtls_replay_reset( ssl ); |
| 7401 | #endif |
| 7402 | |
| 7403 | /* Increment epoch */ |
| 7404 | if( ++ssl->in_epoch == 0 ) |
| 7405 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7406 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7407 | /* This is highly unlikely to happen for legitimate reasons, so |
| 7408 | 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] | 7409 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7410 | } |
| 7411 | } |
| 7412 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7413 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7414 | memset( ssl->in_ctr, 0, 8 ); |
| 7415 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7416 | ssl_update_in_pointers( ssl ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7417 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7418 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7419 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7420 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7421 | 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] | 7422 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7423 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7424 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7425 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7426 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7427 | } |
| 7428 | } |
| 7429 | #endif |
| 7430 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7431 | ssl->state++; |
| 7432 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7433 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7434 | |
| 7435 | return( 0 ); |
| 7436 | } |
| 7437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7438 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 7439 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7440 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 7441 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7442 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7443 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7444 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7445 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7446 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7447 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7448 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7449 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7450 | #if defined(MBEDTLS_SHA512_C) |
| 7451 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7452 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 7453 | else |
| 7454 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7455 | #if defined(MBEDTLS_SHA256_C) |
| 7456 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7457 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7458 | else |
| 7459 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7460 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 7461 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7462 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7463 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 7464 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7465 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 7466 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7467 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7468 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7469 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7470 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7471 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 ); |
| 7472 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7473 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7474 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7475 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7476 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 7477 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7478 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 7479 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7480 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7481 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7482 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7483 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7484 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 7485 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7486 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7487 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7488 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7489 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7490 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7491 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7492 | } |
| 7493 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7494 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7495 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7496 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7497 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7498 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7499 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7500 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7501 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7502 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7503 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7504 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7505 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 7506 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7507 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7508 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7509 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7510 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7511 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7512 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7513 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7514 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7515 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7516 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7517 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7518 | } |
| 7519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7520 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7521 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7522 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7523 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7524 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7525 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7526 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7527 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7528 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7529 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7530 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7531 | #if defined(MBEDTLS_SHA256_C) |
| 7532 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7533 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7534 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7535 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7536 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 7537 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7538 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7539 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7540 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7541 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7543 | #if defined(MBEDTLS_SHA512_C) |
| 7544 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7545 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7546 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7547 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7548 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7549 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7550 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7551 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7552 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7553 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7554 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7555 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7556 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7557 | static void ssl_calc_finished_ssl( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7558 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7559 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7560 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7561 | mbedtls_md5_context md5; |
| 7562 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7563 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7564 | unsigned char padbuf[48]; |
| 7565 | unsigned char md5sum[16]; |
| 7566 | unsigned char sha1sum[20]; |
| 7567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7568 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7569 | if( !session ) |
| 7570 | session = ssl->session; |
| 7571 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7572 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7573 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7574 | mbedtls_md5_init( &md5 ); |
| 7575 | mbedtls_sha1_init( &sha1 ); |
| 7576 | |
| 7577 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7578 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7579 | |
| 7580 | /* |
| 7581 | * SSLv3: |
| 7582 | * hash = |
| 7583 | * MD5( master + pad2 + |
| 7584 | * MD5( handshake + sender + master + pad1 ) ) |
| 7585 | * + SHA1( master + pad2 + |
| 7586 | * SHA1( handshake + sender + master + pad1 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7587 | */ |
| 7588 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7589 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7590 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7591 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7592 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7593 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7594 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7595 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7596 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7597 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7598 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7599 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7600 | : "SRVR"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7601 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7602 | memset( padbuf, 0x36, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7603 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7604 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); |
| 7605 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7606 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7607 | mbedtls_md5_finish_ret( &md5, md5sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7608 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7609 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); |
| 7610 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7611 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); |
| 7612 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7613 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7614 | memset( padbuf, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7615 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7616 | mbedtls_md5_starts_ret( &md5 ); |
| 7617 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7618 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7619 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); |
| 7620 | mbedtls_md5_finish_ret( &md5, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7621 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7622 | mbedtls_sha1_starts_ret( &sha1 ); |
| 7623 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7624 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); |
| 7625 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); |
| 7626 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7628 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7629 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7630 | mbedtls_md5_free( &md5 ); |
| 7631 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7632 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7633 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7634 | mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) ); |
| 7635 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7637 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7638 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7639 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7641 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7642 | static void ssl_calc_finished_tls( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7643 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7644 | { |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7645 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7646 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7647 | mbedtls_md5_context md5; |
| 7648 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7649 | unsigned char padbuf[36]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7651 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7652 | if( !session ) |
| 7653 | session = ssl->session; |
| 7654 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7655 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7656 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7657 | mbedtls_md5_init( &md5 ); |
| 7658 | mbedtls_sha1_init( &sha1 ); |
| 7659 | |
| 7660 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7661 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7662 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7663 | /* |
| 7664 | * TLSv1: |
| 7665 | * hash = PRF( master, finished_label, |
| 7666 | * MD5( handshake ) + SHA1( handshake ) )[0..11] |
| 7667 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7669 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7670 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7671 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7672 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7673 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7674 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7675 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7676 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7677 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7679 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7680 | ? "client finished" |
| 7681 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7682 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7683 | mbedtls_md5_finish_ret( &md5, padbuf ); |
| 7684 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7685 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7686 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7687 | padbuf, 36, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7689 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7690 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7691 | mbedtls_md5_free( &md5 ); |
| 7692 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7693 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7694 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7696 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7697 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7698 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7699 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7700 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7701 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7702 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7703 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7704 | { |
| 7705 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7706 | const char *sender; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7707 | unsigned char padbuf[32]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7708 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7709 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7710 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7711 | psa_status_t status; |
| 7712 | #else |
| 7713 | mbedtls_sha256_context sha256; |
| 7714 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7715 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7716 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7717 | if( !session ) |
| 7718 | session = ssl->session; |
| 7719 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7720 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7721 | ? "client finished" |
| 7722 | : "server finished"; |
| 7723 | |
| 7724 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7725 | sha256_psa = psa_hash_operation_init(); |
| 7726 | |
| 7727 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 7728 | |
| 7729 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 7730 | if( status != PSA_SUCCESS ) |
| 7731 | { |
| 7732 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7733 | return; |
| 7734 | } |
| 7735 | |
| 7736 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7737 | if( status != PSA_SUCCESS ) |
| 7738 | { |
| 7739 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7740 | return; |
| 7741 | } |
| 7742 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 7743 | #else |
| 7744 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7745 | mbedtls_sha256_init( &sha256 ); |
| 7746 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7747 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7748 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7749 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7750 | |
| 7751 | /* |
| 7752 | * TLSv1.2: |
| 7753 | * hash = PRF( master, finished_label, |
| 7754 | * Hash( handshake ) )[0.11] |
| 7755 | */ |
| 7756 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7757 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7758 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7759 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7760 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7761 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7762 | mbedtls_sha256_finish_ret( &sha256, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7763 | mbedtls_sha256_free( &sha256 ); |
| 7764 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7765 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7766 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7767 | padbuf, 32, buf, len ); |
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 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7770 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7771 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7772 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7773 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7774 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7775 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7777 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7778 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7779 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7780 | { |
| 7781 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7782 | const char *sender; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7783 | unsigned char padbuf[48]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7784 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7785 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7786 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7787 | psa_status_t status; |
| 7788 | #else |
| 7789 | mbedtls_sha512_context sha512; |
| 7790 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7791 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7792 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7793 | if( !session ) |
| 7794 | session = ssl->session; |
| 7795 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7796 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7797 | ? "client finished" |
| 7798 | : "server finished"; |
| 7799 | |
| 7800 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7801 | sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7802 | |
| 7803 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7804 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7805 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7806 | if( status != PSA_SUCCESS ) |
| 7807 | { |
| 7808 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7809 | return; |
| 7810 | } |
| 7811 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7812 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7813 | if( status != PSA_SUCCESS ) |
| 7814 | { |
| 7815 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7816 | return; |
| 7817 | } |
| 7818 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7819 | #else |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7820 | mbedtls_sha512_init( &sha512 ); |
| 7821 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7822 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7823 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7824 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7825 | |
| 7826 | /* |
| 7827 | * TLSv1.2: |
| 7828 | * hash = PRF( master, finished_label, |
| 7829 | * Hash( handshake ) )[0.11] |
| 7830 | */ |
| 7831 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7832 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7833 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7834 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7835 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7836 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7837 | mbedtls_sha512_finish_ret( &sha512, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7838 | mbedtls_sha512_free( &sha512 ); |
| 7839 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7840 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7841 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7842 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7843 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7844 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7845 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7846 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7847 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7848 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7849 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7850 | #endif /* MBEDTLS_SHA512_C */ |
| 7851 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7852 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7853 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7854 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7855 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7856 | |
| 7857 | /* |
| 7858 | * Free our handshake params |
| 7859 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 7860 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7861 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7862 | ssl->handshake = NULL; |
| 7863 | |
| 7864 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7865 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7866 | */ |
| 7867 | if( ssl->transform ) |
| 7868 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7869 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7870 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7871 | } |
| 7872 | ssl->transform = ssl->transform_negotiate; |
| 7873 | ssl->transform_negotiate = NULL; |
| 7874 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7875 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7876 | } |
| 7877 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7878 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7879 | { |
| 7880 | int resume = ssl->handshake->resume; |
| 7881 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7882 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7883 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7884 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7885 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7886 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7887 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7888 | ssl->renego_records_seen = 0; |
| 7889 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7890 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7891 | |
| 7892 | /* |
| 7893 | * Free the previous session and switch in the current one |
| 7894 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7895 | if( ssl->session ) |
| 7896 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7897 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 7898 | /* RFC 7366 3.1: keep the EtM state */ |
| 7899 | ssl->session_negotiate->encrypt_then_mac = |
| 7900 | ssl->session->encrypt_then_mac; |
| 7901 | #endif |
| 7902 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7903 | mbedtls_ssl_session_free( ssl->session ); |
| 7904 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7905 | } |
| 7906 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7907 | ssl->session_negotiate = NULL; |
| 7908 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7909 | /* |
| 7910 | * Add cache entry |
| 7911 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7912 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 7913 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7914 | resume == 0 ) |
| 7915 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7916 | 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] | 7917 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7918 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7919 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7920 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7921 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7922 | ssl->handshake->flight != NULL ) |
| 7923 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 7924 | /* Cancel handshake timer */ |
| 7925 | ssl_set_timer( ssl, 0 ); |
| 7926 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7927 | /* Keep last flight around in case we need to resend it: |
| 7928 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7929 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7930 | } |
| 7931 | else |
| 7932 | #endif |
| 7933 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7934 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7935 | ssl->state++; |
| 7936 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7937 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7938 | } |
| 7939 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7940 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7941 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7942 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7943 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7944 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7945 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7946 | ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 7947 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7948 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7949 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 7950 | /* |
| 7951 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7952 | * may define some other value. Currently (early 2016), no defined |
| 7953 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7954 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7955 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7956 | hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7957 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7958 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7959 | ssl->verify_data_len = hash_len; |
| 7960 | 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] | 7961 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7962 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7963 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7964 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7965 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7966 | |
| 7967 | /* |
| 7968 | * In case of session resuming, invert the client and server |
| 7969 | * ChangeCipherSpec messages order. |
| 7970 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7971 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7972 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7973 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7974 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7975 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7976 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7977 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7978 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7979 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7980 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7981 | } |
| 7982 | else |
| 7983 | ssl->state++; |
| 7984 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7985 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 7986 | * Switch to our negotiated transform and session parameters for outbound |
| 7987 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7988 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7989 | 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] | 7990 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7991 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7992 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7993 | { |
| 7994 | unsigned char i; |
| 7995 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7996 | /* Remember current epoch settings for resending */ |
| 7997 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7998 | 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] | 7999 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8000 | /* Set sequence_number to zero */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8001 | memset( ssl->cur_out_ctr + 2, 0, 6 ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8002 | |
| 8003 | /* Increment epoch */ |
| 8004 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8005 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8006 | break; |
| 8007 | |
| 8008 | /* The loop goes to its end iff the counter is wrapping */ |
| 8009 | if( i == 0 ) |
| 8010 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8011 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 8012 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8013 | } |
| 8014 | } |
| 8015 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8016 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8017 | memset( ssl->cur_out_ctr, 0, 8 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8018 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8019 | ssl->transform_out = ssl->transform_negotiate; |
| 8020 | ssl->session_out = ssl->session_negotiate; |
| 8021 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8022 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 8023 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 8024 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8025 | 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] | 8026 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8027 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 8028 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 8029 | } |
| 8030 | } |
| 8031 | #endif |
| 8032 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8033 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8034 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8035 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 8036 | #endif |
| 8037 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 8038 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8039 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 8040 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8041 | return( ret ); |
| 8042 | } |
| 8043 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 8044 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8045 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8046 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 8047 | { |
| 8048 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 8049 | return( ret ); |
| 8050 | } |
| 8051 | #endif |
| 8052 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8053 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8054 | |
| 8055 | return( 0 ); |
| 8056 | } |
| 8057 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8058 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8059 | #define SSL_MAX_HASH_LEN 36 |
| 8060 | #else |
| 8061 | #define SSL_MAX_HASH_LEN 12 |
| 8062 | #endif |
| 8063 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8064 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8065 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 8066 | int ret; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8067 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8068 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8069 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8070 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8071 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8072 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8073 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 8074 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8075 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8076 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8077 | return( ret ); |
| 8078 | } |
| 8079 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8080 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8081 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8082 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 8083 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8084 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8085 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8086 | } |
| 8087 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8088 | /* 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] | 8089 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 8090 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8091 | hash_len = 36; |
| 8092 | else |
| 8093 | #endif |
| 8094 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8095 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8096 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || |
| 8097 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8098 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8099 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 8100 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8101 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8102 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8103 | } |
| 8104 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8105 | 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] | 8106 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8107 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8108 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 8109 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8110 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8111 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8112 | } |
| 8113 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8114 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8115 | ssl->verify_data_len = hash_len; |
| 8116 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8117 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8118 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8119 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8120 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8121 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8122 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8123 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8124 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8125 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8126 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8127 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8128 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8129 | } |
| 8130 | else |
| 8131 | ssl->state++; |
| 8132 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8133 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8134 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8135 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8136 | #endif |
| 8137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8138 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8139 | |
| 8140 | return( 0 ); |
| 8141 | } |
| 8142 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8143 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8144 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8145 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8147 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 8148 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 8149 | mbedtls_md5_init( &handshake->fin_md5 ); |
| 8150 | mbedtls_sha1_init( &handshake->fin_sha1 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8151 | mbedtls_md5_starts_ret( &handshake->fin_md5 ); |
| 8152 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8153 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8154 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 8155 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8156 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8157 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 8158 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 8159 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8160 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8161 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8162 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8163 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8164 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8165 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 8166 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 8167 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8168 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8169 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8170 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8171 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 8172 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8173 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8174 | |
| 8175 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 8176 | |
| 8177 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 8178 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 8179 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 8180 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8181 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8182 | #if defined(MBEDTLS_DHM_C) |
| 8183 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8184 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8185 | #if defined(MBEDTLS_ECDH_C) |
| 8186 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8187 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8188 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8189 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 8190 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8191 | handshake->ecjpake_cache = NULL; |
| 8192 | handshake->ecjpake_cache_len = 0; |
| 8193 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8194 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8195 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 8196 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 8197 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 8198 | #endif |
| 8199 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8200 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8201 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 8202 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 8203 | |
| 8204 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 8205 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8206 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 8207 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8208 | } |
| 8209 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 8210 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8211 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8212 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 8213 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8214 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 8215 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 8216 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 8217 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8218 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 8219 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 8220 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8221 | } |
| 8222 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8223 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8224 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8225 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8226 | } |
| 8227 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8228 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8229 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8230 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8231 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8232 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8233 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8234 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8235 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 8236 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8237 | |
| 8238 | /* |
| 8239 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 8240 | * Now allocate missing structures. |
| 8241 | */ |
| 8242 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8243 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8244 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8245 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8246 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8247 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8248 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8249 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8250 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8251 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 8252 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8253 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8254 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8255 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8256 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8257 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8258 | if( ssl->handshake == NULL || |
| 8259 | ssl->transform_negotiate == NULL || |
| 8260 | ssl->session_negotiate == NULL ) |
| 8261 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 8262 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8264 | mbedtls_free( ssl->handshake ); |
| 8265 | mbedtls_free( ssl->transform_negotiate ); |
| 8266 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8267 | |
| 8268 | ssl->handshake = NULL; |
| 8269 | ssl->transform_negotiate = NULL; |
| 8270 | ssl->session_negotiate = NULL; |
| 8271 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8272 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8273 | } |
| 8274 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8275 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8276 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 8277 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 8278 | ssl_handshake_params_init( ssl->handshake ); |
| 8279 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8280 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8281 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8282 | { |
| 8283 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8284 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8285 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 8286 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 8287 | else |
| 8288 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8289 | |
| 8290 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8291 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8292 | #endif |
| 8293 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8294 | return( 0 ); |
| 8295 | } |
| 8296 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8297 | #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] | 8298 | /* Dummy cookie callbacks for defaults */ |
| 8299 | static int ssl_cookie_write_dummy( void *ctx, |
| 8300 | unsigned char **p, unsigned char *end, |
| 8301 | const unsigned char *cli_id, size_t cli_id_len ) |
| 8302 | { |
| 8303 | ((void) ctx); |
| 8304 | ((void) p); |
| 8305 | ((void) end); |
| 8306 | ((void) cli_id); |
| 8307 | ((void) cli_id_len); |
| 8308 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8309 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8310 | } |
| 8311 | |
| 8312 | static int ssl_cookie_check_dummy( void *ctx, |
| 8313 | const unsigned char *cookie, size_t cookie_len, |
| 8314 | const unsigned char *cli_id, size_t cli_id_len ) |
| 8315 | { |
| 8316 | ((void) ctx); |
| 8317 | ((void) cookie); |
| 8318 | ((void) cookie_len); |
| 8319 | ((void) cli_id); |
| 8320 | ((void) cli_id_len); |
| 8321 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8322 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8323 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8324 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8325 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8326 | /* Once ssl->out_hdr as the address of the beginning of the |
| 8327 | * next outgoing record is set, deduce the other pointers. |
| 8328 | * |
| 8329 | * Note: For TLS, we save the implicit record sequence number |
| 8330 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 8331 | * and the caller has to make sure there's space for this. |
| 8332 | */ |
| 8333 | |
| 8334 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 8335 | mbedtls_ssl_transform *transform ) |
| 8336 | { |
| 8337 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8338 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8339 | { |
| 8340 | ssl->out_ctr = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8341 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8342 | ssl->out_cid = ssl->out_ctr + 8; |
| 8343 | ssl->out_len = ssl->out_cid; |
| 8344 | if( transform != NULL ) |
| 8345 | ssl->out_len += transform->out_cid_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8346 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8347 | ssl->out_len = ssl->out_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8348 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8349 | ssl->out_iv = ssl->out_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8350 | } |
| 8351 | else |
| 8352 | #endif |
| 8353 | { |
| 8354 | ssl->out_ctr = ssl->out_hdr - 8; |
| 8355 | ssl->out_len = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8356 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8357 | ssl->out_cid = ssl->out_len; |
| 8358 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8359 | ssl->out_iv = ssl->out_hdr + 5; |
| 8360 | } |
| 8361 | |
| 8362 | /* Adjust out_msg to make space for explicit IV, if used. */ |
| 8363 | if( transform != NULL && |
| 8364 | ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 8365 | { |
| 8366 | ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen; |
| 8367 | } |
| 8368 | else |
| 8369 | ssl->out_msg = ssl->out_iv; |
| 8370 | } |
| 8371 | |
| 8372 | /* Once ssl->in_hdr as the address of the beginning of the |
| 8373 | * next incoming record is set, deduce the other pointers. |
| 8374 | * |
| 8375 | * Note: For TLS, we save the implicit record sequence number |
| 8376 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 8377 | * and the caller has to make sure there's space for this. |
| 8378 | */ |
| 8379 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8380 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8381 | { |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8382 | /* This function sets the pointers to match the case |
| 8383 | * of unprotected TLS/DTLS records, with both ssl->in_iv |
| 8384 | * and ssl->in_msg pointing to the beginning of the record |
| 8385 | * content. |
| 8386 | * |
| 8387 | * When decrypting a protected record, ssl->in_msg |
| 8388 | * will be shifted to point to the beginning of the |
| 8389 | * record plaintext. |
| 8390 | */ |
| 8391 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8392 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8393 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8394 | { |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8395 | /* This sets the header pointers to match records |
| 8396 | * without CID. When we receive a record containing |
| 8397 | * a CID, the fields are shifted accordingly in |
| 8398 | * ssl_parse_record_header(). */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8399 | ssl->in_ctr = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8400 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8401 | ssl->in_cid = ssl->in_ctr + 8; |
| 8402 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8403 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8404 | ssl->in_len = ssl->in_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8405 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8406 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8407 | } |
| 8408 | else |
| 8409 | #endif |
| 8410 | { |
| 8411 | ssl->in_ctr = ssl->in_hdr - 8; |
| 8412 | ssl->in_len = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8413 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8414 | ssl->in_cid = ssl->in_len; |
| 8415 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8416 | ssl->in_iv = ssl->in_hdr + 5; |
| 8417 | } |
| 8418 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8419 | /* This will be adjusted at record decryption time. */ |
| 8420 | ssl->in_msg = ssl->in_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8421 | } |
| 8422 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8423 | /* |
| 8424 | * Initialize an SSL context |
| 8425 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 8426 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 8427 | { |
| 8428 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 8429 | } |
| 8430 | |
| 8431 | /* |
| 8432 | * Setup an SSL context |
| 8433 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8434 | |
| 8435 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ) |
| 8436 | { |
| 8437 | /* Set the incoming and outgoing record pointers. */ |
| 8438 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8439 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8440 | { |
| 8441 | ssl->out_hdr = ssl->out_buf; |
| 8442 | ssl->in_hdr = ssl->in_buf; |
| 8443 | } |
| 8444 | else |
| 8445 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8446 | { |
| 8447 | ssl->out_hdr = ssl->out_buf + 8; |
| 8448 | ssl->in_hdr = ssl->in_buf + 8; |
| 8449 | } |
| 8450 | |
| 8451 | /* Derive other internal pointers. */ |
| 8452 | ssl_update_out_pointers( ssl, NULL /* no transform enabled */ ); |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8453 | ssl_update_in_pointers ( ssl ); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8454 | } |
| 8455 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8456 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 8457 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8458 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8459 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8460 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8461 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8462 | |
| 8463 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 8464 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8465 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 8466 | |
| 8467 | /* Set to NULL in case of an error condition */ |
| 8468 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8469 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8470 | ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 8471 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8472 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8473 | 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] | 8474 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8475 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8476 | } |
| 8477 | |
| 8478 | ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 8479 | if( ssl->out_buf == NULL ) |
| 8480 | { |
| 8481 | 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] | 8482 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8483 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8484 | } |
| 8485 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8486 | ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 8487 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8488 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8489 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8490 | |
| 8491 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8492 | |
| 8493 | error: |
| 8494 | mbedtls_free( ssl->in_buf ); |
| 8495 | mbedtls_free( ssl->out_buf ); |
| 8496 | |
| 8497 | ssl->conf = NULL; |
| 8498 | |
| 8499 | ssl->in_buf = NULL; |
| 8500 | ssl->out_buf = NULL; |
| 8501 | |
| 8502 | ssl->in_hdr = NULL; |
| 8503 | ssl->in_ctr = NULL; |
| 8504 | ssl->in_len = NULL; |
| 8505 | ssl->in_iv = NULL; |
| 8506 | ssl->in_msg = NULL; |
| 8507 | |
| 8508 | ssl->out_hdr = NULL; |
| 8509 | ssl->out_ctr = NULL; |
| 8510 | ssl->out_len = NULL; |
| 8511 | ssl->out_iv = NULL; |
| 8512 | ssl->out_msg = NULL; |
| 8513 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 8514 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8515 | } |
| 8516 | |
| 8517 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8518 | * Reset an initialized and used SSL context for re-use while retaining |
| 8519 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8520 | * |
| 8521 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 8522 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8523 | */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8524 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8525 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8526 | int ret; |
| 8527 | |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 8528 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \ |
| 8529 | !defined(MBEDTLS_SSL_SRV_C) |
| 8530 | ((void) partial); |
| 8531 | #endif |
| 8532 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8533 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8534 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8535 | /* Cancel any possibly running timer */ |
| 8536 | ssl_set_timer( ssl, 0 ); |
| 8537 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8538 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8539 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8540 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8541 | |
| 8542 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8543 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 8544 | 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] | 8545 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8546 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8547 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8548 | ssl->in_offt = NULL; |
Hanno Becker | f29d470 | 2018-08-10 11:31:15 +0100 | [diff] [blame] | 8549 | ssl_reset_in_out_pointers( ssl ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8550 | |
| 8551 | ssl->in_msgtype = 0; |
| 8552 | ssl->in_msglen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8553 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8554 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 8555 | ssl->in_epoch = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8556 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8557 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 8558 | ssl_dtls_replay_reset( ssl ); |
| 8559 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8560 | |
| 8561 | ssl->in_hslen = 0; |
| 8562 | ssl->nb_zero = 0; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 8563 | |
| 8564 | ssl->keep_current_message = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8565 | |
| 8566 | ssl->out_msgtype = 0; |
| 8567 | ssl->out_msglen = 0; |
| 8568 | ssl->out_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8569 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 8570 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 8571 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8572 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8573 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8574 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 8575 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8576 | ssl->transform_in = NULL; |
| 8577 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8578 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 8579 | ssl->session_in = NULL; |
| 8580 | ssl->session_out = NULL; |
| 8581 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8582 | memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8583 | |
| 8584 | #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] | 8585 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8586 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 8587 | { |
| 8588 | ssl->in_left = 0; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8589 | memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8590 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8592 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 8593 | if( mbedtls_ssl_hw_record_reset != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8594 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8595 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); |
| 8596 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8597 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8598 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); |
| 8599 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8600 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8601 | } |
| 8602 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8603 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8604 | if( ssl->transform ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8605 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8606 | mbedtls_ssl_transform_free( ssl->transform ); |
| 8607 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8608 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8609 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8610 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8611 | if( ssl->session ) |
| 8612 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8613 | mbedtls_ssl_session_free( ssl->session ); |
| 8614 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8615 | ssl->session = NULL; |
| 8616 | } |
| 8617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8618 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8619 | ssl->alpn_chosen = NULL; |
| 8620 | #endif |
| 8621 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8622 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8623 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8624 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8625 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8626 | { |
| 8627 | mbedtls_free( ssl->cli_id ); |
| 8628 | ssl->cli_id = NULL; |
| 8629 | ssl->cli_id_len = 0; |
| 8630 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 8631 | #endif |
| 8632 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8633 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 8634 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8635 | |
| 8636 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8637 | } |
| 8638 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 8639 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8640 | * Reset an initialized and used SSL context for re-use while retaining |
| 8641 | * all application-set variables, function pointers and data. |
| 8642 | */ |
| 8643 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 8644 | { |
| 8645 | return( ssl_session_reset_int( ssl, 0 ) ); |
| 8646 | } |
| 8647 | |
| 8648 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8649 | * SSL set accessors |
| 8650 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8651 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8652 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8653 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8654 | } |
| 8655 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8656 | 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] | 8657 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8658 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 8659 | } |
| 8660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8661 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8662 | 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] | 8663 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8664 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 8665 | } |
| 8666 | #endif |
| 8667 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8668 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8669 | 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] | 8670 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8671 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8672 | } |
| 8673 | #endif |
| 8674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8675 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8676 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 8677 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 8678 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8679 | { |
| 8680 | ssl->disable_datagram_packing = !allow_packing; |
| 8681 | } |
| 8682 | |
| 8683 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 8684 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8685 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8686 | conf->hs_timeout_min = min; |
| 8687 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8688 | } |
| 8689 | #endif |
| 8690 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8691 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8692 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8693 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8694 | } |
| 8695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8696 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8697 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 8698 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8699 | void *p_vrfy ) |
| 8700 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8701 | conf->f_vrfy = f_vrfy; |
| 8702 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8703 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8704 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8705 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8706 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 8707 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8708 | void *p_rng ) |
| 8709 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 8710 | conf->f_rng = f_rng; |
| 8711 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8712 | } |
| 8713 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8714 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 8715 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8716 | void *p_dbg ) |
| 8717 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8718 | conf->f_dbg = f_dbg; |
| 8719 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8720 | } |
| 8721 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8722 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8723 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8724 | mbedtls_ssl_send_t *f_send, |
| 8725 | mbedtls_ssl_recv_t *f_recv, |
| 8726 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8727 | { |
| 8728 | ssl->p_bio = p_bio; |
| 8729 | ssl->f_send = f_send; |
| 8730 | ssl->f_recv = f_recv; |
| 8731 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 8732 | } |
| 8733 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 8734 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8735 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 8736 | { |
| 8737 | ssl->mtu = mtu; |
| 8738 | } |
| 8739 | #endif |
| 8740 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8741 | 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] | 8742 | { |
| 8743 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8744 | } |
| 8745 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8746 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 8747 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8748 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 8749 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8750 | { |
| 8751 | ssl->p_timer = p_timer; |
| 8752 | ssl->f_set_timer = f_set_timer; |
| 8753 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8754 | |
| 8755 | /* Make sure we start with no timer running */ |
| 8756 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8757 | } |
| 8758 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8759 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8760 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8761 | void *p_cache, |
| 8762 | int (*f_get_cache)(void *, mbedtls_ssl_session *), |
| 8763 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8764 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8765 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8766 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8767 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8768 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8769 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8770 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8771 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8772 | 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] | 8773 | { |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8774 | int ret; |
| 8775 | |
| 8776 | if( ssl == NULL || |
| 8777 | session == NULL || |
| 8778 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8779 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8780 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8781 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8782 | } |
| 8783 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 8784 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 8785 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8786 | return( ret ); |
| 8787 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8788 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8789 | |
| 8790 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8791 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8792 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8793 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8794 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8795 | const int *ciphersuites ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8796 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8797 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; |
| 8798 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; |
| 8799 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; |
| 8800 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8801 | } |
| 8802 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8803 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 8804 | const int *ciphersuites, |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8805 | int major, int minor ) |
| 8806 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8807 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8808 | return; |
| 8809 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8810 | 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] | 8811 | return; |
| 8812 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8813 | conf->ciphersuite_list[minor] = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8814 | } |
| 8815 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8816 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8817 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 8818 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8819 | { |
| 8820 | conf->cert_profile = profile; |
| 8821 | } |
| 8822 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8823 | /* Append a new keycert entry to a (possibly empty) list */ |
| 8824 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 8825 | mbedtls_x509_crt *cert, |
| 8826 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8827 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8828 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8829 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8830 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 8831 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8832 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8833 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8834 | new_cert->cert = cert; |
| 8835 | new_cert->key = key; |
| 8836 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8837 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8838 | /* Update head is the list was null, else add to the end */ |
| 8839 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8840 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8841 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8842 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8843 | else |
| 8844 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8845 | mbedtls_ssl_key_cert *cur = *head; |
| 8846 | while( cur->next != NULL ) |
| 8847 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8848 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8849 | } |
| 8850 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8851 | return( 0 ); |
| 8852 | } |
| 8853 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8854 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8855 | mbedtls_x509_crt *own_cert, |
| 8856 | mbedtls_pk_context *pk_key ) |
| 8857 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 8858 | 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] | 8859 | } |
| 8860 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8861 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8862 | mbedtls_x509_crt *ca_chain, |
| 8863 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8864 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8865 | conf->ca_chain = ca_chain; |
| 8866 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 8867 | |
| 8868 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 8869 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 8870 | * cannot be used together. */ |
| 8871 | conf->f_ca_cb = NULL; |
| 8872 | conf->p_ca_cb = NULL; |
| 8873 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8874 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 8875 | |
| 8876 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 8877 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 8878 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 8879 | void *p_ca_cb ) |
| 8880 | { |
| 8881 | conf->f_ca_cb = f_ca_cb; |
| 8882 | conf->p_ca_cb = p_ca_cb; |
| 8883 | |
| 8884 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 8885 | * cannot be used together. */ |
| 8886 | conf->ca_chain = NULL; |
| 8887 | conf->ca_crl = NULL; |
| 8888 | } |
| 8889 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8890 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 8891 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8892 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8893 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 8894 | mbedtls_x509_crt *own_cert, |
| 8895 | mbedtls_pk_context *pk_key ) |
| 8896 | { |
| 8897 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 8898 | own_cert, pk_key ) ); |
| 8899 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 8900 | |
| 8901 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 8902 | mbedtls_x509_crt *ca_chain, |
| 8903 | mbedtls_x509_crl *ca_crl ) |
| 8904 | { |
| 8905 | ssl->handshake->sni_ca_chain = ca_chain; |
| 8906 | ssl->handshake->sni_ca_crl = ca_crl; |
| 8907 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8908 | |
| 8909 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 8910 | int authmode ) |
| 8911 | { |
| 8912 | ssl->handshake->sni_authmode = authmode; |
| 8913 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8914 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8915 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 8916 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8917 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 8918 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 8919 | void *p_vrfy ) |
| 8920 | { |
| 8921 | ssl->f_vrfy = f_vrfy; |
| 8922 | ssl->p_vrfy = p_vrfy; |
| 8923 | } |
| 8924 | #endif |
| 8925 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8926 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8927 | /* |
| 8928 | * Set EC J-PAKE password for current handshake |
| 8929 | */ |
| 8930 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 8931 | const unsigned char *pw, |
| 8932 | size_t pw_len ) |
| 8933 | { |
| 8934 | mbedtls_ecjpake_role role; |
| 8935 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 8936 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8937 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8938 | |
| 8939 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 8940 | role = MBEDTLS_ECJPAKE_SERVER; |
| 8941 | else |
| 8942 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 8943 | |
| 8944 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 8945 | role, |
| 8946 | MBEDTLS_MD_SHA256, |
| 8947 | MBEDTLS_ECP_DP_SECP256R1, |
| 8948 | pw, pw_len ) ); |
| 8949 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8950 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8951 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8952 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 8953 | |
| 8954 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 8955 | { |
| 8956 | /* Remove reference to existing PSK, if any. */ |
| 8957 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8958 | if( conf->psk_opaque != 0 ) |
| 8959 | { |
| 8960 | /* The maintenance of the PSK key slot is the |
| 8961 | * user's responsibility. */ |
| 8962 | conf->psk_opaque = 0; |
| 8963 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 8964 | /* This and the following branch should never |
| 8965 | * be taken simultaenously as we maintain the |
| 8966 | * invariant that raw and opaque PSKs are never |
| 8967 | * configured simultaneously. As a safeguard, |
| 8968 | * though, `else` is omitted here. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 8969 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8970 | if( conf->psk != NULL ) |
| 8971 | { |
| 8972 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 8973 | |
| 8974 | mbedtls_free( conf->psk ); |
| 8975 | conf->psk = NULL; |
| 8976 | conf->psk_len = 0; |
| 8977 | } |
| 8978 | |
| 8979 | /* Remove reference to PSK identity, if any. */ |
| 8980 | if( conf->psk_identity != NULL ) |
| 8981 | { |
| 8982 | mbedtls_free( conf->psk_identity ); |
| 8983 | conf->psk_identity = NULL; |
| 8984 | conf->psk_identity_len = 0; |
| 8985 | } |
| 8986 | } |
| 8987 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 8988 | /* This function assumes that PSK identity in the SSL config is unset. |
| 8989 | * It checks that the provided identity is well-formed and attempts |
| 8990 | * to make a copy of it in the SSL config. |
| 8991 | * On failure, the PSK identity in the config remains unset. */ |
| 8992 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 8993 | unsigned char const *psk_identity, |
| 8994 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 8995 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 8996 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 8997 | if( psk_identity == NULL || |
| 8998 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8999 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 9000 | { |
| 9001 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9002 | } |
| 9003 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9004 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 9005 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 9006 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9007 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 9008 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 9009 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 9010 | |
| 9011 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9012 | } |
| 9013 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9014 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 9015 | const unsigned char *psk, size_t psk_len, |
| 9016 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 9017 | { |
| 9018 | int ret; |
| 9019 | /* Remove opaque/raw PSK + PSK Identity */ |
| 9020 | ssl_conf_remove_psk( conf ); |
| 9021 | |
| 9022 | /* Check and set raw PSK */ |
| 9023 | if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 9024 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9025 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 9026 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9027 | conf->psk_len = psk_len; |
| 9028 | memcpy( conf->psk, psk, conf->psk_len ); |
| 9029 | |
| 9030 | /* Check and set PSK Identity */ |
| 9031 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 9032 | if( ret != 0 ) |
| 9033 | ssl_conf_remove_psk( conf ); |
| 9034 | |
| 9035 | return( ret ); |
| 9036 | } |
| 9037 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9038 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 9039 | { |
| 9040 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9041 | if( ssl->handshake->psk_opaque != 0 ) |
| 9042 | { |
| 9043 | ssl->handshake->psk_opaque = 0; |
| 9044 | } |
| 9045 | else |
| 9046 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9047 | if( ssl->handshake->psk != NULL ) |
| 9048 | { |
| 9049 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 9050 | ssl->handshake->psk_len ); |
| 9051 | mbedtls_free( ssl->handshake->psk ); |
| 9052 | ssl->handshake->psk_len = 0; |
| 9053 | } |
| 9054 | } |
| 9055 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9056 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 9057 | const unsigned char *psk, size_t psk_len ) |
| 9058 | { |
| 9059 | if( psk == NULL || ssl->handshake == NULL ) |
| 9060 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9061 | |
| 9062 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 9063 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9064 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9065 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9066 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 9067 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 9068 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9069 | |
| 9070 | ssl->handshake->psk_len = psk_len; |
| 9071 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 9072 | |
| 9073 | return( 0 ); |
| 9074 | } |
| 9075 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9076 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 9077 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 9078 | psa_key_handle_t psk_slot, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9079 | const unsigned char *psk_identity, |
| 9080 | size_t psk_identity_len ) |
| 9081 | { |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9082 | int ret; |
| 9083 | /* Clear opaque/raw PSK + PSK Identity, if present. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9084 | ssl_conf_remove_psk( conf ); |
| 9085 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9086 | /* Check and set opaque PSK */ |
| 9087 | if( psk_slot == 0 ) |
| 9088 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9089 | conf->psk_opaque = psk_slot; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 9090 | |
| 9091 | /* Check and set PSK Identity */ |
| 9092 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 9093 | psk_identity_len ); |
| 9094 | if( ret != 0 ) |
| 9095 | ssl_conf_remove_psk( conf ); |
| 9096 | |
| 9097 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9098 | } |
| 9099 | |
| 9100 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 9101 | psa_key_handle_t psk_slot ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 9102 | { |
| 9103 | if( psk_slot == 0 || ssl->handshake == NULL ) |
| 9104 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9105 | |
| 9106 | ssl_remove_psk( ssl ); |
| 9107 | ssl->handshake->psk_opaque = psk_slot; |
| 9108 | return( 0 ); |
| 9109 | } |
| 9110 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9111 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9112 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9113 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9114 | size_t), |
| 9115 | void *p_psk ) |
| 9116 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9117 | conf->f_psk = f_psk; |
| 9118 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 9119 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9120 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 9121 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 9122 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 9123 | |
| 9124 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9125 | 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] | 9126 | { |
| 9127 | int ret; |
| 9128 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9129 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || |
| 9130 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) |
| 9131 | { |
| 9132 | mbedtls_mpi_free( &conf->dhm_P ); |
| 9133 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9134 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9135 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9136 | |
| 9137 | return( 0 ); |
| 9138 | } |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 9139 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9140 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 9141 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 9142 | const unsigned char *dhm_P, size_t P_len, |
| 9143 | const unsigned char *dhm_G, size_t G_len ) |
| 9144 | { |
| 9145 | int ret; |
| 9146 | |
| 9147 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 9148 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 9149 | { |
| 9150 | mbedtls_mpi_free( &conf->dhm_P ); |
| 9151 | mbedtls_mpi_free( &conf->dhm_G ); |
| 9152 | return( ret ); |
| 9153 | } |
| 9154 | |
| 9155 | return( 0 ); |
| 9156 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9157 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9158 | 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] | 9159 | { |
| 9160 | int ret; |
| 9161 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9162 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || |
| 9163 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) |
| 9164 | { |
| 9165 | mbedtls_mpi_free( &conf->dhm_P ); |
| 9166 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9167 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9168 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9169 | |
| 9170 | return( 0 ); |
| 9171 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 9172 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9173 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 9174 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 9175 | /* |
| 9176 | * Set the minimum length for Diffie-Hellman parameters |
| 9177 | */ |
| 9178 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 9179 | unsigned int bitlen ) |
| 9180 | { |
| 9181 | conf->dhm_min_bitlen = bitlen; |
| 9182 | } |
| 9183 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 9184 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9185 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 9186 | /* |
| 9187 | * Set allowed/preferred hashes for handshake signatures |
| 9188 | */ |
| 9189 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 9190 | const int *hashes ) |
| 9191 | { |
| 9192 | conf->sig_hashes = hashes; |
| 9193 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9194 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 9195 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 9196 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9197 | /* |
| 9198 | * Set the allowed elliptic curves |
| 9199 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9200 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9201 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9202 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9203 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9204 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9205 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9206 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9207 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9208 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9209 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9210 | /* Initialize to suppress unnecessary compiler warning */ |
| 9211 | size_t hostname_len = 0; |
| 9212 | |
| 9213 | /* Check if new hostname is valid before |
| 9214 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9215 | if( hostname != NULL ) |
| 9216 | { |
| 9217 | hostname_len = strlen( hostname ); |
| 9218 | |
| 9219 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 9220 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9221 | } |
| 9222 | |
| 9223 | /* Now it's clear that we will overwrite the old hostname, |
| 9224 | * so we can free it safely */ |
| 9225 | |
| 9226 | if( ssl->hostname != NULL ) |
| 9227 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9228 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9229 | mbedtls_free( ssl->hostname ); |
| 9230 | } |
| 9231 | |
| 9232 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 9233 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9234 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9235 | { |
| 9236 | ssl->hostname = NULL; |
| 9237 | } |
| 9238 | else |
| 9239 | { |
| 9240 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9241 | if( ssl->hostname == NULL ) |
| 9242 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 9243 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9244 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 9245 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9246 | ssl->hostname[hostname_len] = '\0'; |
| 9247 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9248 | |
| 9249 | return( 0 ); |
| 9250 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 9251 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9252 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9253 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9254 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9255 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9256 | const unsigned char *, size_t), |
| 9257 | void *p_sni ) |
| 9258 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9259 | conf->f_sni = f_sni; |
| 9260 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9261 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9262 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9264 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9265 | 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] | 9266 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9267 | size_t cur_len, tot_len; |
| 9268 | const char **p; |
| 9269 | |
| 9270 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 9271 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 9272 | * MUST NOT be truncated." |
| 9273 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9274 | */ |
| 9275 | tot_len = 0; |
| 9276 | for( p = protos; *p != NULL; p++ ) |
| 9277 | { |
| 9278 | cur_len = strlen( *p ); |
| 9279 | tot_len += cur_len; |
| 9280 | |
| 9281 | if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9282 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9283 | } |
| 9284 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9285 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9286 | |
| 9287 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9288 | } |
| 9289 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9290 | 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] | 9291 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9292 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9293 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9294 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9295 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9296 | 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] | 9297 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9298 | conf->max_major_ver = major; |
| 9299 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9300 | } |
| 9301 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9302 | 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] | 9303 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9304 | conf->min_major_ver = major; |
| 9305 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9306 | } |
| 9307 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9308 | #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] | 9309 | 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] | 9310 | { |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 9311 | conf->fallback = fallback; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 9312 | } |
| 9313 | #endif |
| 9314 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 9315 | #if defined(MBEDTLS_SSL_SRV_C) |
| 9316 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 9317 | char cert_req_ca_list ) |
| 9318 | { |
| 9319 | conf->cert_req_ca_list = cert_req_ca_list; |
| 9320 | } |
| 9321 | #endif |
| 9322 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9323 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9324 | 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] | 9325 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9326 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 9327 | } |
| 9328 | #endif |
| 9329 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9330 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9331 | 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] | 9332 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9333 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 9334 | } |
| 9335 | #endif |
| 9336 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9337 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9338 | 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] | 9339 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9340 | conf->arc4_disabled = arc4; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9341 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9342 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9344 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9345 | 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] | 9346 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9347 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9348 | 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] | 9349 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9350 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9351 | } |
| 9352 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 9353 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9354 | |
| 9355 | return( 0 ); |
| 9356 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9357 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9358 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9359 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9360 | 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] | 9361 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9362 | conf->trunc_hmac = truncate; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9363 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9364 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9365 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9366 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9367 | 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] | 9368 | { |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 9369 | conf->cbc_record_splitting = split; |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 9370 | } |
| 9371 | #endif |
| 9372 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9373 | 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] | 9374 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9375 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9376 | } |
| 9377 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9378 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9379 | 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] | 9380 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9381 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 9382 | } |
| 9383 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9384 | 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] | 9385 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9386 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9387 | } |
| 9388 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9389 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9390 | const unsigned char period[8] ) |
| 9391 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9392 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9393 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9394 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9395 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9396 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9397 | #if defined(MBEDTLS_SSL_CLI_C) |
| 9398 | 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] | 9399 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 9400 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9401 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9402 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9403 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9404 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9405 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 9406 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 9407 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 9408 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9409 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9410 | conf->f_ticket_write = f_ticket_write; |
| 9411 | conf->f_ticket_parse = f_ticket_parse; |
| 9412 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9413 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9414 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9415 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9416 | |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 9417 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 9418 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, |
| 9419 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 9420 | void *p_export_keys ) |
| 9421 | { |
| 9422 | conf->f_export_keys = f_export_keys; |
| 9423 | conf->p_export_keys = p_export_keys; |
| 9424 | } |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 9425 | |
| 9426 | void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf, |
| 9427 | mbedtls_ssl_export_keys_ext_t *f_export_keys_ext, |
| 9428 | void *p_export_keys ) |
| 9429 | { |
| 9430 | conf->f_export_keys_ext = f_export_keys_ext; |
| 9431 | conf->p_export_keys = p_export_keys; |
| 9432 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 9433 | #endif |
| 9434 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9435 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9436 | void mbedtls_ssl_conf_async_private_cb( |
| 9437 | mbedtls_ssl_config *conf, |
| 9438 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 9439 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 9440 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 9441 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9442 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9443 | { |
| 9444 | conf->f_async_sign_start = f_async_sign; |
| 9445 | conf->f_async_decrypt_start = f_async_decrypt; |
| 9446 | conf->f_async_resume = f_async_resume; |
| 9447 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9448 | conf->p_async_config_data = async_config_data; |
| 9449 | } |
| 9450 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 9451 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 9452 | { |
| 9453 | return( conf->p_async_config_data ); |
| 9454 | } |
| 9455 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9456 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9457 | { |
| 9458 | if( ssl->handshake == NULL ) |
| 9459 | return( NULL ); |
| 9460 | else |
| 9461 | return( ssl->handshake->user_async_ctx ); |
| 9462 | } |
| 9463 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9464 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9465 | void *ctx ) |
| 9466 | { |
| 9467 | if( ssl->handshake != NULL ) |
| 9468 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9469 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9470 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9471 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9472 | /* |
| 9473 | * SSL get accessors |
| 9474 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9475 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9476 | { |
| 9477 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); |
| 9478 | } |
| 9479 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9480 | int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl ) |
| 9481 | { |
| 9482 | /* |
| 9483 | * Case A: We're currently holding back |
| 9484 | * a message for further processing. |
| 9485 | */ |
| 9486 | |
| 9487 | if( ssl->keep_current_message == 1 ) |
| 9488 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9489 | 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] | 9490 | return( 1 ); |
| 9491 | } |
| 9492 | |
| 9493 | /* |
| 9494 | * Case B: Further records are pending in the current datagram. |
| 9495 | */ |
| 9496 | |
| 9497 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9498 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 9499 | ssl->in_left > ssl->next_record_offset ) |
| 9500 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9501 | 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] | 9502 | return( 1 ); |
| 9503 | } |
| 9504 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9505 | |
| 9506 | /* |
| 9507 | * Case C: A handshake message is being processed. |
| 9508 | */ |
| 9509 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9510 | if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen ) |
| 9511 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9512 | 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] | 9513 | return( 1 ); |
| 9514 | } |
| 9515 | |
| 9516 | /* |
| 9517 | * Case D: An application data message is being processed |
| 9518 | */ |
| 9519 | if( ssl->in_offt != NULL ) |
| 9520 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9521 | 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] | 9522 | return( 1 ); |
| 9523 | } |
| 9524 | |
| 9525 | /* |
| 9526 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 9527 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9528 | * we implement support for multiple alerts in single records. |
| 9529 | */ |
| 9530 | |
| 9531 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) ); |
| 9532 | return( 0 ); |
| 9533 | } |
| 9534 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 9535 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9536 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 9537 | if( ssl->session != NULL ) |
| 9538 | return( ssl->session->verify_result ); |
| 9539 | |
| 9540 | if( ssl->session_negotiate != NULL ) |
| 9541 | return( ssl->session_negotiate->verify_result ); |
| 9542 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 9543 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9544 | } |
| 9545 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9546 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9547 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9548 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9549 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9550 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9551 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9552 | } |
| 9553 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9554 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9555 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9556 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9557 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9558 | { |
| 9559 | switch( ssl->minor_ver ) |
| 9560 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9561 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9562 | return( "DTLSv1.0" ); |
| 9563 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9564 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9565 | return( "DTLSv1.2" ); |
| 9566 | |
| 9567 | default: |
| 9568 | return( "unknown (DTLS)" ); |
| 9569 | } |
| 9570 | } |
| 9571 | #endif |
| 9572 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9573 | switch( ssl->minor_ver ) |
| 9574 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9575 | case MBEDTLS_SSL_MINOR_VERSION_0: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9576 | return( "SSLv3.0" ); |
| 9577 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9578 | case MBEDTLS_SSL_MINOR_VERSION_1: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9579 | return( "TLSv1.0" ); |
| 9580 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9581 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9582 | return( "TLSv1.1" ); |
| 9583 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9584 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 9585 | return( "TLSv1.2" ); |
| 9586 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9587 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9588 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9589 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9590 | } |
| 9591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9592 | 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] | 9593 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9594 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9595 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9596 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9597 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9598 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl ); |
| 9599 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9600 | if( transform == NULL ) |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9601 | return( (int) out_hdr_len ); |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9602 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9603 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 9604 | if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL ) |
| 9605 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9606 | #endif |
| 9607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9608 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9609 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9610 | case MBEDTLS_MODE_GCM: |
| 9611 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9612 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9613 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9614 | transform_expansion = transform->minlen; |
| 9615 | break; |
| 9616 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9617 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9618 | |
| 9619 | block_size = mbedtls_cipher_get_block_size( |
| 9620 | &transform->cipher_ctx_enc ); |
| 9621 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9622 | /* Expansion due to the addition of the MAC. */ |
| 9623 | transform_expansion += transform->maclen; |
| 9624 | |
| 9625 | /* Expansion due to the addition of CBC padding; |
| 9626 | * Theoretically up to 256 bytes, but we never use |
| 9627 | * more than the block size of the underlying cipher. */ |
| 9628 | transform_expansion += block_size; |
| 9629 | |
| 9630 | /* For TLS 1.1 or higher, an explicit IV is added |
| 9631 | * after the record header. */ |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9632 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9633 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9634 | transform_expansion += block_size; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9635 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9636 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9637 | break; |
| 9638 | |
| 9639 | default: |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 9640 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9641 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9642 | } |
| 9643 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9644 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9645 | if( transform->out_cid_len != 0 ) |
| 9646 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9647 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9648 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9649 | return( (int)( out_hdr_len + transform_expansion ) ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9650 | } |
| 9651 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9652 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9653 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 9654 | { |
| 9655 | size_t max_len; |
| 9656 | |
| 9657 | /* |
| 9658 | * Assume mfl_code is correct since it was checked when set |
| 9659 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9660 | 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] | 9661 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9662 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9663 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9664 | 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] | 9665 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9666 | 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] | 9667 | } |
| 9668 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9669 | /* During a handshake, use the value being negotiated */ |
| 9670 | if( ssl->session_negotiate != NULL && |
| 9671 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 9672 | { |
| 9673 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 9674 | } |
| 9675 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9676 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9677 | } |
| 9678 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 9679 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9680 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9681 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) |
| 9682 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9683 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 9684 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 9685 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 9686 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 9687 | return ( 0 ); |
| 9688 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9689 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 9690 | return( ssl->mtu ); |
| 9691 | |
| 9692 | if( ssl->mtu == 0 ) |
| 9693 | return( ssl->handshake->mtu ); |
| 9694 | |
| 9695 | return( ssl->mtu < ssl->handshake->mtu ? |
| 9696 | ssl->mtu : ssl->handshake->mtu ); |
| 9697 | } |
| 9698 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9699 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9700 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 9701 | { |
| 9702 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 9703 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 9704 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9705 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9706 | (void) ssl; |
| 9707 | #endif |
| 9708 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9709 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9710 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 9711 | |
| 9712 | if( max_len > mfl ) |
| 9713 | max_len = mfl; |
| 9714 | #endif |
| 9715 | |
| 9716 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9717 | if( ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9718 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9719 | const size_t mtu = ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9720 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 9721 | const size_t overhead = (size_t) ret; |
| 9722 | |
| 9723 | if( ret < 0 ) |
| 9724 | return( ret ); |
| 9725 | |
| 9726 | if( mtu <= overhead ) |
| 9727 | { |
| 9728 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 9729 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 9730 | } |
| 9731 | |
| 9732 | if( max_len > mtu - overhead ) |
| 9733 | max_len = mtu - overhead; |
| 9734 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9735 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9736 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 9737 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9738 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9739 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9740 | #endif |
| 9741 | |
| 9742 | return( (int) max_len ); |
| 9743 | } |
| 9744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9745 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9746 | 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] | 9747 | { |
| 9748 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9749 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9750 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9751 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9752 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9753 | #else |
| 9754 | return( NULL ); |
| 9755 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9756 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9757 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9758 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9759 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 9760 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 9761 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9762 | { |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9763 | if( ssl == NULL || |
| 9764 | dst == NULL || |
| 9765 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9766 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9767 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9768 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9769 | } |
| 9770 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 9771 | return( mbedtls_ssl_session_copy( dst, ssl->session ) ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9772 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9773 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9774 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9775 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9776 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9777 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9778 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9779 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9780 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9781 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9782 | if( ssl == NULL || ssl->conf == NULL ) |
| 9783 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9784 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9785 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9786 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9787 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9788 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9789 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9790 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9791 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9792 | #endif |
| 9793 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9794 | return( ret ); |
| 9795 | } |
| 9796 | |
| 9797 | /* |
| 9798 | * Perform the SSL handshake |
| 9799 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9800 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9801 | { |
| 9802 | int ret = 0; |
| 9803 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9804 | if( ssl == NULL || ssl->conf == NULL ) |
| 9805 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9807 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9808 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9809 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9810 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9811 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9812 | |
| 9813 | if( ret != 0 ) |
| 9814 | break; |
| 9815 | } |
| 9816 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9817 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9818 | |
| 9819 | return( ret ); |
| 9820 | } |
| 9821 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9822 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 9823 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9824 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9825 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9826 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9827 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9828 | { |
| 9829 | int ret; |
| 9830 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9831 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9832 | |
| 9833 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9834 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 9835 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9836 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 9837 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9838 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 9839 | 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] | 9840 | return( ret ); |
| 9841 | } |
| 9842 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9843 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9844 | |
| 9845 | return( 0 ); |
| 9846 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9847 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9848 | |
| 9849 | /* |
| 9850 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9851 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 9852 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 9853 | * - 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] | 9854 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9855 | * 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] | 9856 | * 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] | 9857 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9858 | static int ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9859 | { |
| 9860 | int ret; |
| 9861 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9862 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9863 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9864 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 9865 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9866 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 9867 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 9868 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9869 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9870 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9871 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 9872 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9873 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 9874 | ssl->handshake->out_msg_seq = 1; |
| 9875 | else |
| 9876 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 9877 | } |
| 9878 | #endif |
| 9879 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9880 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 9881 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9882 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9883 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9884 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9885 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9886 | return( ret ); |
| 9887 | } |
| 9888 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9889 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9890 | |
| 9891 | return( 0 ); |
| 9892 | } |
| 9893 | |
| 9894 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9895 | * Renegotiate current connection on client, |
| 9896 | * or request renegotiation on server |
| 9897 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9898 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9899 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9900 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9901 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9902 | if( ssl == NULL || ssl->conf == NULL ) |
| 9903 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9904 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9905 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9906 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9907 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9908 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9909 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 9910 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9911 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9912 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 9913 | |
| 9914 | /* Did we already try/start sending HelloRequest? */ |
| 9915 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9916 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 9917 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9918 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9919 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9920 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9921 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9922 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9923 | /* |
| 9924 | * On client, either start the renegotiation process or, |
| 9925 | * if already in progress, continue the handshake |
| 9926 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9927 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9928 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9929 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 9930 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9931 | |
| 9932 | if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 ) |
| 9933 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9934 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9935 | return( ret ); |
| 9936 | } |
| 9937 | } |
| 9938 | else |
| 9939 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9940 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9941 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9942 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9943 | return( ret ); |
| 9944 | } |
| 9945 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9946 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9947 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 9948 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9949 | } |
| 9950 | |
| 9951 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9952 | * Check record counters and renegotiate if they're above the limit. |
| 9953 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9954 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9955 | { |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9956 | size_t ep_len = ssl_ep_len( ssl ); |
| 9957 | int in_ctr_cmp; |
| 9958 | int out_ctr_cmp; |
| 9959 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9960 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
| 9961 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9962 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9963 | { |
| 9964 | return( 0 ); |
| 9965 | } |
| 9966 | |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9967 | in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, |
| 9968 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 9969 | out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len, |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9970 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
| 9971 | |
| 9972 | if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9973 | { |
| 9974 | return( 0 ); |
| 9975 | } |
| 9976 | |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 9977 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9978 | return( mbedtls_ssl_renegotiate( ssl ) ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9979 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9980 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9981 | |
| 9982 | /* |
| 9983 | * Receive application data decrypted from the SSL layer |
| 9984 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9985 | 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] | 9986 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9987 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 9988 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9989 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9990 | if( ssl == NULL || ssl->conf == NULL ) |
| 9991 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9992 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9993 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9994 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9995 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9996 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 9997 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9998 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 9999 | return( ret ); |
| 10000 | |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10001 | if( ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10002 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10003 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 10004 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10005 | return( ret ); |
| 10006 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10007 | } |
| 10008 | #endif |
| 10009 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10010 | /* |
| 10011 | * Check if renegotiation is necessary and/or handshake is |
| 10012 | * in process. If yes, perform/continue, and fall through |
| 10013 | * if an unexpected packet is received while the client |
| 10014 | * is waiting for the ServerHello. |
| 10015 | * |
| 10016 | * (There is no equivalent to the last condition on |
| 10017 | * the server-side as it is not treated as within |
| 10018 | * a handshake while waiting for the ClientHello |
| 10019 | * after a renegotiation request.) |
| 10020 | */ |
| 10021 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10022 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10023 | ret = ssl_check_ctr_renegotiate( ssl ); |
| 10024 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10025 | ret != 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10026 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10027 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10028 | return( ret ); |
| 10029 | } |
| 10030 | #endif |
| 10031 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10032 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10033 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10034 | ret = mbedtls_ssl_handshake( ssl ); |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10035 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10036 | ret != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10037 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10038 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10039 | return( ret ); |
| 10040 | } |
| 10041 | } |
| 10042 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 10043 | /* Loop as long as no application data record is available */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10044 | while( ssl->in_offt == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10045 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10046 | /* Start timer if not already running */ |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10047 | if( ssl->f_get_timer != NULL && |
| 10048 | ssl->f_get_timer( ssl->p_timer ) == -1 ) |
| 10049 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10050 | ssl_set_timer( ssl, ssl->conf->read_timeout ); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10051 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10052 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10053 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10054 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10055 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
| 10056 | return( 0 ); |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10057 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10058 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 10059 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10060 | } |
| 10061 | |
| 10062 | if( ssl->in_msglen == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10063 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10064 | { |
| 10065 | /* |
| 10066 | * OpenSSL sends empty messages to randomize the IV |
| 10067 | */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10068 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10069 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10070 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10071 | return( 0 ); |
| 10072 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10073 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10074 | return( ret ); |
| 10075 | } |
| 10076 | } |
| 10077 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10078 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10079 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10080 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10081 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10082 | /* |
| 10083 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 10084 | * - For server-side, expect CLIENT_HELLO. |
| 10085 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 10086 | */ |
| 10087 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10088 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10089 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10090 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10091 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10092 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10093 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10094 | |
| 10095 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10096 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10097 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10098 | { |
| 10099 | continue; |
| 10100 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10101 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10102 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10103 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10104 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10105 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10106 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10107 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10108 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10109 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10110 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10111 | |
| 10112 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10113 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10114 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10115 | { |
| 10116 | continue; |
| 10117 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10118 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10119 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10120 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10121 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 10122 | |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10123 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10124 | /* Determine whether renegotiation attempt should be accepted */ |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10125 | if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 10126 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 10127 | ssl->conf->allow_legacy_renegotiation == |
| 10128 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) ) |
| 10129 | { |
| 10130 | /* |
| 10131 | * Accept renegotiation request |
| 10132 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10133 | |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10134 | /* DTLS clients need to know renego is server-initiated */ |
| 10135 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10136 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 10137 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 10138 | { |
| 10139 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 10140 | } |
| 10141 | #endif |
| 10142 | ret = ssl_start_renegotiation( ssl ); |
| 10143 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10144 | ret != 0 ) |
| 10145 | { |
| 10146 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
| 10147 | return( ret ); |
| 10148 | } |
| 10149 | } |
| 10150 | else |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10151 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10152 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10153 | /* |
| 10154 | * Refuse renegotiation |
| 10155 | */ |
| 10156 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10157 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10159 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 10160 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10161 | { |
Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 10162 | /* SSLv3 does not have a "no_renegotiation" warning, so |
| 10163 | we send a fatal alert and abort the connection. */ |
| 10164 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 10165 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 10166 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10167 | } |
| 10168 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10169 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 10170 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 10171 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10172 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10173 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10174 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 10175 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 10176 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10177 | { |
| 10178 | return( ret ); |
| 10179 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10180 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 10181 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10182 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || |
| 10183 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 10184 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10185 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 10186 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 10187 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10188 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10189 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10190 | /* At this point, we don't know whether the renegotiation has been |
| 10191 | * completed or not. The cases to consider are the following: |
| 10192 | * 1) The renegotiation is complete. In this case, no new record |
| 10193 | * has been read yet. |
| 10194 | * 2) The renegotiation is incomplete because the client received |
| 10195 | * an application data record while awaiting the ServerHello. |
| 10196 | * 3) The renegotiation is incomplete because the client received |
| 10197 | * a non-handshake, non-application data message while awaiting |
| 10198 | * the ServerHello. |
| 10199 | * In each of these case, looping will be the proper action: |
| 10200 | * - For 1), the next iteration will read a new record and check |
| 10201 | * if it's application data. |
| 10202 | * - For 2), the loop condition isn't satisfied as application data |
| 10203 | * is present, hence continue is the same as break |
| 10204 | * - For 3), the loop condition is satisfied and read_record |
| 10205 | * will re-deliver the message that was held back by the client |
| 10206 | * when expecting the ServerHello. |
| 10207 | */ |
| 10208 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10209 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10210 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10211 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 10212 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10213 | if( ssl->conf->renego_max_records >= 0 ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 10214 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10215 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10216 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10217 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10218 | "but not honored by client" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10219 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10220 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 10221 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 10222 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10223 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10224 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10225 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
| 10226 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10227 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10228 | 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] | 10229 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10230 | } |
| 10231 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10232 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10233 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10234 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); |
| 10235 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10236 | } |
| 10237 | |
| 10238 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10239 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 10240 | /* We're going to return something now, cancel timer, |
| 10241 | * except if handshake (renegotiation) is in progress */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10242 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 10243 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10244 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 10245 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10246 | /* If we requested renego but received AppData, resend HelloRequest. |
| 10247 | * Do it now, after setting in_offt, to avoid taking this branch |
| 10248 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10249 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10250 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10251 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10252 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10253 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10254 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10255 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10256 | return( ret ); |
| 10257 | } |
| 10258 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10259 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10260 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10261 | } |
| 10262 | |
| 10263 | n = ( len < ssl->in_msglen ) |
| 10264 | ? len : ssl->in_msglen; |
| 10265 | |
| 10266 | memcpy( buf, ssl->in_offt, n ); |
| 10267 | ssl->in_msglen -= n; |
| 10268 | |
| 10269 | if( ssl->in_msglen == 0 ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10270 | { |
| 10271 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10272 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 10273 | ssl->keep_current_message = 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10274 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10275 | else |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10276 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10277 | /* more data available */ |
| 10278 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10279 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10281 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10282 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 10283 | return( (int) n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10284 | } |
| 10285 | |
| 10286 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10287 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 10288 | * fragment length and buffer size. |
| 10289 | * |
| 10290 | * According to RFC 5246 Section 6.2.1: |
| 10291 | * |
| 10292 | * Zero-length fragments of Application data MAY be sent as they are |
| 10293 | * potentially useful as a traffic analysis countermeasure. |
| 10294 | * |
| 10295 | * Therefore, it is possible that the input message length is 0 and the |
| 10296 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10297 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10298 | static int ssl_write_real( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10299 | const unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10300 | { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 10301 | int ret = mbedtls_ssl_get_max_out_record_payload( ssl ); |
| 10302 | const size_t max_len = (size_t) ret; |
| 10303 | |
| 10304 | if( ret < 0 ) |
| 10305 | { |
| 10306 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret ); |
| 10307 | return( ret ); |
| 10308 | } |
| 10309 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10310 | if( len > max_len ) |
| 10311 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10312 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10313 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10314 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10315 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10316 | "maximum fragment length: %d > %d", |
| 10317 | len, max_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10318 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10319 | } |
| 10320 | else |
| 10321 | #endif |
| 10322 | len = max_len; |
| 10323 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10324 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10325 | if( ssl->out_left != 0 ) |
| 10326 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10327 | /* |
| 10328 | * The user has previously tried to send the data and |
| 10329 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 10330 | * written. In this case, we expect the high-level write function |
| 10331 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 10332 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10333 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10334 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10335 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10336 | return( ret ); |
| 10337 | } |
| 10338 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10339 | else |
Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 10340 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10341 | /* |
| 10342 | * The user is trying to send a message the first time, so we need to |
| 10343 | * copy the data into the internal buffers and setup the data structure |
| 10344 | * to keep track of partial writes |
| 10345 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10346 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10347 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10348 | memcpy( ssl->out_msg, buf, len ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10349 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 10350 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10351 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10352 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10353 | return( ret ); |
| 10354 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10355 | } |
| 10356 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10357 | return( (int) len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10358 | } |
| 10359 | |
| 10360 | /* |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10361 | * Write application data, doing 1/n-1 splitting if necessary. |
| 10362 | * |
| 10363 | * 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] | 10364 | * then the caller will call us again with the same arguments, so |
Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 10365 | * remember whether we already did the split or not. |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10366 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10367 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10368 | static int ssl_write_split( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10369 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10370 | { |
| 10371 | int ret; |
| 10372 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 10373 | if( ssl->conf->cbc_record_splitting == |
| 10374 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 10375 | len <= 1 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10376 | ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 || |
| 10377 | mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) |
| 10378 | != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10379 | { |
| 10380 | return( ssl_write_real( ssl, buf, len ) ); |
| 10381 | } |
| 10382 | |
| 10383 | if( ssl->split_done == 0 ) |
| 10384 | { |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10385 | if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10386 | return( ret ); |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10387 | ssl->split_done = 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10388 | } |
| 10389 | |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10390 | if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) |
| 10391 | return( ret ); |
| 10392 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10393 | |
| 10394 | return( ret + 1 ); |
| 10395 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10396 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10397 | |
| 10398 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10399 | * Write application data (public-facing wrapper) |
| 10400 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10401 | 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] | 10402 | { |
| 10403 | int ret; |
| 10404 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10405 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10406 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10407 | if( ssl == NULL || ssl->conf == NULL ) |
| 10408 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10409 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10410 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10411 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) |
| 10412 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10413 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10414 | return( ret ); |
| 10415 | } |
| 10416 | #endif |
| 10417 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10418 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10419 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10420 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10421 | { |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 10422 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10423 | return( ret ); |
| 10424 | } |
| 10425 | } |
| 10426 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10427 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10428 | ret = ssl_write_split( ssl, buf, len ); |
| 10429 | #else |
| 10430 | ret = ssl_write_real( ssl, buf, len ); |
| 10431 | #endif |
| 10432 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10433 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10434 | |
| 10435 | return( ret ); |
| 10436 | } |
| 10437 | |
| 10438 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10439 | * Notify the peer that the connection is being closed |
| 10440 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10441 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10442 | { |
| 10443 | int ret; |
| 10444 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10445 | if( ssl == NULL || ssl->conf == NULL ) |
| 10446 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10447 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10448 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10449 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 10450 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10451 | return( mbedtls_ssl_flush_output( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10452 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10453 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10454 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10455 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 10456 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 10457 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10458 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10459 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10460 | return( ret ); |
| 10461 | } |
| 10462 | } |
| 10463 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10464 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10465 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 10466 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10467 | } |
| 10468 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10469 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10470 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10471 | if( transform == NULL ) |
| 10472 | return; |
| 10473 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10474 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10475 | deflateEnd( &transform->ctx_deflate ); |
| 10476 | inflateEnd( &transform->ctx_inflate ); |
| 10477 | #endif |
| 10478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10479 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); |
| 10480 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 10481 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 10482 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10483 | mbedtls_md_free( &transform->md_ctx_enc ); |
| 10484 | mbedtls_md_free( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 10485 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10486 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10487 | mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10488 | } |
| 10489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10490 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10491 | 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] | 10492 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10493 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10494 | |
| 10495 | while( cur != NULL ) |
| 10496 | { |
| 10497 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10498 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10499 | cur = next; |
| 10500 | } |
| 10501 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10502 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10503 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10504 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10505 | |
| 10506 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ) |
| 10507 | { |
| 10508 | unsigned offset; |
| 10509 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 10510 | |
| 10511 | if( hs == NULL ) |
| 10512 | return; |
| 10513 | |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 10514 | ssl_free_buffered_record( ssl ); |
| 10515 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10516 | for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10517 | ssl_buffering_free_slot( ssl, offset ); |
| 10518 | } |
| 10519 | |
| 10520 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 10521 | uint8_t slot ) |
| 10522 | { |
| 10523 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 10524 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 10525 | |
| 10526 | if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 10527 | return; |
| 10528 | |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10529 | if( hs_buf->is_valid == 1 ) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10530 | { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10531 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Hanno Becker | 805f2e1 | 2018-10-12 16:31:41 +0100 | [diff] [blame] | 10532 | mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len ); |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10533 | mbedtls_free( hs_buf->data ); |
| 10534 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10535 | } |
| 10536 | } |
| 10537 | |
| 10538 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 10539 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10540 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10541 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10542 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 10543 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10544 | if( handshake == NULL ) |
| 10545 | return; |
| 10546 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 10547 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 10548 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 10549 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 10550 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 10551 | handshake->async_in_progress = 0; |
| 10552 | } |
| 10553 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 10554 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10555 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 10556 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 10557 | mbedtls_md5_free( &handshake->fin_md5 ); |
| 10558 | mbedtls_sha1_free( &handshake->fin_sha1 ); |
| 10559 | #endif |
| 10560 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10561 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 10562 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 10563 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 10564 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10565 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 10566 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 10567 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10568 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 10569 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 10570 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 10571 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10572 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 10573 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 10574 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10575 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 10576 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10577 | #if defined(MBEDTLS_DHM_C) |
| 10578 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10579 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10580 | #if defined(MBEDTLS_ECDH_C) |
| 10581 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10582 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 10583 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 10584 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 10585 | #if defined(MBEDTLS_SSL_CLI_C) |
| 10586 | mbedtls_free( handshake->ecjpake_cache ); |
| 10587 | handshake->ecjpake_cache = NULL; |
| 10588 | handshake->ecjpake_cache_len = 0; |
| 10589 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 10590 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10591 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 10592 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 10593 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 10594 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10595 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 10596 | #endif |
| 10597 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 10598 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 10599 | if( handshake->psk != NULL ) |
| 10600 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10601 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 10602 | mbedtls_free( handshake->psk ); |
| 10603 | } |
| 10604 | #endif |
| 10605 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10606 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 10607 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10608 | /* |
| 10609 | * Free only the linked list wrapper, not the keys themselves |
| 10610 | * since the belong to the SNI callback |
| 10611 | */ |
| 10612 | if( handshake->sni_key_cert != NULL ) |
| 10613 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10614 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10615 | |
| 10616 | while( cur != NULL ) |
| 10617 | { |
| 10618 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10619 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10620 | cur = next; |
| 10621 | } |
| 10622 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10623 | #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] | 10624 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 10625 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 10626 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 10627 | if( handshake->ecrs_peer_cert != NULL ) |
| 10628 | { |
| 10629 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 10630 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 10631 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 10632 | #endif |
| 10633 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 10634 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 10635 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10636 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 10637 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10638 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10639 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10640 | mbedtls_free( handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 10641 | ssl_flight_free( handshake->flight ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10642 | ssl_buffering_free( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 10643 | #endif |
| 10644 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 10645 | #if defined(MBEDTLS_ECDH_C) && \ |
| 10646 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 10647 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 10648 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 10649 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10650 | mbedtls_platform_zeroize( handshake, |
| 10651 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10652 | } |
| 10653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10654 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10655 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10656 | if( session == NULL ) |
| 10657 | return; |
| 10658 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10659 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 10660 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 10661 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 10662 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 10663 | #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] | 10664 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 10665 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 10666 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10667 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10668 | } |
| 10669 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10670 | /* |
| 10671 | * Free an SSL context |
| 10672 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10673 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10674 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10675 | if( ssl == NULL ) |
| 10676 | return; |
| 10677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10678 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10679 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 10680 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10681 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 10682 | 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] | 10683 | mbedtls_free( ssl->out_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10684 | } |
| 10685 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 10686 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10687 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 10688 | 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] | 10689 | mbedtls_free( ssl->in_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10690 | } |
| 10691 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10692 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 10693 | if( ssl->compress_buf != NULL ) |
| 10694 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 10695 | 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] | 10696 | mbedtls_free( ssl->compress_buf ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 10697 | } |
| 10698 | #endif |
| 10699 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10700 | if( ssl->transform ) |
| 10701 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10702 | mbedtls_ssl_transform_free( ssl->transform ); |
| 10703 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10704 | } |
| 10705 | |
| 10706 | if( ssl->handshake ) |
| 10707 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10708 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10709 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 10710 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10711 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10712 | mbedtls_free( ssl->handshake ); |
| 10713 | mbedtls_free( ssl->transform_negotiate ); |
| 10714 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10715 | } |
| 10716 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 10717 | if( ssl->session ) |
| 10718 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10719 | mbedtls_ssl_session_free( ssl->session ); |
| 10720 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 10721 | } |
| 10722 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 10723 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 10724 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10725 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10726 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10727 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10728 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 10729 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10730 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10731 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 10732 | if( mbedtls_ssl_hw_record_finish != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 10733 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10734 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); |
| 10735 | mbedtls_ssl_hw_record_finish( ssl ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 10736 | } |
| 10737 | #endif |
| 10738 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 10739 | #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] | 10740 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 10741 | #endif |
| 10742 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10743 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 10744 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 10745 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10746 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10747 | } |
| 10748 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10749 | /* |
| 10750 | * Initialze mbedtls_ssl_config |
| 10751 | */ |
| 10752 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 10753 | { |
| 10754 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 10755 | } |
| 10756 | |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 10757 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 10758 | static int ssl_preset_default_hashes[] = { |
| 10759 | #if defined(MBEDTLS_SHA512_C) |
| 10760 | MBEDTLS_MD_SHA512, |
| 10761 | MBEDTLS_MD_SHA384, |
| 10762 | #endif |
| 10763 | #if defined(MBEDTLS_SHA256_C) |
| 10764 | MBEDTLS_MD_SHA256, |
| 10765 | MBEDTLS_MD_SHA224, |
| 10766 | #endif |
Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 10767 | #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] | 10768 | MBEDTLS_MD_SHA1, |
| 10769 | #endif |
| 10770 | MBEDTLS_MD_NONE |
| 10771 | }; |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 10772 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 10773 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10774 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 10775 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 10776 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 10777 | 0 |
| 10778 | }; |
| 10779 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10780 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10781 | static int ssl_preset_suiteb_hashes[] = { |
| 10782 | MBEDTLS_MD_SHA256, |
| 10783 | MBEDTLS_MD_SHA384, |
| 10784 | MBEDTLS_MD_NONE |
| 10785 | }; |
| 10786 | #endif |
| 10787 | |
| 10788 | #if defined(MBEDTLS_ECP_C) |
| 10789 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 10790 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10791 | MBEDTLS_ECP_DP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 10792 | #endif |
| 10793 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10794 | MBEDTLS_ECP_DP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 10795 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10796 | MBEDTLS_ECP_DP_NONE |
| 10797 | }; |
| 10798 | #endif |
| 10799 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10800 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 10801 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10802 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 10803 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10804 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10805 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 10806 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10807 | int ret; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 10808 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10809 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 10810 | /* Use the functions here so that they are covered in tests, |
| 10811 | * but otherwise access member directly for efficiency */ |
| 10812 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 10813 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10814 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10815 | /* |
| 10816 | * Things that are common to all presets |
| 10817 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 10818 | #if defined(MBEDTLS_SSL_CLI_C) |
| 10819 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 10820 | { |
| 10821 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 10822 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 10823 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 10824 | #endif |
| 10825 | } |
| 10826 | #endif |
| 10827 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 10828 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10829 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 10830 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10831 | |
| 10832 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 10833 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 10834 | #endif |
| 10835 | |
| 10836 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 10837 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 10838 | #endif |
| 10839 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 10840 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 10841 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; |
| 10842 | #endif |
| 10843 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 10844 | #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] | 10845 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 10846 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 10847 | #endif |
| 10848 | |
| 10849 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 10850 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 10851 | #endif |
| 10852 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 10853 | #if defined(MBEDTLS_SSL_SRV_C) |
| 10854 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
| 10855 | #endif |
| 10856 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10857 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10858 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 10859 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 10860 | #endif |
| 10861 | |
| 10862 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 10863 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10864 | memset( conf->renego_period, 0x00, 2 ); |
| 10865 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10866 | #endif |
| 10867 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10868 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 10869 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 10870 | { |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 10871 | const unsigned char dhm_p[] = |
| 10872 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 10873 | const unsigned char dhm_g[] = |
| 10874 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
| 10875 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 10876 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 10877 | dhm_p, sizeof( dhm_p ), |
| 10878 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10879 | { |
| 10880 | return( ret ); |
| 10881 | } |
| 10882 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 10883 | #endif |
| 10884 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10885 | /* |
| 10886 | * Preset-specific defaults |
| 10887 | */ |
| 10888 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10889 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10890 | /* |
| 10891 | * NSA Suite B |
| 10892 | */ |
| 10893 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 10894 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 10895 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 10896 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 10897 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 10898 | |
| 10899 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 10900 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 10901 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 10902 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 10903 | ssl_preset_suiteb_ciphersuites; |
| 10904 | |
| 10905 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10906 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10907 | #endif |
| 10908 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10909 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10910 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
| 10911 | #endif |
| 10912 | |
| 10913 | #if defined(MBEDTLS_ECP_C) |
| 10914 | conf->curve_list = ssl_preset_suiteb_curves; |
| 10915 | #endif |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 10916 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10917 | |
| 10918 | /* |
| 10919 | * Default |
| 10920 | */ |
| 10921 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 10922 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 10923 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 10924 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 10925 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 10926 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 10927 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 10928 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 10929 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10930 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 10931 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 10932 | |
| 10933 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10934 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 10935 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 10936 | #endif |
| 10937 | |
| 10938 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 10939 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 10940 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 10941 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 10942 | mbedtls_ssl_list_ciphersuites(); |
| 10943 | |
| 10944 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10945 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 10946 | #endif |
| 10947 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10948 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 10949 | conf->sig_hashes = ssl_preset_default_hashes; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10950 | #endif |
| 10951 | |
| 10952 | #if defined(MBEDTLS_ECP_C) |
| 10953 | conf->curve_list = mbedtls_ecp_grp_id_list(); |
| 10954 | #endif |
| 10955 | |
| 10956 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 10957 | conf->dhm_min_bitlen = 1024; |
| 10958 | #endif |
| 10959 | } |
| 10960 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10961 | return( 0 ); |
| 10962 | } |
| 10963 | |
| 10964 | /* |
| 10965 | * Free mbedtls_ssl_config |
| 10966 | */ |
| 10967 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 10968 | { |
| 10969 | #if defined(MBEDTLS_DHM_C) |
| 10970 | mbedtls_mpi_free( &conf->dhm_P ); |
| 10971 | mbedtls_mpi_free( &conf->dhm_G ); |
| 10972 | #endif |
| 10973 | |
| 10974 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 10975 | if( conf->psk != NULL ) |
| 10976 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10977 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10978 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 10979 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10980 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 10981 | } |
| 10982 | |
| 10983 | if( conf->psk_identity != NULL ) |
| 10984 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10985 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 10986 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 10987 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10988 | conf->psk_identity_len = 0; |
| 10989 | } |
| 10990 | #endif |
| 10991 | |
| 10992 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10993 | ssl_key_cert_free( conf->key_cert ); |
| 10994 | #endif |
| 10995 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10996 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10997 | } |
| 10998 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 10999 | #if defined(MBEDTLS_PK_C) && \ |
| 11000 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11001 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11002 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11003 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11004 | 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] | 11005 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11006 | #if defined(MBEDTLS_RSA_C) |
| 11007 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 11008 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11009 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11010 | #if defined(MBEDTLS_ECDSA_C) |
| 11011 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 11012 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11013 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11014 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11015 | } |
| 11016 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 11017 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 11018 | { |
| 11019 | switch( type ) { |
| 11020 | case MBEDTLS_PK_RSA: |
| 11021 | return( MBEDTLS_SSL_SIG_RSA ); |
| 11022 | case MBEDTLS_PK_ECDSA: |
| 11023 | case MBEDTLS_PK_ECKEY: |
| 11024 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 11025 | default: |
| 11026 | return( MBEDTLS_SSL_SIG_ANON ); |
| 11027 | } |
| 11028 | } |
| 11029 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11030 | 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] | 11031 | { |
| 11032 | switch( sig ) |
| 11033 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11034 | #if defined(MBEDTLS_RSA_C) |
| 11035 | case MBEDTLS_SSL_SIG_RSA: |
| 11036 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11037 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11038 | #if defined(MBEDTLS_ECDSA_C) |
| 11039 | case MBEDTLS_SSL_SIG_ECDSA: |
| 11040 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11041 | #endif |
| 11042 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11043 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11044 | } |
| 11045 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 11046 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11047 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 11048 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 11049 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 11050 | |
| 11051 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 11052 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 11053 | mbedtls_pk_type_t sig_alg ) |
| 11054 | { |
| 11055 | switch( sig_alg ) |
| 11056 | { |
| 11057 | case MBEDTLS_PK_RSA: |
| 11058 | return( set->rsa ); |
| 11059 | case MBEDTLS_PK_ECDSA: |
| 11060 | return( set->ecdsa ); |
| 11061 | default: |
| 11062 | return( MBEDTLS_MD_NONE ); |
| 11063 | } |
| 11064 | } |
| 11065 | |
| 11066 | /* Add a signature-hash-pair to a signature-hash set */ |
| 11067 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 11068 | mbedtls_pk_type_t sig_alg, |
| 11069 | mbedtls_md_type_t md_alg ) |
| 11070 | { |
| 11071 | switch( sig_alg ) |
| 11072 | { |
| 11073 | case MBEDTLS_PK_RSA: |
| 11074 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 11075 | set->rsa = md_alg; |
| 11076 | break; |
| 11077 | |
| 11078 | case MBEDTLS_PK_ECDSA: |
| 11079 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 11080 | set->ecdsa = md_alg; |
| 11081 | break; |
| 11082 | |
| 11083 | default: |
| 11084 | break; |
| 11085 | } |
| 11086 | } |
| 11087 | |
| 11088 | /* Allow exactly one hash algorithm for each signature. */ |
| 11089 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 11090 | mbedtls_md_type_t md_alg ) |
| 11091 | { |
| 11092 | set->rsa = md_alg; |
| 11093 | set->ecdsa = md_alg; |
| 11094 | } |
| 11095 | |
| 11096 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
| 11097 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 11098 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 11099 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11100 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 11101 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11102 | 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] | 11103 | { |
| 11104 | switch( hash ) |
| 11105 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11106 | #if defined(MBEDTLS_MD5_C) |
| 11107 | case MBEDTLS_SSL_HASH_MD5: |
| 11108 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11109 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11110 | #if defined(MBEDTLS_SHA1_C) |
| 11111 | case MBEDTLS_SSL_HASH_SHA1: |
| 11112 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11113 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11114 | #if defined(MBEDTLS_SHA256_C) |
| 11115 | case MBEDTLS_SSL_HASH_SHA224: |
| 11116 | return( MBEDTLS_MD_SHA224 ); |
| 11117 | case MBEDTLS_SSL_HASH_SHA256: |
| 11118 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11119 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11120 | #if defined(MBEDTLS_SHA512_C) |
| 11121 | case MBEDTLS_SSL_HASH_SHA384: |
| 11122 | return( MBEDTLS_MD_SHA384 ); |
| 11123 | case MBEDTLS_SSL_HASH_SHA512: |
| 11124 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11125 | #endif |
| 11126 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11127 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11128 | } |
| 11129 | } |
| 11130 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11131 | /* |
| 11132 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 11133 | */ |
| 11134 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 11135 | { |
| 11136 | switch( md ) |
| 11137 | { |
| 11138 | #if defined(MBEDTLS_MD5_C) |
| 11139 | case MBEDTLS_MD_MD5: |
| 11140 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 11141 | #endif |
| 11142 | #if defined(MBEDTLS_SHA1_C) |
| 11143 | case MBEDTLS_MD_SHA1: |
| 11144 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 11145 | #endif |
| 11146 | #if defined(MBEDTLS_SHA256_C) |
| 11147 | case MBEDTLS_MD_SHA224: |
| 11148 | return( MBEDTLS_SSL_HASH_SHA224 ); |
| 11149 | case MBEDTLS_MD_SHA256: |
| 11150 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 11151 | #endif |
| 11152 | #if defined(MBEDTLS_SHA512_C) |
| 11153 | case MBEDTLS_MD_SHA384: |
| 11154 | return( MBEDTLS_SSL_HASH_SHA384 ); |
| 11155 | case MBEDTLS_MD_SHA512: |
| 11156 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 11157 | #endif |
| 11158 | default: |
| 11159 | return( MBEDTLS_SSL_HASH_NONE ); |
| 11160 | } |
| 11161 | } |
| 11162 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 11163 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11164 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11165 | * 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] | 11166 | * 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] | 11167 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 11168 | 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] | 11169 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11170 | const mbedtls_ecp_group_id *gid; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11171 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 11172 | if( ssl->conf->curve_list == NULL ) |
| 11173 | return( -1 ); |
| 11174 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 11175 | 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] | 11176 | if( *gid == grp_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 11177 | return( 0 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11178 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 11179 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11180 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 11181 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11182 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11183 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11184 | /* |
| 11185 | * Check if a hash proposed by the peer is in our list. |
| 11186 | * Return 0 if we're willing to use it, -1 otherwise. |
| 11187 | */ |
| 11188 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 11189 | mbedtls_md_type_t md ) |
| 11190 | { |
| 11191 | const int *cur; |
| 11192 | |
| 11193 | if( ssl->conf->sig_hashes == NULL ) |
| 11194 | return( -1 ); |
| 11195 | |
| 11196 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) |
| 11197 | if( *cur == (int) md ) |
| 11198 | return( 0 ); |
| 11199 | |
| 11200 | return( -1 ); |
| 11201 | } |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11202 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11203 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11204 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 11205 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 11206 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11207 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 11208 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11209 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11210 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11211 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11212 | int usage = 0; |
| 11213 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11214 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11215 | const char *ext_oid; |
| 11216 | size_t ext_len; |
| 11217 | #endif |
| 11218 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11219 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \ |
| 11220 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11221 | ((void) cert); |
| 11222 | ((void) cert_endpoint); |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11223 | ((void) flags); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11224 | #endif |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11225 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11226 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 11227 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11228 | { |
| 11229 | /* Server part of the key exchange */ |
| 11230 | switch( ciphersuite->key_exchange ) |
| 11231 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11232 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 11233 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11234 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11235 | break; |
| 11236 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11237 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 11238 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 11239 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 11240 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11241 | break; |
| 11242 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11243 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 11244 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11245 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11246 | break; |
| 11247 | |
| 11248 | /* 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] | 11249 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 11250 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 11251 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 11252 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 11253 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11254 | usage = 0; |
| 11255 | } |
| 11256 | } |
| 11257 | else |
| 11258 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11259 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 11260 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11261 | } |
| 11262 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11263 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11264 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11265 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11266 | ret = -1; |
| 11267 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11268 | #else |
| 11269 | ((void) ciphersuite); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11270 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11271 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11272 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 11273 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11274 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11275 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 11276 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11277 | } |
| 11278 | else |
| 11279 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11280 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 11281 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11282 | } |
| 11283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11284 | 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] | 11285 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11286 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11287 | ret = -1; |
| 11288 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11289 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11290 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11291 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11292 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11293 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 11294 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11295 | /* |
| 11296 | * Convert version numbers to/from wire format |
| 11297 | * and, for DTLS, to/from TLS equivalent. |
| 11298 | * |
| 11299 | * For TLS this is the identity. |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 11300 | * 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] | 11301 | * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1) |
| 11302 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
| 11303 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11304 | 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] | 11305 | unsigned char ver[2] ) |
| 11306 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11307 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11308 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11309 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11310 | if( minor == MBEDTLS_SSL_MINOR_VERSION_2 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11311 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 11312 | |
| 11313 | ver[0] = (unsigned char)( 255 - ( major - 2 ) ); |
| 11314 | ver[1] = (unsigned char)( 255 - ( minor - 1 ) ); |
| 11315 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 11316 | else |
| 11317 | #else |
| 11318 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11319 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 11320 | { |
| 11321 | ver[0] = (unsigned char) major; |
| 11322 | ver[1] = (unsigned char) minor; |
| 11323 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11324 | } |
| 11325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11326 | 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] | 11327 | const unsigned char ver[2] ) |
| 11328 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11329 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11330 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11331 | { |
| 11332 | *major = 255 - ver[0] + 2; |
| 11333 | *minor = 255 - ver[1] + 1; |
| 11334 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11335 | if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11336 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 11337 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 11338 | else |
| 11339 | #else |
| 11340 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11341 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 11342 | { |
| 11343 | *major = ver[0]; |
| 11344 | *minor = ver[1]; |
| 11345 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11346 | } |
| 11347 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 11348 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 11349 | { |
| 11350 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 11351 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 11352 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 11353 | |
| 11354 | switch( md ) |
| 11355 | { |
| 11356 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 11357 | #if defined(MBEDTLS_MD5_C) |
| 11358 | case MBEDTLS_SSL_HASH_MD5: |
Janos Follath | 182013f | 2016-10-25 10:50:22 +0100 | [diff] [blame] | 11359 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 11360 | #endif |
| 11361 | #if defined(MBEDTLS_SHA1_C) |
| 11362 | case MBEDTLS_SSL_HASH_SHA1: |
| 11363 | ssl->handshake->calc_verify = ssl_calc_verify_tls; |
| 11364 | break; |
| 11365 | #endif |
| 11366 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 11367 | #if defined(MBEDTLS_SHA512_C) |
| 11368 | case MBEDTLS_SSL_HASH_SHA384: |
| 11369 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 11370 | break; |
| 11371 | #endif |
| 11372 | #if defined(MBEDTLS_SHA256_C) |
| 11373 | case MBEDTLS_SSL_HASH_SHA256: |
| 11374 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 11375 | break; |
| 11376 | #endif |
| 11377 | default: |
| 11378 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 11379 | } |
| 11380 | |
| 11381 | return 0; |
| 11382 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 11383 | (void) ssl; |
| 11384 | (void) md; |
| 11385 | |
| 11386 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 11387 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 11388 | } |
| 11389 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11390 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 11391 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 11392 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, |
| 11393 | unsigned char *output, |
| 11394 | unsigned char *data, size_t data_len ) |
| 11395 | { |
| 11396 | int ret = 0; |
| 11397 | mbedtls_md5_context mbedtls_md5; |
| 11398 | mbedtls_sha1_context mbedtls_sha1; |
| 11399 | |
| 11400 | mbedtls_md5_init( &mbedtls_md5 ); |
| 11401 | mbedtls_sha1_init( &mbedtls_sha1 ); |
| 11402 | |
| 11403 | /* |
| 11404 | * digitally-signed struct { |
| 11405 | * opaque md5_hash[16]; |
| 11406 | * opaque sha_hash[20]; |
| 11407 | * }; |
| 11408 | * |
| 11409 | * md5_hash |
| 11410 | * MD5(ClientHello.random + ServerHello.random |
| 11411 | * + ServerParams); |
| 11412 | * sha_hash |
| 11413 | * SHA(ClientHello.random + ServerHello.random |
| 11414 | * + ServerParams); |
| 11415 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11416 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11417 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11418 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11419 | goto exit; |
| 11420 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11421 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11422 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 11423 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11424 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11425 | goto exit; |
| 11426 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11427 | 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] | 11428 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11429 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11430 | goto exit; |
| 11431 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11432 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11433 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11434 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11435 | goto exit; |
| 11436 | } |
| 11437 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11438 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11439 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11440 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11441 | goto exit; |
| 11442 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11443 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11444 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 11445 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11446 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11447 | goto exit; |
| 11448 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11449 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11450 | data_len ) ) != 0 ) |
| 11451 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11452 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11453 | goto exit; |
| 11454 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11455 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11456 | output + 16 ) ) != 0 ) |
| 11457 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11458 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11459 | goto exit; |
| 11460 | } |
| 11461 | |
| 11462 | exit: |
| 11463 | mbedtls_md5_free( &mbedtls_md5 ); |
| 11464 | mbedtls_sha1_free( &mbedtls_sha1 ); |
| 11465 | |
| 11466 | if( ret != 0 ) |
| 11467 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 11468 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 11469 | |
| 11470 | return( ret ); |
| 11471 | |
| 11472 | } |
| 11473 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 11474 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 11475 | |
| 11476 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 11477 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11478 | |
| 11479 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 11480 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 11481 | unsigned char *hash, size_t *hashlen, |
| 11482 | unsigned char *data, size_t data_len, |
| 11483 | mbedtls_md_type_t md_alg ) |
| 11484 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11485 | psa_status_t status; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 11486 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11487 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 11488 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 11489 | 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] | 11490 | |
| 11491 | if( ( status = psa_hash_setup( &hash_operation, |
| 11492 | hash_alg ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11493 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11494 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11495 | goto exit; |
| 11496 | } |
| 11497 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11498 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 11499 | 64 ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11500 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11501 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11502 | goto exit; |
| 11503 | } |
| 11504 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11505 | if( ( status = psa_hash_update( &hash_operation, |
| 11506 | data, data_len ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11507 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11508 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11509 | goto exit; |
| 11510 | } |
| 11511 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11512 | if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE, |
| 11513 | hashlen ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11514 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11515 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11516 | goto exit; |
| 11517 | } |
| 11518 | |
| 11519 | exit: |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11520 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11521 | { |
| 11522 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 11523 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11524 | switch( status ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11525 | { |
| 11526 | case PSA_ERROR_NOT_SUPPORTED: |
| 11527 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11528 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11529 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 11530 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 11531 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 11532 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 11533 | default: |
| 11534 | return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED ); |
| 11535 | } |
| 11536 | } |
| 11537 | return( 0 ); |
| 11538 | } |
| 11539 | |
| 11540 | #else |
| 11541 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11542 | 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] | 11543 | unsigned char *hash, size_t *hashlen, |
| 11544 | unsigned char *data, size_t data_len, |
| 11545 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11546 | { |
| 11547 | int ret = 0; |
| 11548 | mbedtls_md_context_t ctx; |
| 11549 | 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] | 11550 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11551 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 11552 | 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] | 11553 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11554 | mbedtls_md_init( &ctx ); |
| 11555 | |
| 11556 | /* |
| 11557 | * digitally-signed struct { |
| 11558 | * opaque client_random[32]; |
| 11559 | * opaque server_random[32]; |
| 11560 | * ServerDHParams params; |
| 11561 | * }; |
| 11562 | */ |
| 11563 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 11564 | { |
| 11565 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 11566 | goto exit; |
| 11567 | } |
| 11568 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 11569 | { |
| 11570 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 11571 | goto exit; |
| 11572 | } |
| 11573 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 11574 | { |
| 11575 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 11576 | goto exit; |
| 11577 | } |
| 11578 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 11579 | { |
| 11580 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 11581 | goto exit; |
| 11582 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 11583 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11584 | { |
| 11585 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 11586 | goto exit; |
| 11587 | } |
| 11588 | |
| 11589 | exit: |
| 11590 | mbedtls_md_free( &ctx ); |
| 11591 | |
| 11592 | if( ret != 0 ) |
| 11593 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 11594 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 11595 | |
| 11596 | return( ret ); |
| 11597 | } |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11598 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 11599 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11600 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 11601 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 11602 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11603 | #endif /* MBEDTLS_SSL_TLS_C */ |