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