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 |
| 34 | #define CLIENT_HELLO_LEGACY_VERSION_LEN 2 |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 35 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 36 | /* Write extensions */ |
| 37 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 38 | /* |
| 39 | * ssl_tls13_write_supported_versions_ext(): |
| 40 | * |
| 41 | * struct { |
| 42 | * ProtocolVersion versions<2..254>; |
| 43 | * } SupportedVersions; |
| 44 | */ |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 45 | static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 46 | unsigned char *buf, |
| 47 | unsigned char *end, |
| 48 | size_t *olen ) |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 49 | { |
| 50 | unsigned char *p = buf; |
| 51 | |
| 52 | *olen = 0; |
| 53 | |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 54 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 55 | |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 56 | /* |
| 57 | * ExtensionType 2 |
| 58 | * ExtensionLength 2 |
| 59 | * VersionSLength 1 |
| 60 | * Version 2 |
| 61 | */ |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 62 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 ); |
| 63 | |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 64 | /* Write Extension Type */ |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 65 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 66 | |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 67 | /* Write Extension Length */ |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 68 | MBEDTLS_PUT_UINT16_BE( 3, p, 2); |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 69 | p += 4; |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 70 | |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 71 | /* Length of the SupportedVersions field data */ |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 72 | *p++ = 0x2; |
| 73 | |
| 74 | /* This implementation only supports a single TLS version, and only |
| 75 | * advertises a single value. |
| 76 | */ |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 77 | mbedtls_ssl_write_version( ssl->conf->max_major_ver, |
| 78 | ssl->conf->max_minor_ver, |
| 79 | ssl->conf->transport, p ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 80 | |
| 81 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 82 | ssl->conf->max_major_ver, |
| 83 | ssl->conf->max_minor_ver ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 84 | |
| 85 | *olen = 7; |
| 86 | |
| 87 | return( 0 ); |
| 88 | } |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 89 | |
| 90 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 91 | |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 92 | static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 93 | unsigned char *buf, |
| 94 | unsigned char *end, |
| 95 | size_t *olen ) |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 96 | { |
| 97 | ((void) ssl); |
| 98 | ((void) buf); |
| 99 | ((void) end); |
| 100 | ((void) olen); |
| 101 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 102 | } |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 103 | |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 104 | static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 105 | unsigned char *buf, |
| 106 | unsigned char *end, |
| 107 | size_t *olen ) |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 108 | { |
| 109 | ((void) ssl); |
| 110 | ((void) buf); |
| 111 | ((void) end); |
| 112 | ((void) olen); |
| 113 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 114 | } |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 115 | |
| 116 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 117 | |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame^] | 118 | /* Write ciphersuites |
| 119 | * CipherSuite cipher_suites<2..2^16-2>; |
| 120 | */ |
| 121 | static int ssl_tls13_write_client_hello_ciphersuites( |
| 122 | mbedtls_ssl_context *ssl, |
| 123 | unsigned char *buf, |
| 124 | unsigned char *end, |
| 125 | size_t *olen ) |
| 126 | { |
| 127 | /* Ciphersuite-related variables */ |
| 128 | const int *ciphersuites; |
| 129 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 130 | /* ciphersuite_start points to the start of |
| 131 | the ciphersuite list, i.e. to the length field*/ |
| 132 | unsigned char *ciphersuite_start, *ciphersuite_iter; |
| 133 | size_t buf_len; |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 134 | |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame^] | 135 | *olen = 0 ; |
| 136 | |
| 137 | /* |
| 138 | * Ciphersuite list |
| 139 | * |
| 140 | * This is a list of the symmetric cipher options supported by |
| 141 | * the client, specifically the record protection algorithm |
| 142 | * ( including secret key length ) and a hash to be used with |
| 143 | * HKDF, in descending order of client preference. |
| 144 | */ |
| 145 | ciphersuites = ssl->conf->ciphersuite_list; |
| 146 | |
| 147 | /* Check available spaces for ciphersuite */ |
| 148 | MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); |
| 149 | |
| 150 | /* Write ciphersuites */ |
| 151 | ciphersuite_start = buf + 2; |
| 152 | ciphersuite_iter = ciphersuite_start; |
| 153 | |
| 154 | for ( size_t i = 0; ciphersuites[i] != 0; i++ ) |
| 155 | { |
| 156 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] ); |
| 157 | |
| 158 | if( ciphersuite_info == NULL ) |
| 159 | continue; |
| 160 | |
| 161 | if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 || |
| 162 | ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 163 | continue; |
| 164 | |
| 165 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", |
| 166 | (unsigned int) ciphersuites[i], |
| 167 | ciphersuite_info->name ) ); |
| 168 | |
| 169 | /* Check for available spaces */ |
| 170 | MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); |
| 171 | |
| 172 | MBEDTLS_PUT_UINT16_BE( ciphersuites[i], ciphersuite_iter, 0); |
| 173 | ciphersuite_iter += 2; |
| 174 | |
| 175 | } |
| 176 | |
| 177 | buf_len = ciphersuite_iter - ciphersuite_start; |
| 178 | |
| 179 | /* write ciphersuite buf length */ |
| 180 | MBEDTLS_PUT_UINT16_BE( buf_len, buf, 0 ); |
| 181 | |
| 182 | |
| 183 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 184 | ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites", |
| 185 | buf_len/2 ) ); |
| 186 | |
| 187 | return( 0 ); |
| 188 | } |
| 189 | |
| 190 | /* Functions for writing ClientHello message */ |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 191 | static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 192 | unsigned char *buf, |
| 193 | size_t buflen, |
| 194 | size_t *len_with_binders ) |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 195 | { |
Jerry Yu | c4d2244 | 2021-08-27 20:04:33 +0800 | [diff] [blame] | 196 | /* Extensions */ |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 197 | |
| 198 | /* extension_start |
| 199 | * Used during extension writing where the |
| 200 | * buffer pointer to the beginning of the |
| 201 | * extension list must be kept to write |
| 202 | * the total extension list size in the end. |
| 203 | */ |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 204 | int ret; |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 205 | unsigned char *extension_start; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 206 | size_t cur_ext_len; /* Size of the current extension */ |
| 207 | size_t total_ext_len; /* Size of list of extensions */ |
| 208 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 209 | /* Buffer management */ |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 210 | unsigned char *start = buf; |
| 211 | unsigned char *end = buf + buflen; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 212 | |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame^] | 213 | *len_with_binders = 0; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 214 | |
| 215 | /* Keeping track of the included extensions */ |
| 216 | ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; |
| 217 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 218 | /* NOTE: |
| 219 | * Even for DTLS 1.3, we are writing a TLS handshake header here. |
| 220 | * The actual DTLS 1.3 handshake header is inserted in |
| 221 | * the record writing routine mbedtls_ssl_write_record(). |
| 222 | * |
| 223 | * For cTLS the length, and the version field |
| 224 | * are elided. The random bytes are shorter. |
| 225 | */ |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 226 | |
| 227 | if( ssl->conf->max_major_ver == 0 ) |
| 228 | { |
| 229 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, " |
| 230 | "consider using mbedtls_ssl_config_defaults()" ) ); |
| 231 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 232 | } |
| 233 | |
| 234 | ssl->major_ver = ssl->conf->min_major_ver; |
| 235 | ssl->minor_ver = ssl->conf->min_minor_ver; |
| 236 | |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame^] | 237 | /* Write legacy_version |
| 238 | * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 |
| 239 | * For TLS 1.3 we use the legacy version number {0x03, 0x03} |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 240 | * instead of the true version number. |
| 241 | * |
| 242 | * For DTLS 1.3 we use the legacy version number |
| 243 | * {254,253}. |
| 244 | * |
| 245 | * In cTLS the version number is elided. |
| 246 | */ |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 247 | MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_LEGACY_VERSION_LEN ); |
Jerry Yu | 2ac6419 | 2021-08-26 18:38:58 +0800 | [diff] [blame] | 248 | MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0); |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 249 | buf += CLIENT_HELLO_LEGACY_VERSION_LEN; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 250 | |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame^] | 251 | /* Write random bytes |
| 252 | Random random |
| 253 | */ |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 254 | MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RANDOM_LEN ); |
| 255 | memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN ); |
Jerry Yu | e885b76 | 2021-08-26 17:32:34 +0800 | [diff] [blame] | 256 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 257 | buf, CLIENT_HELLO_RANDOM_LEN ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 258 | |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 259 | buf += CLIENT_HELLO_RANDOM_LEN; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 260 | |
| 261 | /* Versions of TLS before TLS 1.3 supported a |
| 262 | * "session resumption" feature which has been merged with pre-shared |
| 263 | * keys in this version. A client which has a |
| 264 | * cached session ID set by a pre-TLS 1.3 server SHOULD set this |
| 265 | * field to that value. In compatibility mode, |
| 266 | * this field MUST be non-empty, so a client not offering a |
| 267 | * pre-TLS 1.3 session MUST generate a new 32-byte value. This value |
| 268 | * need not be random but SHOULD be unpredictable to avoid |
| 269 | * implementations fixating on a specific value ( also known as |
| 270 | * ossification ). Otherwise, it MUST be set as a zero-length vector |
| 271 | * ( i.e., a zero-valued single byte length field ). |
| 272 | */ |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame^] | 273 | MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 274 | *buf++ = 0; /* session id length set to zero */ |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 275 | |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame^] | 276 | /* Write ciphersuites */ |
| 277 | ret = ssl_tls13_write_client_hello_ciphersuites( ssl, buf, end, &cur_ext_len ); |
| 278 | if( ret != 0) |
| 279 | return( ret ); |
| 280 | buf += cur_ext_len; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 281 | |
| 282 | /* For every TLS 1.3 ClientHello, this vector MUST contain exactly |
| 283 | * one byte set to zero, which corresponds to the 'null' compression |
| 284 | * method in prior versions of TLS. |
| 285 | * |
| 286 | * For cTLS this field is elided. |
| 287 | */ |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame^] | 288 | MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 289 | *buf++ = 1; |
| 290 | *buf++ = MBEDTLS_SSL_COMPRESS_NULL; |
| 291 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 292 | |
| 293 | /* First write extensions, then the total length */ |
| 294 | extension_start = buf; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 295 | buf += 2; |
| 296 | |
| 297 | /* Supported Versions Extension is mandatory with TLS 1.3. |
| 298 | * |
| 299 | * For cTLS we only need to provide it if there is more than one version |
| 300 | * and currently there is only one. |
| 301 | */ |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 302 | ret = ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len ); |
| 303 | if( ret != 0 ) |
| 304 | return( ret ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 305 | buf += cur_ext_len; |
| 306 | |
| 307 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 308 | /* The supported_groups and the key_share extensions are |
| 309 | * REQUIRED for ECDHE ciphersuites. |
| 310 | */ |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 311 | ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 312 | if( ret != 0 ) |
| 313 | return( ret ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 314 | buf += cur_ext_len; |
| 315 | |
| 316 | /* The supported_signature_algorithms extension is REQUIRED for |
| 317 | * certificate authenticated ciphersuites. */ |
Jerry Yu | e41dec0 | 2021-08-31 10:57:07 +0800 | [diff] [blame] | 318 | ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, buf, end, &cur_ext_len ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 319 | if( ret != 0 ) |
| 320 | return( ret ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 321 | buf += cur_ext_len; |
| 322 | |
| 323 | /* We need to send the key shares under three conditions: |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 324 | * 1) A certificate-based ciphersuite is being offered. In this case |
| 325 | * supported_groups and supported_signature extensions have been |
| 326 | * successfully added. |
| 327 | * 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] | 328 | * psk_key_exchange_modes has been added as the last extension. |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 329 | * 3) Or, in case all ciphers are supported ( which includes #1 and #2 |
| 330 | * from above ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 331 | */ |
| 332 | |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 333 | ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 334 | if( ret != 0 ) |
| 335 | return( ret ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 336 | buf += cur_ext_len; |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame^] | 337 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 338 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 339 | |
| 340 | /* Add more extensions here */ |
| 341 | |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame^] | 342 | total_ext_len = buf - extension_start - 2; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 343 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET , |
| 344 | total_ext_len ) ); |
| 345 | |
| 346 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len ); |
| 347 | |
| 348 | /* Write extension length */ |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 349 | MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0 ); |
Jerry Yu | 2ac6419 | 2021-08-26 18:38:58 +0800 | [diff] [blame] | 350 | extension_start += 2; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 351 | |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame^] | 352 | *len_with_binders = buf - start; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 353 | return( 0 ); |
| 354 | } |
| 355 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 356 | static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 357 | { |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 358 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); |
| 359 | return( 0 ); |
| 360 | } |
Jerry Yu | ef6b36b | 2021-08-24 16:29:02 +0800 | [diff] [blame] | 361 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 362 | static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ) |
| 363 | { |
| 364 | int ret; |
Jerry Yu | ef6b36b | 2021-08-24 16:29:02 +0800 | [diff] [blame] | 365 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 366 | if( ssl->conf->f_rng == NULL ) |
| 367 | { |
| 368 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); |
| 369 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
| 370 | } |
Jerry Yu | ef6b36b | 2021-08-24 16:29:02 +0800 | [diff] [blame] | 371 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 372 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 373 | ssl->handshake->randbytes, |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 374 | CLIENT_HELLO_RANDOM_LEN ) ) != 0 ) |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 375 | { |
| 376 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); |
| 377 | return( ret ); |
| 378 | } |
Jerry Yu | 6f13f64 | 2021-08-26 17:18:15 +0800 | [diff] [blame] | 379 | |
| 380 | return( 0 ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 381 | } |
| 382 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 383 | /* |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 384 | * Write ClientHello handshake message. |
| 385 | * |
| 386 | * Structure of this message: |
| 387 | * |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 388 | * struct { |
| 389 | * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 |
| 390 | * Random random; |
| 391 | * opaque legacy_session_id<0..32>; |
| 392 | * CipherSuite cipher_suites<2..2^16-2>; |
| 393 | * opaque legacy_compression_methods<1..2^8-1>; |
| 394 | * Extension extensions<8..2^16-1>; |
| 395 | * } ClientHello; |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 396 | */ |
| 397 | static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 398 | { |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 399 | int ret = 0; |
| 400 | unsigned char *buf; |
| 401 | size_t buf_len, msg_len; |
| 402 | |
| 403 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); |
| 404 | |
| 405 | MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello, ( ssl ) ); |
| 406 | |
| 407 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg, |
| 408 | ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 409 | &buf, &buf_len ) ); |
| 410 | |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 411 | MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body, |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 412 | ( ssl, buf, buf_len, &msg_len ) ); |
| 413 | |
| 414 | mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 415 | msg_len ); |
| 416 | ssl->handshake->update_checksum( ssl, buf, 0 ); |
| 417 | |
| 418 | MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) ); |
| 419 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg, |
| 420 | ( ssl, buf_len, msg_len ) ); |
| 421 | |
| 422 | cleanup: |
| 423 | |
| 424 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); |
| 425 | return ret; |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 426 | } |
| 427 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 428 | int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 429 | { |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 430 | int ret = 0; |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 431 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 432 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) |
| 433 | { |
| 434 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) ); |
| 435 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 436 | } |
| 437 | |
| 438 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); |
| 439 | |
| 440 | switch( ssl->state ) |
| 441 | { |
| 442 | /* |
| 443 | * ssl->state is initialized as HELLO_REQUEST. It is same |
| 444 | * with CLIENT_HELLO status |
| 445 | */ |
| 446 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 447 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 448 | ret = ssl_tls13_write_client_hello( ssl ); |
| 449 | break; |
| 450 | |
| 451 | case MBEDTLS_SSL_SERVER_HELLO: |
| 452 | // Stop here : we haven't finished whole flow |
| 453 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 454 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS ); |
| 455 | break; |
| 456 | |
| 457 | default: |
| 458 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 459 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 460 | } |
| 461 | |
| 462 | return( ret ); |
| 463 | } |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 464 | |
Jerry Yu | 3cc4c2a | 2021-08-06 16:29:08 +0800 | [diff] [blame] | 465 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 466 | |
| 467 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |