Jerry Yu | 3cc4c2a | 2021-08-06 16:29:08 +0800 | [diff] [blame] | 1 | /* |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2 | * TLS 1.3 server-side functions |
Jerry Yu | 3cc4c2a | 2021-08-06 16:29:08 +0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 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. |
| 18 | */ |
| 19 | |
| 20 | #include "common.h" |
| 21 | |
Jerry Yu | fb4b647 | 2022-01-27 15:03:26 +0800 | [diff] [blame] | 22 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 3cc4c2a | 2021-08-06 16:29:08 +0800 | [diff] [blame] | 23 | |
Jerry Yu | 687101b | 2021-09-14 16:03:56 +0800 | [diff] [blame] | 24 | #include "mbedtls/debug.h" |
| 25 | |
Jerry Yu | 3cc4c2a | 2021-08-06 16:29:08 +0800 | [diff] [blame] | 26 | #include "ssl_misc.h" |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 27 | #include "ssl_tls13_keys.h" |
Gilles Peskine | 923d5c9 | 2021-12-15 12:56:54 +0100 | [diff] [blame] | 28 | #include "ssl_debug_helpers.h" |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 29 | #include <string.h> |
| 30 | #if defined(MBEDTLS_ECP_C) |
| 31 | #include "mbedtls/ecp.h" |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 32 | #endif /* MBEDTLS_ECP_C */ |
Jerry Yu | 3cc4c2a | 2021-08-06 16:29:08 +0800 | [diff] [blame] | 33 | |
XiaokangQian | a9c5841 | 2022-02-17 09:41:26 +0000 | [diff] [blame] | 34 | #if defined(MBEDTLS_PLATFORM_C) |
| 35 | #include "mbedtls/platform.h" |
| 36 | #else |
| 37 | #include <stdlib.h> |
| 38 | #define mbedtls_calloc calloc |
| 39 | #define mbedtls_free free |
| 40 | #endif /* MBEDTLS_PLATFORM_C */ |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 41 | |
| 42 | /* From RFC 8446: |
| 43 | * struct { |
| 44 | * select (Handshake.msg_type) { |
| 45 | * case client_hello: |
| 46 | * ProtocolVersion versions<2..254>; |
| 47 | * case server_hello: // and HelloRetryRequest |
| 48 | * ProtocolVersion selected_version; |
| 49 | * }; |
| 50 | * } SupportedVersions; |
| 51 | */ |
| 52 | static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl, |
| 53 | const unsigned char *buf, |
| 54 | const unsigned char *end ) |
| 55 | { |
| 56 | size_t list_len; |
| 57 | int tls13_supported = 0; |
| 58 | int major_ver, minor_ver; |
| 59 | const unsigned char *p = buf; |
| 60 | const unsigned char *version_end; |
| 61 | |
| 62 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 ); |
| 63 | |
| 64 | list_len = p[0]; |
| 65 | p += 1; |
| 66 | |
| 67 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_len ); |
| 68 | if( list_len % 2 != 0 ) |
| 69 | { |
| 70 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid supported version list length %" MBEDTLS_PRINTF_SIZET, |
| 71 | list_len ) ); |
| 72 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 73 | } |
| 74 | |
| 75 | version_end = p + list_len; |
| 76 | while( p < version_end ) |
| 77 | { |
| 78 | mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p ); |
| 79 | |
| 80 | /* In this implementation we only support TLS 1.3 and DTLS 1.3. */ |
| 81 | if( major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 && |
| 82 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 83 | { |
| 84 | tls13_supported = 1; |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | p += 2; |
| 89 | } |
| 90 | |
| 91 | if( tls13_supported == 0 ) |
| 92 | { |
| 93 | /* When we support runtime negotiation of TLS 1.2 and TLS 1.3, we need |
XiaokangQian | c5763b5 | 2022-04-02 03:34:37 +0000 | [diff] [blame] | 94 | * a graceful fallback to TLS 1.2 in this case. |
| 95 | */ |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 96 | |
| 97 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 is not supported by the client" ) ); |
| 98 | |
| 99 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION, |
| 100 | MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION ); |
| 101 | return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION ); |
| 102 | } |
| 103 | |
| 104 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Negotiated version. Supported is [%d:%d]", |
| 105 | major_ver, minor_ver ) ); |
| 106 | |
| 107 | ssl->major_ver = major_ver; |
| 108 | ssl->minor_ver = minor_ver; |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 109 | return( 0 ); |
| 110 | } |
| 111 | |
| 112 | #if ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) ) |
| 113 | /* This function parses the TLS 1.3 supported_groups extension and |
| 114 | * stores the received groups in ssl->handshake->curves. |
| 115 | * |
| 116 | * From RFC 8446: |
| 117 | * enum { |
| 118 | * ... (0xFFFF) |
| 119 | * } NamedGroup; |
| 120 | * struct { |
| 121 | * NamedGroup named_group_list<2..2^16-1>; |
| 122 | * } NamedGroupList; |
| 123 | */ |
| 124 | static int mbedtls_ssl_tls13_parse_supported_groups_ext( |
| 125 | mbedtls_ssl_context *ssl, |
| 126 | const unsigned char *buf, const unsigned char *end ) |
| 127 | { |
| 128 | |
| 129 | size_t list_size, our_size; |
| 130 | const unsigned char *p = buf; |
| 131 | const mbedtls_ecp_curve_info *curve_info, **curves; |
| 132 | const unsigned char *extentions_end; |
| 133 | |
| 134 | MBEDTLS_SSL_DEBUG_BUF( 3, "supported_groups extension", p, end - buf ); |
| 135 | list_size = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 136 | p += 2; |
| 137 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, list_size ); |
| 138 | if( list_size % 2 != 0 ) |
| 139 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 140 | |
XiaokangQian | c5763b5 | 2022-04-02 03:34:37 +0000 | [diff] [blame] | 141 | /* At the moment, this can happen when receiving a second |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 142 | * ClientHello after an HRR. We should properly reset the |
| 143 | * state upon receiving an HRR, in which case we should |
| 144 | * not observe handshake->curves already being allocated. */ |
| 145 | if( ssl->handshake->curves != NULL ) |
| 146 | { |
XiaokangQian | 8840888 | 2022-04-02 10:15:03 +0000 | [diff] [blame] | 147 | //mbedtls_free( ssl->handshake->curves ); |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 148 | ssl->handshake->curves = NULL; |
| 149 | } |
| 150 | |
| 151 | /* Don't allow our peer to make us allocate too much memory, |
XiaokangQian | c5763b5 | 2022-04-02 03:34:37 +0000 | [diff] [blame] | 152 | * and leave room for a final 0 |
| 153 | */ |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 154 | our_size = list_size / 2 + 1; |
| 155 | if( our_size > MBEDTLS_ECP_DP_MAX ) |
| 156 | our_size = MBEDTLS_ECP_DP_MAX; |
| 157 | |
| 158 | if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL ) |
| 159 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 160 | |
| 161 | extentions_end = p + list_size; |
| 162 | ssl->handshake->curves = curves; |
| 163 | |
| 164 | while ( p < extentions_end && our_size > 1 ) |
| 165 | { |
| 166 | uint16_t tls_grp_id = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 167 | curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_grp_id ); |
| 168 | |
| 169 | /* mbedtls_ecp_curve_info_from_tls_id() uses the mbedtls_ecp_curve_info |
| 170 | * data structure (defined in ecp.c), which only includes the list of |
| 171 | * curves implemented. Hence, we only add curves that are also supported |
XiaokangQian | c5763b5 | 2022-04-02 03:34:37 +0000 | [diff] [blame] | 172 | * and implemented by the server. |
| 173 | */ |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 174 | if( curve_info != NULL ) |
| 175 | { |
| 176 | *curves++ = curve_info; |
| 177 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "supported curve: %s", curve_info->name ) ); |
| 178 | our_size--; |
| 179 | } |
| 180 | |
| 181 | p += 2; |
| 182 | } |
| 183 | |
| 184 | return( 0 ); |
| 185 | |
| 186 | } |
| 187 | #endif /* MBEDTLS_ECDH_C || ( MBEDTLS_ECDSA_C */ |
| 188 | |
XiaokangQian | 8840888 | 2022-04-02 10:15:03 +0000 | [diff] [blame] | 189 | #if defined(MBEDTLS_ECDH_C) |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 190 | /* |
| 191 | * ssl_tls13_parse_key_shares_ext() verifies whether the information in the |
| 192 | * extension is correct and stores the provided key shares. Whether this is an |
| 193 | * acceptable key share depends on the selected ciphersuite. |
| 194 | * |
| 195 | * Possible return values are: |
| 196 | * - 0: Successful processing of the client provided key share extension. |
| 197 | * - MBEDTLS_ERR_SSL_HRR_REQUIRED: The key share provided by the client |
| 198 | * does not match a group supported by the server. A HelloRetryRequest will |
| 199 | * be needed. |
| 200 | * - Another negative return value for fatal errors. |
| 201 | */ |
| 202 | |
| 203 | static int ssl_tls13_parse_key_shares_ext( mbedtls_ssl_context *ssl, |
| 204 | const unsigned char *buf, |
| 205 | const unsigned char *end ) |
| 206 | { |
| 207 | int ret = 0; |
| 208 | unsigned char const *p = buf; |
| 209 | unsigned char const *extentions_end; |
| 210 | |
| 211 | size_t total_ext_len, cur_share_len; |
| 212 | int match_found = 0; |
| 213 | |
| 214 | /* From RFC 8446: |
| 215 | * |
| 216 | * struct { |
| 217 | * KeyShareEntry client_shares<0..2^16-1>; |
| 218 | * } KeyShareClientHello; |
| 219 | * |
| 220 | */ |
| 221 | |
| 222 | /* Read total legnth of KeyShareClientHello */ |
| 223 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 224 | |
| 225 | total_ext_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 226 | p += 2; |
| 227 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, total_ext_len ); |
| 228 | |
| 229 | ssl->handshake->offered_group_id = 0; |
| 230 | extentions_end = p + total_ext_len; |
| 231 | |
| 232 | /* We try to find a suitable key share entry and copy it to the |
| 233 | * handshake context. Later, we have to find out whether we can do |
| 234 | * something with the provided key share or whether we have to |
XiaokangQian | c5763b5 | 2022-04-02 03:34:37 +0000 | [diff] [blame] | 235 | * dismiss it and send a HelloRetryRequest message. |
| 236 | */ |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 237 | |
| 238 | for( ; p < extentions_end; p += cur_share_len ) |
| 239 | { |
XiaokangQian | 9b5d04b | 2022-04-10 10:20:43 +0000 | [diff] [blame^] | 240 | uint16_t group; |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * struct { |
| 244 | * NamedGroup group; |
| 245 | * opaque key_exchange<1..2^16-1>; |
| 246 | * } KeyShareEntry; |
| 247 | */ |
| 248 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extentions_end, 4 ); |
| 249 | |
XiaokangQian | 9b5d04b | 2022-04-10 10:20:43 +0000 | [diff] [blame^] | 250 | group = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 251 | p += 2; |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 252 | |
| 253 | cur_share_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | 9b5d04b | 2022-04-10 10:20:43 +0000 | [diff] [blame^] | 254 | p += 2; |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 255 | |
| 256 | /* Continue parsing even if we have already found a match, |
XiaokangQian | c5763b5 | 2022-04-02 03:34:37 +0000 | [diff] [blame] | 257 | * for input validation purposes. |
| 258 | */ |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 259 | if( match_found == 1 ) |
| 260 | continue; |
| 261 | |
| 262 | /* |
| 263 | * NamedGroup matching |
| 264 | * |
| 265 | * For now, we only support ECDHE groups, but e.g. |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 266 | |
XiaokangQian | 9b5d04b | 2022-04-10 10:20:43 +0000 | [diff] [blame^] | 267 | * Type 1: ECDHE shares |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 268 | * |
| 269 | * - Check if we recognize the group |
| 270 | * - Check if it's supported |
| 271 | */ |
| 272 | |
XiaokangQian | 9b5d04b | 2022-04-10 10:20:43 +0000 | [diff] [blame^] | 273 | if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) ) |
| 274 | { |
| 275 | const mbedtls_ecp_curve_info *curve_info = |
| 276 | mbedtls_ecp_curve_info_from_tls_id( group ); |
| 277 | if( curve_info == NULL ) |
| 278 | { |
| 279 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid TLS curve group id" ) ); |
| 280 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 281 | } |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 282 | |
XiaokangQian | 9b5d04b | 2022-04-10 10:20:43 +0000 | [diff] [blame^] | 283 | match_found = 1; |
| 284 | |
| 285 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) ); |
| 286 | |
| 287 | ret = mbedtls_ssl_tls13_read_public_ecdhe_share( ssl, p, end - p ); |
| 288 | if( ret != 0 ) |
| 289 | return( ret ); |
| 290 | } |
| 291 | else |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 292 | { |
| 293 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "Unrecognized NamedGroup %u", |
XiaokangQian | 9b5d04b | 2022-04-10 10:20:43 +0000 | [diff] [blame^] | 294 | (unsigned) group ) ); |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 295 | continue; |
| 296 | } |
| 297 | |
XiaokangQian | 9b5d04b | 2022-04-10 10:20:43 +0000 | [diff] [blame^] | 298 | ssl->handshake->offered_group_id = group; |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | if( match_found == 0 ) |
| 302 | { |
| 303 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching key share" ) ); |
| 304 | return( MBEDTLS_ERR_SSL_HRR_REQUIRED ); |
| 305 | } |
| 306 | return( 0 ); |
| 307 | } |
XiaokangQian | 8840888 | 2022-04-02 10:15:03 +0000 | [diff] [blame] | 308 | #endif /* MBEDTLS_ECDH_C */ |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 309 | |
| 310 | #if defined(MBEDTLS_SSL_COOKIE_C) |
| 311 | static int ssl_tls13_parse_cookie_ext( mbedtls_ssl_context *ssl, |
| 312 | const unsigned char *buf, |
| 313 | const unsigned char *end ) |
| 314 | { |
| 315 | int ret = 0; |
| 316 | size_t cookie_len; |
| 317 | unsigned char const *p = buf; |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 318 | |
| 319 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse cookie extension" ) ); |
| 320 | |
| 321 | if( ssl->conf->f_cookie_check != NULL ) |
| 322 | { |
| 323 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 324 | cookie_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 325 | p += 2; |
| 326 | |
| 327 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cookie_len ); |
| 328 | |
| 329 | MBEDTLS_SSL_DEBUG_BUF( 3, "Received cookie", p, cookie_len ); |
| 330 | |
| 331 | if( ssl->conf->f_cookie_check( ssl->conf->p_cookie, |
| 332 | p, cookie_len, ssl->cli_id, |
| 333 | ssl->cli_id_len ) != 0 ) |
| 334 | { |
| 335 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) ); |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 336 | ret = MBEDTLS_ERR_SSL_HRR_REQUIRED; |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) ); |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
XiaokangQian | c5763b5 | 2022-04-02 03:34:37 +0000 | [diff] [blame] | 343 | else |
| 344 | { |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 345 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) ); |
| 346 | } |
| 347 | |
| 348 | return( ret ); |
| 349 | } |
| 350 | #endif /* MBEDTLS_SSL_COOKIE_C */ |
| 351 | |
| 352 | /* |
| 353 | * |
| 354 | * STATE HANDLING: ClientHello |
| 355 | * |
| 356 | * There are three possible classes of outcomes when parsing the CH: |
| 357 | * |
| 358 | * 1) The CH was well-formed and matched the server's configuration. |
| 359 | * |
| 360 | * In this case, the server progresses to sending its ServerHello. |
| 361 | * |
| 362 | * 2) The CH was well-formed but didn't match the server's configuration. |
| 363 | * |
| 364 | * For example, the client might not have offered a key share which |
| 365 | * the server supports, or the server might require a cookie. |
| 366 | * |
| 367 | * In this case, the server sends a HelloRetryRequest. |
| 368 | * |
| 369 | * 3) The CH was ill-formed |
| 370 | * |
| 371 | * In this case, we abort the handshake. |
| 372 | * |
| 373 | */ |
| 374 | |
| 375 | /* |
| 376 | * Overview |
| 377 | */ |
| 378 | |
| 379 | /* Main entry point from the state machine; orchestrates the otherfunctions. */ |
| 380 | static int ssl_client_hello_process( mbedtls_ssl_context *ssl ); |
| 381 | |
| 382 | static int ssl_client_hello_parse( mbedtls_ssl_context *ssl, |
| 383 | const unsigned char *buf, |
| 384 | const unsigned char *end ); |
| 385 | |
| 386 | /* Update the handshake state machine */ |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 387 | static int ssl_client_hello_postprocess( mbedtls_ssl_context *ssl, |
| 388 | int hrr_required ); |
| 389 | |
| 390 | /* |
| 391 | * Implementation |
| 392 | */ |
| 393 | |
| 394 | #define SSL_CLIENT_HELLO_OK 0 |
| 395 | #define SSL_CLIENT_HELLO_HRR_REQUIRED 1 |
| 396 | |
| 397 | static int ssl_client_hello_process( mbedtls_ssl_context *ssl ) |
| 398 | { |
| 399 | |
| 400 | int ret = 0; |
| 401 | int hrr_required = SSL_CLIENT_HELLO_OK; |
| 402 | unsigned char* buf = NULL; |
| 403 | size_t buflen = 0; |
| 404 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) ); |
| 405 | |
| 406 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 407 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( |
| 408 | ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 409 | &buf, &buflen ) ); |
| 410 | |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 411 | MBEDTLS_SSL_PROC_CHK_NEG( ssl_client_hello_parse( ssl, buf, buf + buflen ) ); |
| 412 | hrr_required = ret; |
| 413 | |
| 414 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "postprocess" ) ); |
| 415 | MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess( ssl, hrr_required ) ); |
| 416 | |
| 417 | cleanup: |
| 418 | |
| 419 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) ); |
| 420 | return( ret ); |
| 421 | } |
| 422 | |
| 423 | static void ssl_debug_print_client_hello_exts( mbedtls_ssl_context *ssl ) |
| 424 | { |
XiaokangQian | 3207a32 | 2022-02-23 03:15:27 +0000 | [diff] [blame] | 425 | ((void) ssl); |
| 426 | |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 427 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Extensions:" ) ); |
| 428 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "- KEY_SHARE_EXTENSION ( %s )", |
| 429 | ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_KEY_SHARE ) > 0 ) ? |
| 430 | "TRUE" : "FALSE" ) ); |
| 431 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PSK_KEY_EXCHANGE_MODES_EXTENSION ( %s )", |
| 432 | ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ) > 0 ) ? |
| 433 | "TRUE" : "FALSE" ) ); |
| 434 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "- PRE_SHARED_KEY_EXTENSION ( %s )", |
| 435 | ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_PRE_SHARED_KEY ) > 0 ) ? |
| 436 | "TRUE" : "FALSE" ) ); |
| 437 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SIGNATURE_ALGORITHM_EXTENSION ( %s )", |
| 438 | ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SIG_ALG ) > 0 ) ? |
| 439 | "TRUE" : "FALSE" ) ); |
| 440 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_GROUPS_EXTENSION ( %s )", |
| 441 | ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ) >0 ) ? |
| 442 | "TRUE" : "FALSE" ) ); |
| 443 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SUPPORTED_VERSION_EXTENSION ( %s )", |
| 444 | ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ) > 0 ) ? |
| 445 | "TRUE" : "FALSE" ) ); |
| 446 | #if defined ( MBEDTLS_SSL_SERVER_NAME_INDICATION ) |
| 447 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "- SERVERNAME_EXTENSION ( %s )", |
| 448 | ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_SERVERNAME ) > 0 ) ? |
| 449 | "TRUE" : "FALSE" ) ); |
| 450 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 451 | #if defined ( MBEDTLS_SSL_COOKIE_C ) |
| 452 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "- COOKIE_EXTENSION ( %s )", |
| 453 | ( ( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) >0 ) ? |
| 454 | "TRUE" : "FALSE" ) ); |
| 455 | #endif /* MBEDTLS_SSL_COOKIE_C */ |
| 456 | } |
| 457 | |
| 458 | static int ssl_client_hello_has_exts( mbedtls_ssl_context *ssl, |
| 459 | int ext_id_mask ) |
| 460 | { |
| 461 | int masked = ssl->handshake->extensions_present & ext_id_mask; |
| 462 | return( masked == ext_id_mask ); |
| 463 | } |
| 464 | |
| 465 | static int ssl_client_hello_has_cert_extensions( mbedtls_ssl_context *ssl ) |
| 466 | { |
| 467 | return( ssl_client_hello_has_exts( ssl, |
| 468 | MBEDTLS_SSL_EXT_SUPPORTED_GROUPS | |
| 469 | MBEDTLS_SSL_EXT_KEY_SHARE | |
| 470 | MBEDTLS_SSL_EXT_SIG_ALG ) ); |
| 471 | } |
| 472 | |
| 473 | static int ssl_check_certificate_key_exchange( mbedtls_ssl_context *ssl ) |
| 474 | { |
| 475 | if( !mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) ) |
| 476 | return( 0 ); |
| 477 | |
| 478 | if( !ssl_client_hello_has_cert_extensions( ssl ) ) |
| 479 | return( 0 ); |
| 480 | |
| 481 | ssl->handshake->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL; |
| 482 | return( 1 ); |
| 483 | } |
| 484 | |
| 485 | static int ssl_client_hello_parse( mbedtls_ssl_context *ssl, |
| 486 | const unsigned char *buf, |
| 487 | const unsigned char *end ) |
| 488 | { |
| 489 | int ret; |
| 490 | size_t i, j; |
| 491 | size_t comp_len, sess_len; |
| 492 | size_t cipher_suites_len; |
| 493 | size_t ext_len; |
| 494 | const unsigned char *ciph_offset; |
| 495 | const unsigned char *p = buf; |
| 496 | const unsigned char *extensions_end; |
| 497 | |
| 498 | const int* ciphersuites; |
| 499 | const mbedtls_ssl_ciphersuite_t* ciphersuite_info; |
| 500 | |
| 501 | int hrr_required = 0; |
| 502 | |
| 503 | ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; |
| 504 | |
| 505 | /* |
| 506 | * ClientHello layer: |
| 507 | * 0 . 1 protocol version |
| 508 | * 2 . 33 random bytes ( starting with 4 bytes of Unix time ) |
XiaokangQian | c5763b5 | 2022-04-02 03:34:37 +0000 | [diff] [blame] | 509 | * 34 . 34 session id length ( 1 byte ) |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 510 | * 35 . 34+x session id |
| 511 | * 35+x . 35+x DTLS only: cookie length ( 1 byte ) |
| 512 | * 36+x . .. DTLS only: cookie |
| 513 | * .. . .. ciphersuite list length ( 2 bytes ) |
| 514 | * .. . .. ciphersuite list |
| 515 | * .. . .. compression alg. list length ( 1 byte ) |
| 516 | * .. . .. compression alg. list |
| 517 | * .. . .. extensions length ( 2 bytes, optional ) |
| 518 | * .. . .. extensions ( optional ) |
| 519 | */ |
| 520 | |
XiaokangQian | c5763b5 | 2022-04-02 03:34:37 +0000 | [diff] [blame] | 521 | /* Needs to be updated due to mandatory extensions |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 522 | * Minimal length ( with everything empty and extensions ommitted ) is |
| 523 | * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can |
| 524 | * read at least up to session id length without worrying. |
| 525 | */ |
| 526 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 38 ); |
| 527 | |
| 528 | /* ... |
| 529 | * ProtocolVersion legacy_version = 0x0303; // TLS 1.2 |
| 530 | * ... |
| 531 | * with ProtocolVersion defined as: |
| 532 | * uint16 ProtocolVersion; |
| 533 | */ |
| 534 | if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 && |
| 535 | p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) ) |
| 536 | { |
| 537 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) ); |
| 538 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION, |
| 539 | MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION ); |
| 540 | ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; |
| 541 | return ret; |
| 542 | } |
| 543 | p += 2; |
| 544 | |
| 545 | /* |
| 546 | * Save client random |
| 547 | */ |
| 548 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 ); |
| 549 | |
| 550 | memcpy( &ssl->handshake->randbytes[0], p, 32 ); |
XiaokangQian | c5763b5 | 2022-04-02 03:34:37 +0000 | [diff] [blame] | 551 | /* skip random bytes */ |
| 552 | p += 32; |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 553 | |
| 554 | /* |
| 555 | * Parse session ID |
| 556 | */ |
| 557 | sess_len = p[0]; |
XiaokangQian | c5763b5 | 2022-04-02 03:34:37 +0000 | [diff] [blame] | 558 | p++; |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 559 | |
| 560 | if( sess_len > 32 ) |
| 561 | { |
| 562 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 563 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 564 | } |
| 565 | |
| 566 | ssl->session_negotiate->id_len = sess_len; |
| 567 | |
| 568 | /* Note that this field is echoed even if |
| 569 | * the client's value corresponded to a cached pre-TLS 1.3 session |
| 570 | * which the server has chosen not to resume. A client which |
| 571 | * receives a legacy_session_id_echo field that does not match what |
| 572 | * it sent in the ClientHello MUST abort the handshake with an |
| 573 | * "illegal_parameter" alert. |
| 574 | */ |
| 575 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id length ( %" MBEDTLS_PRINTF_SIZET " )", sess_len ) ); |
| 576 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf, sess_len ); |
| 577 | |
| 578 | memcpy( &ssl->session_negotiate->id[0], p, sess_len ); /* write session id */ |
| 579 | p += sess_len; |
| 580 | |
| 581 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 582 | cipher_suites_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 583 | p += 2; |
| 584 | |
| 585 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, cipher_suites_len ); |
| 586 | |
| 587 | /* store pointer to ciphersuite list */ |
| 588 | ciph_offset = p; |
| 589 | |
| 590 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", |
| 591 | p, cipher_suites_len ); |
| 592 | |
| 593 | /* skip ciphersuites for now */ |
| 594 | p += cipher_suites_len; |
| 595 | |
| 596 | /* |
| 597 | * For TLS 1.3 we are not using compression. |
| 598 | */ |
XiaokangQian | a9c5841 | 2022-02-17 09:41:26 +0000 | [diff] [blame] | 599 | comp_len = p[0]; |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 600 | p++; |
| 601 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, comp_len ); |
| 602 | |
| 603 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression", |
| 604 | p, comp_len ); |
| 605 | |
| 606 | /* Determine whether we are indeed using null compression */ |
XiaokangQian | a9c5841 | 2022-02-17 09:41:26 +0000 | [diff] [blame] | 607 | if( ( comp_len != 1 ) && ( p[0] == 0 ) ) |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 608 | { |
| 609 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 610 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
| 611 | } |
| 612 | |
| 613 | /* skip compression */ |
| 614 | p++; |
| 615 | |
| 616 | /* |
| 617 | * Check the extension length |
| 618 | */ |
| 619 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 620 | |
| 621 | ext_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 622 | p += 2; |
| 623 | extensions_end = p + ext_len; |
| 624 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, ext_len ); |
| 625 | |
| 626 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p, ext_len ); |
| 627 | |
| 628 | while( p < extensions_end ) |
| 629 | { |
| 630 | unsigned int extension_type; |
| 631 | size_t extension_data_len; |
| 632 | const unsigned char *extension_data_end; |
| 633 | |
| 634 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 ); |
| 635 | extension_type = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 636 | extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 ); |
| 637 | p += 4; |
| 638 | |
| 639 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len ); |
| 640 | extension_data_end = p + extension_data_len; |
| 641 | |
| 642 | switch( extension_type ) |
| 643 | { |
| 644 | #if defined(MBEDTLS_SSL_COOKIE_C) |
| 645 | case MBEDTLS_TLS_EXT_COOKIE: |
| 646 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found cookie extension" ) ); |
| 647 | |
| 648 | ret = ssl_tls13_parse_cookie_ext( ssl, p, |
| 649 | extension_data_end ); |
| 650 | |
| 651 | /* if cookie verification failed then we return a hello retry |
| 652 | * message, or return success and set cookie extension present |
| 653 | */ |
| 654 | if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED ) |
| 655 | { |
| 656 | hrr_required = 1; |
| 657 | } |
| 658 | else if( ret == 0 ) |
| 659 | { |
| 660 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_COOKIE; |
| 661 | } |
| 662 | break; |
| 663 | #endif /* MBEDTLS_SSL_COOKIE_C */ |
| 664 | |
| 665 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) |
| 666 | case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS: |
| 667 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported group extension" ) ); |
| 668 | |
| 669 | /* Supported Groups Extension |
| 670 | * |
| 671 | * When sent by the client, the "supported_groups" extension |
| 672 | * indicates the named groups which the client supports, |
| 673 | * ordered from most preferred to least preferred. |
| 674 | */ |
| 675 | ret = mbedtls_ssl_tls13_parse_supported_groups_ext( ssl, p, |
| 676 | extension_data_end ); |
| 677 | if( ret != 0 ) |
| 678 | { |
| 679 | MBEDTLS_SSL_DEBUG_RET( 1, |
| 680 | "mbedtls_ssl_parse_supported_groups_ext", ret ); |
| 681 | return( ret ); |
| 682 | } |
| 683 | |
| 684 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS; |
| 685 | break; |
| 686 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */ |
| 687 | |
XiaokangQian | 8840888 | 2022-04-02 10:15:03 +0000 | [diff] [blame] | 688 | #if defined(MBEDTLS_ECDH_C) |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 689 | case MBEDTLS_TLS_EXT_KEY_SHARE: |
| 690 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key share extension" ) ); |
| 691 | |
| 692 | /* |
| 693 | * Key Share Extension |
| 694 | * |
| 695 | * When sent by the client, the "key_share" extension |
| 696 | * contains the endpoint's cryptographic parameters for |
| 697 | * ECDHE/DHE key establishment methods. |
| 698 | */ |
| 699 | ret = ssl_tls13_parse_key_shares_ext( ssl, p, extension_data_end ); |
| 700 | if( ret == MBEDTLS_ERR_SSL_HRR_REQUIRED ) |
| 701 | { |
| 702 | hrr_required = 1; |
| 703 | ret = 0; |
| 704 | } |
| 705 | |
| 706 | if( ret != 0 ) |
| 707 | return( ret ); |
| 708 | |
| 709 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE; |
| 710 | break; |
XiaokangQian | 8840888 | 2022-04-02 10:15:03 +0000 | [diff] [blame] | 711 | #endif /* MBEDTLS_ECDH_C */ |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 712 | |
| 713 | case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS: |
| 714 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported versions extension" ) ); |
| 715 | |
| 716 | ret = ssl_tls13_parse_supported_versions_ext( |
| 717 | ssl, p, extension_data_end ); |
| 718 | if( ret != 0 ) |
| 719 | { |
| 720 | MBEDTLS_SSL_DEBUG_RET( 1, |
| 721 | ( "ssl_tls13_parse_supported_versions_ext" ), ret ); |
| 722 | return( ret ); |
| 723 | } |
| 724 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS; |
| 725 | break; |
| 726 | |
| 727 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 728 | case MBEDTLS_TLS_EXT_SIG_ALG: |
| 729 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) ); |
| 730 | |
| 731 | ret = mbedtls_ssl_tls13_parse_sig_alg_ext( ssl, p, |
| 732 | extension_data_end ); |
| 733 | if( ret != 0 ) |
| 734 | { |
| 735 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 736 | ( "ssl_parse_supported_signature_algorithms_server_ext ( %d )", |
| 737 | ret ) ); |
| 738 | return( ret ); |
| 739 | } |
| 740 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 741 | break; |
| 742 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 743 | |
| 744 | default: |
| 745 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 746 | ( "unknown extension found: %ud ( ignoring )", |
| 747 | extension_type ) ); |
| 748 | } |
| 749 | |
| 750 | p += extension_data_len; |
| 751 | } |
| 752 | |
| 753 | /* Update checksum with either |
| 754 | * - The entire content of the CH message, if no PSK extension is present |
| 755 | * - The content up to but excluding the PSK extension, if present. |
| 756 | */ |
XiaokangQian | c4b8c99 | 2022-04-07 11:31:38 +0000 | [diff] [blame] | 757 | mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_SERVER_HELLO, |
| 758 | buf, p - buf ); |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 759 | /* |
| 760 | * Search for a matching ciphersuite |
| 761 | */ |
| 762 | ciphersuites = ssl->conf->ciphersuite_list; |
| 763 | ciphersuite_info = NULL; |
| 764 | for ( j = 0, p = ciph_offset; j < cipher_suites_len; j += 2, p += 2 ) |
| 765 | { |
| 766 | for ( i = 0; ciphersuites[i] != 0; i++ ) |
| 767 | { |
| 768 | if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) || |
| 769 | p[1] != ( ( ciphersuites[i] ) & 0xFF ) ) |
| 770 | continue; |
| 771 | |
| 772 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( |
| 773 | ciphersuites[i] ); |
| 774 | |
| 775 | if( ciphersuite_info == NULL ) |
| 776 | { |
| 777 | MBEDTLS_SSL_DEBUG_MSG( |
| 778 | 1, |
| 779 | ( "mbedtls_ssl_ciphersuite_from_id: should never happen" ) ); |
| 780 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 781 | } |
| 782 | |
| 783 | goto have_ciphersuite; |
| 784 | |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 789 | |
| 790 | have_ciphersuite: |
| 791 | |
| 792 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", |
| 793 | ciphersuite_info->name ) ); |
| 794 | |
| 795 | ssl->session_negotiate->ciphersuite = ciphersuites[i]; |
| 796 | ssl->handshake->ciphersuite_info = ciphersuite_info; |
| 797 | |
| 798 | /* List all the extensions we have received */ |
| 799 | ssl_debug_print_client_hello_exts( ssl ); |
| 800 | |
| 801 | /* |
| 802 | * Determine the key exchange algorithm to use. |
| 803 | * There are three types of key exchanges supported in TLS 1.3: |
| 804 | * - (EC)DH with ECDSA, |
| 805 | * - (EC)DH with PSK, |
| 806 | * - plain PSK. |
| 807 | * |
| 808 | * The PSK-based key exchanges may additionally be used with 0-RTT. |
| 809 | * |
| 810 | * Our built-in order of preference is |
| 811 | * 1 ) Plain PSK Mode |
| 812 | * 2 ) (EC)DHE-PSK Mode |
| 813 | * 3 ) Certificate Mode |
| 814 | */ |
| 815 | |
| 816 | if( !ssl_check_certificate_key_exchange( ssl ) ) |
| 817 | { |
| 818 | MBEDTLS_SSL_DEBUG_MSG( |
| 819 | 1, |
| 820 | ( "ClientHello message misses mandatory extensions." ) ); |
| 821 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION , |
| 822 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
| 823 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
| 824 | } |
| 825 | |
| 826 | #if defined(MBEDTLS_SSL_COOKIE_C) |
| 827 | /* If we failed to see a cookie extension, and we required it through the |
| 828 | * configuration settings ( rr_config ), then we need to send a HRR msg. |
| 829 | * Conceptually, this is similiar to having received a cookie that failed |
| 830 | * the verification check. |
| 831 | */ |
| 832 | if( ( ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON ) && |
| 833 | !( ssl->handshake->extensions_present & MBEDTLS_SSL_EXT_COOKIE ) ) |
| 834 | { |
| 835 | MBEDTLS_SSL_DEBUG_MSG( |
| 836 | 2, |
| 837 | ( "Cookie extension missing. Need to send a HRR." ) ); |
| 838 | hrr_required = 1; |
| 839 | } |
| 840 | #endif /* MBEDTLS_SSL_COOKIE_C */ |
| 841 | |
| 842 | if( hrr_required == 1 ) |
| 843 | return( SSL_CLIENT_HELLO_HRR_REQUIRED ); |
| 844 | |
| 845 | return( 0 ); |
| 846 | } |
| 847 | |
| 848 | static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl, |
| 849 | int hrr_required ) |
| 850 | { |
| 851 | int ret = 0; |
| 852 | |
XiaokangQian | 7ac3ab3 | 2022-02-22 04:03:26 +0000 | [diff] [blame] | 853 | if( ssl->handshake->hello_retry_requests_sent == 0 && |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 854 | ssl->conf->rr_config == MBEDTLS_SSL_FORCE_RR_CHECK_ON ) |
| 855 | { |
| 856 | hrr_required = SSL_CLIENT_HELLO_HRR_REQUIRED; |
| 857 | } |
| 858 | |
| 859 | if( hrr_required == SSL_CLIENT_HELLO_HRR_REQUIRED ) |
| 860 | { |
| 861 | /* |
| 862 | * Create stateless transcript hash for HRR |
| 863 | */ |
| 864 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "Reset transcript for HRR" ) ); |
| 865 | ret = mbedtls_ssl_reset_transcript_for_hrr( ssl ); |
| 866 | if( ret != 0 ) |
| 867 | { |
| 868 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_reset_transcript_for_hrr", |
| 869 | ret ); |
| 870 | return( ret ); |
| 871 | } |
| 872 | mbedtls_ssl_session_reset_msg_layer( ssl, 0 ); |
| 873 | |
| 874 | /* Transmit Hello Retry Request */ |
| 875 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST ); |
| 876 | return( 0 ); |
| 877 | } |
| 878 | |
| 879 | ret = mbedtls_ssl_tls13_key_schedule_stage_early( ssl ); |
| 880 | if( ret != 0 ) |
| 881 | { |
| 882 | MBEDTLS_SSL_DEBUG_RET( 1, |
| 883 | "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret ); |
| 884 | return( ret ); |
| 885 | } |
| 886 | |
| 887 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); |
| 888 | return( 0 ); |
| 889 | |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | * TLS and DTLS 1.3 State Maschine -- server side |
| 894 | */ |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 895 | int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 896 | { |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 897 | int ret = 0; |
| 898 | |
| 899 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) |
| 900 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 901 | |
Jerry Yu | e3b3412 | 2021-09-28 17:53:35 +0800 | [diff] [blame] | 902 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls13 server state: %s(%d)", |
| 903 | mbedtls_ssl_states_str( ssl->state ), |
| 904 | ssl->state ) ); |
Jerry Yu | 6e81b27 | 2021-09-27 11:16:17 +0800 | [diff] [blame] | 905 | |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 906 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 907 | return( ret ); |
| 908 | |
| 909 | switch( ssl->state ) |
| 910 | { |
| 911 | /* start state */ |
| 912 | case MBEDTLS_SSL_HELLO_REQUEST: |
XiaokangQian | 7ac3ab3 | 2022-02-22 04:03:26 +0000 | [diff] [blame] | 913 | ssl->handshake->hello_retry_requests_sent = 0; |
XiaokangQian | 7807f9f | 2022-02-15 10:04:37 +0000 | [diff] [blame] | 914 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO ); |
| 915 | |
| 916 | break; |
| 917 | |
| 918 | /* ----- READ CLIENT HELLO ----*/ |
| 919 | |
| 920 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 921 | |
| 922 | ret = ssl_client_hello_process( ssl ); |
| 923 | if( ret != 0 ) |
| 924 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_client_hello_process", ret ); |
| 925 | |
| 926 | break; |
| 927 | |
| 928 | case MBEDTLS_SSL_HANDSHAKE_WRAPUP: |
| 929 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 930 | |
| 931 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for all traffic" ) ); |
| 932 | |
| 933 | mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application ); |
| 934 | mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application ); |
| 935 | |
| 936 | mbedtls_ssl_tls13_handshake_wrapup( ssl ); |
| 937 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET ); |
| 938 | |
| 939 | break; |
| 940 | |
| 941 | default: |
| 942 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 943 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 944 | } |
| 945 | |
| 946 | return( ret ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 947 | } |
Jerry Yu | 3cc4c2a | 2021-08-06 16:29:08 +0800 | [diff] [blame] | 948 | |
Jerry Yu | fb4b647 | 2022-01-27 15:03:26 +0800 | [diff] [blame] | 949 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */ |