Jerry Yu | 3cc4c2a | 2021-08-06 16:29:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * TLS 1.3 client-side functions |
| 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 | * This file is part of mbed TLS ( https://tls.mbed.org ) |
| 20 | */ |
| 21 | |
| 22 | #include "common.h" |
| 23 | |
| 24 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 25 | |
| 26 | #if defined(MBEDTLS_SSL_CLI_C) |
| 27 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 28 | #include <string.h> |
| 29 | |
Jerry Yu | 3cc4c2a | 2021-08-06 16:29:08 +0800 | [diff] [blame] | 30 | #include "ssl_misc.h" |
Jerry Yu | a13c7e7 | 2021-08-17 10:44:40 +0800 | [diff] [blame] | 31 | #include <mbedtls/debug.h> |
| 32 | |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 33 | #define CLIENT_HELLO_RANDOM_LEN 32 |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 34 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 35 | /* Write extensions */ |
| 36 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 37 | /* |
| 38 | * ssl_tls13_write_supported_versions_ext(): |
| 39 | * |
| 40 | * struct { |
| 41 | * ProtocolVersion versions<2..254>; |
| 42 | * } SupportedVersions; |
| 43 | */ |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 44 | static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 45 | unsigned char *buf, |
| 46 | unsigned char *end, |
| 47 | size_t *olen ) |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 48 | { |
| 49 | unsigned char *p = buf; |
| 50 | |
| 51 | *olen = 0; |
| 52 | |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 53 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 54 | |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 55 | /* |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 56 | * Check space for extension header. |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 57 | * |
| 58 | * extension_type 2 |
| 59 | * extension_data_length 2 |
| 60 | * version_length 1 |
| 61 | * versions 2 |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 62 | */ |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 63 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 ); |
| 64 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 65 | /* Write extension_type */ |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 66 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 67 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 68 | /* Write extension_data_length */ |
Jerry Yu | b7ab336 | 2021-08-31 16:16:19 +0800 | [diff] [blame] | 69 | MBEDTLS_PUT_UINT16_BE( 3, p, 2 ); |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 70 | p += 4; |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 71 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 72 | /* Length of versions */ |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 73 | *p++ = 0x2; |
| 74 | |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 75 | /* Write values of supported versions. |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 76 | * |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 77 | * They are defined by the configuration. |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 78 | * |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 79 | * Currently, only one version is advertised. |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 80 | */ |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 81 | mbedtls_ssl_write_version( ssl->conf->max_major_ver, |
| 82 | ssl->conf->max_minor_ver, |
| 83 | ssl->conf->transport, p ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 84 | |
| 85 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 86 | ssl->conf->max_major_ver, |
| 87 | ssl->conf->max_minor_ver ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 88 | |
| 89 | *olen = 7; |
| 90 | |
| 91 | return( 0 ); |
| 92 | } |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 93 | |
| 94 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 95 | |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame^] | 96 | /* |
| 97 | * Functions for writing supported_groups extension. |
| 98 | * |
| 99 | * Stucture of supported_groups: |
| 100 | * enum { |
| 101 | * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019), |
| 102 | * x25519(0x001D), x448(0x001E), |
| 103 | * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102), |
| 104 | * ffdhe6144(0x0103), ffdhe8192(0x0104), |
| 105 | * ffdhe_private_use(0x01FC..0x01FF), |
| 106 | * ecdhe_private_use(0xFE00..0xFEFF), |
| 107 | * (0xFFFF) |
| 108 | * } NamedGroup; |
| 109 | * struct { |
| 110 | * NamedGroup named_group_list<2..2^16-1>; |
| 111 | * } NamedGroupList; |
| 112 | */ |
| 113 | /* Find out available ecdhe named groups in current configuration */ |
| 114 | #if defined(MBEDTLS_ECDH_C) |
| 115 | /* |
| 116 | * In versions of TLS prior to TLS 1.3, this extension was named |
| 117 | * 'elliptic_curves' and only contained elliptic curve groups. |
| 118 | */ |
| 119 | static int ssl_tls13_write_named_group_ecdhe( mbedtls_ssl_context *ssl, |
| 120 | unsigned char *buf, |
| 121 | unsigned char *end, |
| 122 | size_t *olen ) |
| 123 | { |
| 124 | unsigned char *p = buf; |
| 125 | #if !defined(MBEDTLS_ECP_C) |
| 126 | ((void) ssl); |
| 127 | #endif |
| 128 | |
| 129 | *olen = 0; |
| 130 | |
| 131 | #if defined(MBEDTLS_ECP_C) |
| 132 | for ( const mbedtls_ecp_group_id *grp_id = ssl->conf->curve_list; |
| 133 | *grp_id != MBEDTLS_ECP_DP_NONE; |
| 134 | grp_id++ ) |
| 135 | { |
| 136 | const mbedtls_ecp_curve_info *info; |
| 137 | info = mbedtls_ecp_curve_info_from_grp_id( *grp_id ); |
| 138 | if( info == NULL ) |
| 139 | continue; |
| 140 | #else |
| 141 | for ( const mbedtls_ecp_curve_info *info = mbedtls_ecp_curve_list(); |
| 142 | info->grp_id != MBEDTLS_ECP_DP_NONE; |
| 143 | info++ ) |
| 144 | { |
| 145 | #endif |
| 146 | if( !mbedtls_ssl_named_group_is_ecdhe( info->tls_id ) ) |
| 147 | continue; |
| 148 | |
| 149 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2); |
| 150 | MBEDTLS_PUT_UINT16_BE( info->tls_id, p, 0 ); |
| 151 | p += 2; |
| 152 | |
| 153 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )", |
| 154 | mbedtls_ecp_curve_info_from_tls_id( info->tls_id )->name, |
| 155 | info->tls_id ) ); |
| 156 | } |
| 157 | |
| 158 | *olen = p - buf; |
| 159 | |
| 160 | return( 0 ); |
| 161 | } |
| 162 | #else |
| 163 | static int ssl_tls13_write_named_group_ecdhe( mbedtls_ssl_context *ssl, |
| 164 | unsigned char *buf, |
| 165 | unsigned char *end, |
| 166 | size_t *olen ) |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 167 | { |
| 168 | ((void) ssl); |
| 169 | ((void) buf); |
| 170 | ((void) end); |
Jerry Yu | 7533635 | 2021-09-01 15:59:36 +0800 | [diff] [blame] | 171 | *olen = 0; |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame^] | 172 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 173 | } |
| 174 | #endif /* MBEDTLS_ECDH_C */ |
| 175 | |
| 176 | /* Find out available dhe named groups in current configuration */ |
| 177 | static int ssl_tls13_write_named_group_dhe( mbedtls_ssl_context *ssl, |
| 178 | unsigned char *buf, |
| 179 | unsigned char *end, |
| 180 | size_t *olen ) |
| 181 | { |
| 182 | ((void) ssl); |
| 183 | ((void) buf); |
| 184 | ((void) end); |
| 185 | *olen = 0; |
| 186 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "write_named_group_dhe is not implemented" ) ); |
| 187 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Supported Groups Extension (supported_groups) |
| 192 | */ |
| 193 | static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl, |
| 194 | unsigned char *buf, |
| 195 | unsigned char *end, |
| 196 | size_t *olen ) |
| 197 | { |
| 198 | unsigned char *p = buf ; |
| 199 | unsigned char *named_group_ptr; /* Start of named_group_list */ |
| 200 | size_t named_group_len = 0; |
| 201 | int ret = 0, ret_ecdhe, ret_dhe; |
| 202 | |
| 203 | *olen = 0; |
| 204 | |
| 205 | if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) |
| 206 | return( 0 ); |
| 207 | |
| 208 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) ); |
| 209 | |
| 210 | /* Check there is space for extension header */ |
| 211 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 212 | p += 6; |
| 213 | |
| 214 | named_group_ptr = p; |
| 215 | ret_ecdhe = ssl_tls13_write_named_group_ecdhe( ssl, p, end, &named_group_len ); |
| 216 | if( ret_ecdhe != 0 ) |
| 217 | { |
| 218 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_ecdhe", ret ); |
| 219 | } |
| 220 | p += named_group_len; |
| 221 | |
| 222 | ret_dhe = ssl_tls13_write_named_group_dhe( ssl, p, end, &named_group_len ); |
| 223 | if( ret_dhe != 0 ) |
| 224 | { |
| 225 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_dhe", ret ); |
| 226 | } |
| 227 | p += named_group_len; |
| 228 | |
| 229 | /* Both ECDHE and DHE Fail. */ |
| 230 | if( ret_ecdhe != 0 && ret_dhe != 0 ) |
| 231 | { |
| 232 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Both ECDHE and DHE groups are fail. " ) ); |
| 233 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 234 | } |
| 235 | |
| 236 | /* Length of named_group_list*/ |
| 237 | named_group_len = p - named_group_ptr; |
| 238 | if( named_group_len == 0 ) |
| 239 | { |
| 240 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No Named Group Available." ) ); |
| 241 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 242 | } |
| 243 | |
| 244 | /* Write extension_type */ |
| 245 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 ); |
| 246 | /* Write extension_data_length */ |
| 247 | MBEDTLS_PUT_UINT16_BE( named_group_len + 2, buf, 2 ); |
| 248 | /* Write length of named_group_list */ |
| 249 | MBEDTLS_PUT_UINT16_BE( named_group_len, buf, 4 ); |
| 250 | |
| 251 | MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", buf + 4, named_group_len + 2 ); |
| 252 | |
| 253 | *olen = p - buf; |
| 254 | |
| 255 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS; |
| 256 | |
| 257 | return( ret ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 258 | } |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 259 | |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 260 | static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 261 | unsigned char *buf, |
| 262 | unsigned char *end, |
| 263 | size_t *olen ) |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 264 | { |
| 265 | ((void) ssl); |
| 266 | ((void) buf); |
| 267 | ((void) end); |
Jerry Yu | 7533635 | 2021-09-01 15:59:36 +0800 | [diff] [blame] | 268 | *olen = 0; |
| 269 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "key share extension is not available" ) ); |
| 270 | return( 0 ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 271 | } |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 272 | |
| 273 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 274 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 275 | /* |
| 276 | * Functions for writing ClientHello message. |
| 277 | */ |
| 278 | /* Write cipher_suites |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 279 | * CipherSuite cipher_suites<2..2^16-2>; |
| 280 | */ |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 281 | static int ssl_tls13_write_client_hello_cipher_suites( |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 282 | mbedtls_ssl_context *ssl, |
| 283 | unsigned char *buf, |
| 284 | unsigned char *end, |
| 285 | size_t *olen ) |
| 286 | { |
Jerry Yu | fec982e | 2021-09-07 17:26:06 +0800 | [diff] [blame] | 287 | unsigned char *p = buf; |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 288 | const int *ciphersuite_list; |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 289 | unsigned char *cipher_suites_ptr; /* Start of the cipher_suites list */ |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 290 | size_t cipher_suites_len; |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 291 | |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 292 | *olen = 0 ; |
| 293 | |
| 294 | /* |
| 295 | * Ciphersuite list |
| 296 | * |
| 297 | * This is a list of the symmetric cipher options supported by |
| 298 | * the client, specifically the record protection algorithm |
| 299 | * ( including secret key length ) and a hash to be used with |
| 300 | * HKDF, in descending order of client preference. |
| 301 | */ |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 302 | ciphersuite_list = ssl->conf->ciphersuite_list; |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 303 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 304 | /* Check there is space for the cipher suite list length (2 bytes). */ |
Jerry Yu | 4e38828 | 2021-09-06 21:28:08 +0800 | [diff] [blame] | 305 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 306 | p += 2; |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 307 | |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 308 | /* Write cipher_suites */ |
Jerry Yu | 4e38828 | 2021-09-06 21:28:08 +0800 | [diff] [blame] | 309 | cipher_suites_ptr = p; |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 310 | for ( size_t i = 0; ciphersuite_list[i] != 0; i++ ) |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 311 | { |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 312 | int cipher_suite = ciphersuite_list[i]; |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 313 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 314 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 315 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite ); |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 316 | if( ciphersuite_info == NULL ) |
| 317 | continue; |
Jerry Yu | dbfb7bd | 2021-09-04 09:58:58 +0800 | [diff] [blame] | 318 | if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver && |
| 319 | MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) ) |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 320 | continue; |
| 321 | |
| 322 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 323 | (unsigned int) cipher_suite, |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 324 | ciphersuite_info->name ) ); |
| 325 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 326 | /* Check there is space for the cipher suite identifier (2 bytes). */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 327 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 328 | MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 ); |
| 329 | p += 2; |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 330 | } |
| 331 | |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 332 | /* Write the cipher_suites length in number of bytes */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 333 | cipher_suites_len = p - cipher_suites_ptr; |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 334 | MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 ); |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 335 | MBEDTLS_SSL_DEBUG_MSG( 3, |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 336 | ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites", |
| 337 | cipher_suites_len/2 ) ); |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 338 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 339 | /* Output the total length of cipher_suites field. */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 340 | *olen = p - buf; |
Jerry Yu | f171e83 | 2021-08-31 18:31:09 +0800 | [diff] [blame] | 341 | |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 342 | return( 0 ); |
| 343 | } |
| 344 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 345 | /* |
| 346 | * Structure of ClientHello message: |
| 347 | * |
| 348 | * struct { |
| 349 | * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 |
| 350 | * Random random; |
| 351 | * opaque legacy_session_id<0..32>; |
| 352 | * CipherSuite cipher_suites<2..2^16-2>; |
| 353 | * opaque legacy_compression_methods<1..2^8-1>; |
| 354 | * Extension extensions<8..2^16-1>; |
| 355 | * } ClientHello; |
| 356 | */ |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 357 | static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 358 | unsigned char *buf, |
Jerry Yu | ef387d7 | 2021-09-02 13:59:41 +0800 | [diff] [blame] | 359 | unsigned char *end, |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 360 | size_t *olen ) |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 361 | { |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 362 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 363 | int ret; |
Jerry Yu | 8c02bb4 | 2021-09-03 21:09:22 +0800 | [diff] [blame] | 364 | unsigned char *extensions_len_ptr; /* Pointer to extensions length */ |
Jerry Yu | 790656a | 2021-09-01 15:51:48 +0800 | [diff] [blame] | 365 | size_t output_len; /* Length of buffer used by function */ |
| 366 | size_t extensions_len; /* Length of the list of extensions*/ |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 367 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 368 | /* Buffer management */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 369 | unsigned char *p = buf; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 370 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 371 | *olen = 0; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 372 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 373 | /* No validation needed here. It has been done by ssl_conf_check() */ |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 374 | ssl->major_ver = ssl->conf->min_major_ver; |
| 375 | ssl->minor_ver = ssl->conf->min_minor_ver; |
| 376 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 377 | /* |
| 378 | * Write legacy_version |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 379 | * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 380 | * |
| 381 | * For TLS 1.3 we use the legacy version number {0x03, 0x03} |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 382 | * instead of the true version number. |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 383 | */ |
Jerry Yu | fec982e | 2021-09-07 17:26:06 +0800 | [diff] [blame] | 384 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 385 | MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 ); |
Jerry Yu | fec982e | 2021-09-07 17:26:06 +0800 | [diff] [blame] | 386 | p += 2; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 387 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 388 | /* Write the random bytes ( random ).*/ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 389 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, CLIENT_HELLO_RANDOM_LEN ); |
| 390 | memcpy( p, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN ); |
Jerry Yu | e885b76 | 2021-08-26 17:32:34 +0800 | [diff] [blame] | 391 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 392 | p, CLIENT_HELLO_RANDOM_LEN ); |
| 393 | p += CLIENT_HELLO_RANDOM_LEN; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 394 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 395 | /* |
| 396 | * Write legacy_session_id |
| 397 | * |
| 398 | * Versions of TLS before TLS 1.3 supported a "session resumption" feature |
| 399 | * which has been merged with pre-shared keys in this version. A client |
| 400 | * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set |
| 401 | * this field to that value. In compatibility mode, this field MUST be |
| 402 | * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate |
| 403 | * a new 32-byte value. This value need not be random but SHOULD be |
| 404 | * unpredictable to avoid implementations fixating on a specific value |
| 405 | * ( also known as ossification ). Otherwise, it MUST be set as a zero-length |
| 406 | * vector ( i.e., a zero-valued single byte length field ). |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 407 | */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 408 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 ); |
| 409 | *p++ = 0; /* session id length set to zero */ |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 410 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 411 | /* Write cipher_suites */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 412 | ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len ); |
Jerry Yu | dbfb7bd | 2021-09-04 09:58:58 +0800 | [diff] [blame] | 413 | if( ret != 0 ) |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 414 | return( ret ); |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 415 | p += output_len; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 416 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 417 | /* Write legacy_compression_methods |
| 418 | * |
| 419 | * For every TLS 1.3 ClientHello, this vector MUST contain exactly |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 420 | * one byte set to zero, which corresponds to the 'null' compression |
| 421 | * method in prior versions of TLS. |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 422 | */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 423 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 424 | *p++ = 1; |
| 425 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 426 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 427 | /* Write extensions */ |
| 428 | |
| 429 | /* Keeping track of the included extensions */ |
| 430 | ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 431 | |
| 432 | /* First write extensions, then the total length */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 433 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 434 | extensions_len_ptr = p; |
| 435 | p += 2; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 436 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 437 | /* Write supported_versions extension |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 438 | * |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 439 | * Supported Versions Extension is mandatory with TLS 1.3. |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 440 | */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 441 | ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 442 | if( ret != 0 ) |
| 443 | return( ret ); |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 444 | p += output_len; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 445 | |
| 446 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 447 | /* Write supported_groups extension |
| 448 | * |
| 449 | * It is REQUIRED for ECDHE cipher_suites. |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 450 | */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 451 | ret = ssl_tls13_write_supported_groups_ext( ssl, p, end, &output_len ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 452 | if( ret != 0 ) |
| 453 | return( ret ); |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 454 | p += output_len; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 455 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 456 | /* Write key_share extension |
| 457 | * |
| 458 | * We need to send the key shares under three conditions: |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 459 | * 1) A certificate-based ciphersuite is being offered. In this case |
| 460 | * supported_groups and supported_signature extensions have been |
| 461 | * successfully added. |
| 462 | * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 463 | * psk_key_exchange_modes has been added as the last extension. |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 464 | * 3) Or, in case all ciphers are supported ( which includes #1 and #2 |
| 465 | * from above ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 466 | */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 467 | ret = ssl_tls13_write_key_shares_ext( ssl, p, end, &output_len ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 468 | if( ret != 0 ) |
| 469 | return( ret ); |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 470 | p += output_len; |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 471 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 472 | /* Write signature_algorithms extension |
| 473 | * |
| 474 | * It is REQUIRED for certificate authenticated cipher_suites. |
| 475 | */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 476 | ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, p, end, &output_len ); |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 477 | if( ret != 0 ) |
| 478 | return( ret ); |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 479 | p += output_len; |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 480 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 481 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 482 | |
| 483 | /* Add more extensions here */ |
| 484 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 485 | /* Write the length of the list of extensions. */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 486 | extensions_len = p - extensions_len_ptr - 2; |
Jerry Yu | 790656a | 2021-09-01 15:51:48 +0800 | [diff] [blame] | 487 | MBEDTLS_PUT_UINT16_BE( extensions_len, extensions_len_ptr, 0 ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 488 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET , |
Jerry Yu | 790656a | 2021-09-01 15:51:48 +0800 | [diff] [blame] | 489 | extensions_len ) ); |
| 490 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extensions_len_ptr, extensions_len ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 491 | |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 492 | *olen = p - buf; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 493 | return( 0 ); |
| 494 | } |
| 495 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 496 | static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 497 | { |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 498 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); |
| 499 | return( 0 ); |
| 500 | } |
Jerry Yu | ef6b36b | 2021-08-24 16:29:02 +0800 | [diff] [blame] | 501 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 502 | static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ) |
| 503 | { |
| 504 | int ret; |
Jerry Yu | ef6b36b | 2021-08-24 16:29:02 +0800 | [diff] [blame] | 505 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 506 | if( ssl->conf->f_rng == NULL ) |
| 507 | { |
| 508 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); |
| 509 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
| 510 | } |
Jerry Yu | ef6b36b | 2021-08-24 16:29:02 +0800 | [diff] [blame] | 511 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 512 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 513 | ssl->handshake->randbytes, |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 514 | CLIENT_HELLO_RANDOM_LEN ) ) != 0 ) |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 515 | { |
Jerry Yu | 8c02bb4 | 2021-09-03 21:09:22 +0800 | [diff] [blame] | 516 | MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 517 | return( ret ); |
| 518 | } |
Jerry Yu | 6f13f64 | 2021-08-26 17:18:15 +0800 | [diff] [blame] | 519 | |
| 520 | return( 0 ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 521 | } |
| 522 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 523 | /* |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 524 | * Write ClientHello handshake message. |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 525 | */ |
| 526 | static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 527 | { |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 528 | int ret = 0; |
| 529 | unsigned char *buf; |
| 530 | size_t buf_len, msg_len; |
| 531 | |
| 532 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); |
| 533 | |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 534 | MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 535 | |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 536 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( |
| 537 | ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 538 | &buf, &buf_len ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 539 | |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 540 | MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf, |
Jerry Yu | ef387d7 | 2021-09-02 13:59:41 +0800 | [diff] [blame] | 541 | buf + buf_len, |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 542 | &msg_len ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 543 | |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 544 | mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, |
| 545 | MBEDTLS_SSL_HS_CLIENT_HELLO, |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 546 | msg_len ); |
| 547 | ssl->handshake->update_checksum( ssl, buf, msg_len ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 548 | |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 549 | MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) ); |
| 550 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl, |
| 551 | buf_len, |
| 552 | msg_len ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 553 | |
| 554 | cleanup: |
| 555 | |
| 556 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); |
| 557 | return ret; |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 558 | } |
| 559 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 560 | int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 561 | { |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 562 | int ret = 0; |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 563 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 564 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); |
| 565 | |
| 566 | switch( ssl->state ) |
| 567 | { |
| 568 | /* |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 569 | * ssl->state is initialized as HELLO_REQUEST. It is the same |
| 570 | * as CLIENT_HELLO state. |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 571 | */ |
| 572 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 573 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 574 | ret = ssl_tls13_write_client_hello( ssl ); |
| 575 | break; |
| 576 | |
| 577 | case MBEDTLS_SSL_SERVER_HELLO: |
| 578 | // Stop here : we haven't finished whole flow |
| 579 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 580 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS ); |
| 581 | break; |
| 582 | |
| 583 | default: |
| 584 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 585 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 586 | } |
| 587 | |
| 588 | return( ret ); |
| 589 | } |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 590 | |
Jerry Yu | 3cc4c2a | 2021-08-06 16:29:08 +0800 | [diff] [blame] | 591 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 592 | |
| 593 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |