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