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