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