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