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