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