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 | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 31 | #include "mbedtls/debug.h" |
| 32 | #include "mbedtls/error.h" |
Jerry Yu | a13c7e7 | 2021-08-17 10:44:40 +0800 | [diff] [blame] | 33 | |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 34 | #define CLIENT_HELLO_RANDOM_LEN 32 |
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 | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 56 | /* Check if we have space for header and length fields: |
| 57 | * - extension_type (2 bytes) |
| 58 | * - extension_data_length (2 bytes) |
| 59 | * - versions_length (1 byte ) |
| 60 | * - versions (2 bytes) |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 61 | */ |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 62 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 ); |
| 63 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 64 | /* Write extension_type */ |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 65 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 66 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 67 | /* Write extension_data_length */ |
Jerry Yu | b7ab336 | 2021-08-31 16:16:19 +0800 | [diff] [blame] | 68 | MBEDTLS_PUT_UINT16_BE( 3, p, 2 ); |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 69 | p += 4; |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 70 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 71 | /* Length of versions */ |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 72 | *p++ = 0x2; |
| 73 | |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 74 | /* Write values of supported versions. |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 75 | * |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 76 | * They are defined by the configuration. |
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 | * Currently, only one version is advertised. |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 79 | */ |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 80 | mbedtls_ssl_write_version( ssl->conf->max_major_ver, |
| 81 | ssl->conf->max_minor_ver, |
| 82 | ssl->conf->transport, p ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 83 | |
| 84 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 85 | ssl->conf->max_major_ver, |
| 86 | ssl->conf->max_minor_ver ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 87 | |
| 88 | *olen = 7; |
| 89 | |
| 90 | return( 0 ); |
| 91 | } |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 92 | |
| 93 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 94 | |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 95 | /* |
| 96 | * Functions for writing supported_groups extension. |
| 97 | * |
| 98 | * Stucture of supported_groups: |
| 99 | * enum { |
| 100 | * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019), |
| 101 | * x25519(0x001D), x448(0x001E), |
| 102 | * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102), |
| 103 | * ffdhe6144(0x0103), ffdhe8192(0x0104), |
| 104 | * ffdhe_private_use(0x01FC..0x01FF), |
| 105 | * ecdhe_private_use(0xFE00..0xFEFF), |
| 106 | * (0xFFFF) |
| 107 | * } NamedGroup; |
| 108 | * struct { |
| 109 | * NamedGroup named_group_list<2..2^16-1>; |
| 110 | * } NamedGroupList; |
| 111 | */ |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 112 | #if defined(MBEDTLS_ECDH_C) |
| 113 | /* |
| 114 | * In versions of TLS prior to TLS 1.3, this extension was named |
| 115 | * 'elliptic_curves' and only contained elliptic curve groups. |
| 116 | */ |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 117 | static int ssl_tls13_write_named_group_list_ecdhe( mbedtls_ssl_context *ssl, |
| 118 | unsigned char *buf, |
| 119 | unsigned char *end, |
| 120 | size_t *olen ) |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 121 | { |
| 122 | unsigned char *p = buf; |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 123 | |
| 124 | *olen = 0; |
| 125 | |
Jerry Yu | 7c522d4 | 2021-09-08 17:55:09 +0800 | [diff] [blame] | 126 | if( ssl->conf->curve_list == NULL ) |
| 127 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 128 | |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 129 | for ( const mbedtls_ecp_group_id *grp_id = ssl->conf->curve_list; |
| 130 | *grp_id != MBEDTLS_ECP_DP_NONE; |
| 131 | grp_id++ ) |
| 132 | { |
| 133 | const mbedtls_ecp_curve_info *info; |
| 134 | info = mbedtls_ecp_curve_info_from_grp_id( *grp_id ); |
| 135 | if( info == NULL ) |
| 136 | continue; |
Jerry Yu | 7c522d4 | 2021-09-08 17:55:09 +0800 | [diff] [blame] | 137 | |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 138 | if( !mbedtls_ssl_tls13_named_group_is_ecdhe( info->tls_id ) ) |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 139 | continue; |
| 140 | |
| 141 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2); |
| 142 | MBEDTLS_PUT_UINT16_BE( info->tls_id, p, 0 ); |
| 143 | p += 2; |
| 144 | |
| 145 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )", |
| 146 | mbedtls_ecp_curve_info_from_tls_id( info->tls_id )->name, |
| 147 | info->tls_id ) ); |
| 148 | } |
| 149 | |
| 150 | *olen = p - buf; |
| 151 | |
| 152 | return( 0 ); |
| 153 | } |
| 154 | #else |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 155 | static int ssl_tls13_write_named_group_list_ecdhe( mbedtls_ssl_context *ssl, |
| 156 | unsigned char *buf, |
| 157 | unsigned char *end, |
| 158 | size_t *olen ) |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 159 | { |
| 160 | ((void) ssl); |
| 161 | ((void) buf); |
| 162 | ((void) end); |
Jerry Yu | 7533635 | 2021-09-01 15:59:36 +0800 | [diff] [blame] | 163 | *olen = 0; |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 164 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 165 | } |
| 166 | #endif /* MBEDTLS_ECDH_C */ |
| 167 | |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 168 | static int ssl_tls13_write_named_group_list_dhe( mbedtls_ssl_context *ssl, |
| 169 | unsigned char *buf, |
| 170 | unsigned char *end, |
| 171 | size_t *olen ) |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 172 | { |
| 173 | ((void) ssl); |
| 174 | ((void) buf); |
| 175 | ((void) end); |
| 176 | *olen = 0; |
| 177 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "write_named_group_dhe is not implemented" ) ); |
| 178 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 179 | } |
| 180 | |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 181 | static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl, |
| 182 | unsigned char *buf, |
| 183 | unsigned char *end, |
| 184 | size_t *olen ) |
| 185 | { |
| 186 | unsigned char *p = buf ; |
Jerry Yu | 72fc69b | 2021-09-10 10:23:24 +0800 | [diff] [blame^] | 187 | unsigned char *named_group_list_ptr; /* Start of named_group_list */ |
| 188 | size_t named_group_list_len; /* Length of named_group_list */ |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 189 | size_t output_len = 0; |
| 190 | int ret_ecdhe, ret_dhe; |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 191 | |
| 192 | *olen = 0; |
| 193 | |
| 194 | if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) |
| 195 | return( 0 ); |
| 196 | |
| 197 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) ); |
| 198 | |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 199 | /* Check if we have space for header and length fields: |
| 200 | * - extension_type (2 bytes) |
| 201 | * - extension_data_length (2 bytes) |
| 202 | * - named_group_list_length (2 bytes) |
| 203 | */ |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 204 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 205 | p += 6; |
| 206 | |
Jerry Yu | 72fc69b | 2021-09-10 10:23:24 +0800 | [diff] [blame^] | 207 | named_group_list_ptr = p; |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 208 | ret_ecdhe = ssl_tls13_write_named_group_list_ecdhe( ssl, p, end, &output_len ); |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 209 | if( ret_ecdhe != 0 ) |
| 210 | { |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 211 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_list_ecdhe", ret_ecdhe ); |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 212 | } |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 213 | p += output_len; |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 214 | |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 215 | ret_dhe = ssl_tls13_write_named_group_list_dhe( ssl, p, end, &output_len ); |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 216 | if( ret_dhe != 0 ) |
| 217 | { |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 218 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_list_dhe", ret_dhe ); |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 219 | } |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 220 | p += output_len; |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 221 | |
| 222 | /* Both ECDHE and DHE Fail. */ |
| 223 | if( ret_ecdhe != 0 && ret_dhe != 0 ) |
| 224 | { |
| 225 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Both ECDHE and DHE groups are fail. " ) ); |
| 226 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 227 | } |
| 228 | |
| 229 | /* Length of named_group_list*/ |
Jerry Yu | 72fc69b | 2021-09-10 10:23:24 +0800 | [diff] [blame^] | 230 | named_group_list_len = p - named_group_list_ptr; |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 231 | if( named_group_list_len == 0 ) |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 232 | { |
Jerry Yu | 72fc69b | 2021-09-10 10:23:24 +0800 | [diff] [blame^] | 233 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group Available." ) ); |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 234 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 235 | } |
| 236 | |
| 237 | /* Write extension_type */ |
| 238 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 ); |
| 239 | /* Write extension_data_length */ |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 240 | MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 ); |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 241 | /* Write length of named_group_list */ |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 242 | MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 ); |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 243 | |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 244 | MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", buf + 4, named_group_list_len + 2 ); |
Jerry Yu | 6b64fe3 | 2021-09-01 17:05:13 +0800 | [diff] [blame] | 245 | |
| 246 | *olen = p - buf; |
| 247 | |
| 248 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS; |
| 249 | |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 250 | return( 0 ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 251 | } |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 252 | |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 253 | /* |
| 254 | * Functions for writing key_share extension. |
| 255 | */ |
| 256 | #if defined(MBEDTLS_ECDH_C) |
Jerry Yu | 7c522d4 | 2021-09-08 17:55:09 +0800 | [diff] [blame] | 257 | static int ssl_tls13_generate_and_write_ecdh_key_exchange( |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 258 | mbedtls_ssl_context *ssl, |
| 259 | uint16_t named_group, |
| 260 | unsigned char *buf, |
| 261 | unsigned char *end, |
| 262 | size_t *olen ) |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 263 | { |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 264 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 265 | const mbedtls_ecp_curve_info *curve_info = |
| 266 | mbedtls_ecp_curve_info_from_tls_id( named_group ); |
| 267 | |
| 268 | if( curve_info == NULL ) |
| 269 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 270 | |
| 271 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) ); |
| 272 | |
| 273 | if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx, |
| 274 | curve_info->grp_id ) ) != 0 ) |
| 275 | { |
| 276 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret ); |
| 277 | return( ret ); |
| 278 | } |
| 279 | |
| 280 | ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, olen, |
| 281 | buf, end - buf, |
| 282 | ssl->conf->f_rng, ssl->conf->p_rng ); |
| 283 | if( ret != 0 ) |
| 284 | { |
| 285 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret ); |
| 286 | return( ret ); |
| 287 | } |
| 288 | |
| 289 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 290 | MBEDTLS_DEBUG_ECDH_Q ); |
Jerry Yu | 7533635 | 2021-09-01 15:59:36 +0800 | [diff] [blame] | 291 | return( 0 ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 292 | } |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 293 | #endif /* MBEDTLS_ECDH_C */ |
| 294 | |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 295 | static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl, |
| 296 | uint16_t *group_id ) |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 297 | { |
| 298 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 299 | |
| 300 | /* Pick first entry of curve list. |
| 301 | * |
| 302 | * TODO: When we introduce PQC KEMs, we'll have a NamedGroup |
| 303 | * list instead, and can just return its first element. |
| 304 | */ |
| 305 | |
| 306 | /* Check if ecdhe named groups are available and pick first entry */ |
| 307 | #if defined(MBEDTLS_ECDH_C) |
| 308 | #if !defined(MBEDTLS_ECP_C) |
| 309 | ((void) ssl); |
| 310 | #endif |
| 311 | #if defined(MBEDTLS_ECP_C) |
| 312 | for ( const mbedtls_ecp_group_id * grp_id = ssl->conf->curve_list; |
| 313 | *grp_id != MBEDTLS_ECP_DP_NONE; |
| 314 | grp_id++ ) |
| 315 | { |
| 316 | const mbedtls_ecp_curve_info *info; |
| 317 | info = mbedtls_ecp_curve_info_from_grp_id( *grp_id ); |
| 318 | #else |
| 319 | for ( const mbedtls_ecp_curve_info *info = mbedtls_ecp_curve_list(); |
| 320 | info->grp_id != MBEDTLS_ECP_DP_NONE; |
| 321 | info++ ) |
| 322 | { |
| 323 | #endif |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 324 | if( info != NULL && mbedtls_ssl_tls13_named_group_is_ecdhe( info->tls_id ) ) |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 325 | { |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 326 | *group_id = info->tls_id; |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 327 | return( 0 ); |
| 328 | } |
| 329 | } |
| 330 | #else |
| 331 | ((void) ssl); |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 332 | ((void) group_id); |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 333 | #endif /* MBEDTLS_ECDH_C */ |
| 334 | |
| 335 | /* |
| 336 | * Add DHE named groups here. |
| 337 | * Check if ecdhe named groups are available and pick first entry |
| 338 | */ |
| 339 | |
| 340 | return( ret ); |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * ssl_tls13_write_key_share_ext |
| 345 | * |
| 346 | * Structure of key_share extension in ClientHelo: |
| 347 | * |
| 348 | * struct { |
| 349 | * NamedGroup group; |
| 350 | * opaque key_exchange<1..2^16-1>; |
| 351 | * } KeyShareEntry; |
| 352 | * struct { |
| 353 | * KeyShareEntry client_shares<0..2^16-1>; |
| 354 | * } KeyShareClientHello; |
| 355 | */ |
| 356 | static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl, |
| 357 | unsigned char *buf, |
| 358 | unsigned char *end, |
| 359 | size_t *olen ) |
| 360 | { |
| 361 | unsigned char *p = buf; |
| 362 | unsigned char *client_shares_ptr; /* Start of client_shares */ |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 363 | size_t client_shares_len; /* Length of client_shares */ |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 364 | uint16_t group_id; |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 365 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 366 | |
| 367 | *olen = 0; |
| 368 | |
| 369 | if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) |
| 370 | return( 0 ); |
| 371 | |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 372 | /* Check if we have space for header and length fields: |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 373 | * - extension_type (2 bytes) |
| 374 | * - extension_data_length (2 bytes) |
| 375 | * - client_shares_length (2 bytes) |
| 376 | */ |
| 377 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 378 | p += 6; |
| 379 | |
| 380 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) ); |
| 381 | |
| 382 | /* HRR could already have requested something else. */ |
| 383 | group_id = ssl->handshake->offered_group_id; |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 384 | if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) && |
| 385 | !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) ) |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 386 | { |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 387 | MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl, |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 388 | &group_id ) ); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Dispatch to type-specific key generation function. |
| 393 | * |
| 394 | * So far, we're only supporting ECDHE. With the introduction |
| 395 | * of PQC KEMs, we'll want to have multiple branches, one per |
| 396 | * type of KEM, and dispatch to the corresponding crypto. And |
| 397 | * only one key share entry is allowed. |
| 398 | */ |
| 399 | client_shares_ptr = p; |
| 400 | #if defined(MBEDTLS_ECDH_C) |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 401 | if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) ) |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 402 | { |
| 403 | /* Pointer of group */ |
| 404 | unsigned char *group_id_ptr = p; |
| 405 | /* Length of key_exchange */ |
| 406 | size_t key_exchange_len; |
| 407 | |
| 408 | /* Check there is space for header of KeyShareEntry |
| 409 | * - group (2 bytes) |
| 410 | * - key_exchange_length (2 bytes) |
| 411 | */ |
| 412 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); |
| 413 | p += 4; |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 414 | ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id, |
| 415 | p, end, |
| 416 | &key_exchange_len ); |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 417 | p += key_exchange_len; |
| 418 | if( ret != 0 ) |
| 419 | return( ret ); |
| 420 | |
| 421 | /* Write group */ |
| 422 | MBEDTLS_PUT_UINT16_BE( group_id, group_id_ptr, 0 ); |
| 423 | /* Write key_exchange_length */ |
| 424 | MBEDTLS_PUT_UINT16_BE( key_exchange_len, group_id_ptr, 2 ); |
| 425 | } |
| 426 | else |
| 427 | #endif /* MBEDTLS_ECDH_C */ |
| 428 | if( 0 /* other KEMs? */ ) |
| 429 | { |
| 430 | /* Do something */ |
| 431 | } |
| 432 | else |
| 433 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 434 | |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 435 | /* Length of client_shares */ |
| 436 | client_shares_len = p - client_shares_ptr; |
| 437 | if( client_shares_len == 0) |
| 438 | { |
| 439 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) ); |
| 440 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 7c522d4 | 2021-09-08 17:55:09 +0800 | [diff] [blame] | 441 | } |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 442 | /* Write extension_type */ |
| 443 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 ); |
| 444 | /* Write extension_data_length */ |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 445 | MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 ); |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 446 | /* Write client_shares_length */ |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 447 | MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 ); |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 448 | |
| 449 | /* Update offered_group_id field */ |
| 450 | ssl->handshake->offered_group_id = group_id; |
| 451 | |
| 452 | /* Output the total length of key_share extension. */ |
| 453 | *olen = p - buf; |
| 454 | |
| 455 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *olen ); |
| 456 | |
| 457 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE; |
| 458 | |
| 459 | cleanup: |
| 460 | |
| 461 | return( ret ); |
| 462 | } |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 463 | |
| 464 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 465 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 466 | /* Write cipher_suites |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 467 | * CipherSuite cipher_suites<2..2^16-2>; |
| 468 | */ |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 469 | static int ssl_tls13_write_client_hello_cipher_suites( |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 470 | mbedtls_ssl_context *ssl, |
| 471 | unsigned char *buf, |
| 472 | unsigned char *end, |
| 473 | size_t *olen ) |
| 474 | { |
Jerry Yu | fec982e | 2021-09-07 17:26:06 +0800 | [diff] [blame] | 475 | unsigned char *p = buf; |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 476 | const int *ciphersuite_list; |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 477 | unsigned char *cipher_suites_ptr; /* Start of the cipher_suites list */ |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 478 | size_t cipher_suites_len; |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 479 | |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 480 | *olen = 0 ; |
| 481 | |
| 482 | /* |
| 483 | * Ciphersuite list |
| 484 | * |
| 485 | * This is a list of the symmetric cipher options supported by |
| 486 | * the client, specifically the record protection algorithm |
| 487 | * ( including secret key length ) and a hash to be used with |
| 488 | * HKDF, in descending order of client preference. |
| 489 | */ |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 490 | ciphersuite_list = ssl->conf->ciphersuite_list; |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 491 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 492 | /* Check there is space for the cipher suite list length (2 bytes). */ |
Jerry Yu | 4e38828 | 2021-09-06 21:28:08 +0800 | [diff] [blame] | 493 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 494 | p += 2; |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 495 | |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 496 | /* Write cipher_suites */ |
Jerry Yu | 4e38828 | 2021-09-06 21:28:08 +0800 | [diff] [blame] | 497 | cipher_suites_ptr = p; |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 498 | for ( size_t i = 0; ciphersuite_list[i] != 0; i++ ) |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 499 | { |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 500 | int cipher_suite = ciphersuite_list[i]; |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 501 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 502 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 503 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite ); |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 504 | if( ciphersuite_info == NULL ) |
| 505 | continue; |
Jerry Yu | dbfb7bd | 2021-09-04 09:58:58 +0800 | [diff] [blame] | 506 | if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver && |
| 507 | MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) ) |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 508 | continue; |
| 509 | |
| 510 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 511 | (unsigned int) cipher_suite, |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 512 | ciphersuite_info->name ) ); |
| 513 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 514 | /* Check there is space for the cipher suite identifier (2 bytes). */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 515 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 516 | MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 ); |
| 517 | p += 2; |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 518 | } |
| 519 | |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 520 | /* Write the cipher_suites length in number of bytes */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 521 | cipher_suites_len = p - cipher_suites_ptr; |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 522 | MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 ); |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 523 | MBEDTLS_SSL_DEBUG_MSG( 3, |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 524 | ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites", |
| 525 | cipher_suites_len/2 ) ); |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 526 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 527 | /* Output the total length of cipher_suites field. */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 528 | *olen = p - buf; |
Jerry Yu | f171e83 | 2021-08-31 18:31:09 +0800 | [diff] [blame] | 529 | |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 530 | return( 0 ); |
| 531 | } |
| 532 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 533 | /* |
| 534 | * Structure of ClientHello message: |
| 535 | * |
| 536 | * struct { |
| 537 | * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 |
| 538 | * Random random; |
| 539 | * opaque legacy_session_id<0..32>; |
| 540 | * CipherSuite cipher_suites<2..2^16-2>; |
| 541 | * opaque legacy_compression_methods<1..2^8-1>; |
| 542 | * Extension extensions<8..2^16-1>; |
| 543 | * } ClientHello; |
| 544 | */ |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 545 | static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 546 | unsigned char *buf, |
Jerry Yu | ef387d7 | 2021-09-02 13:59:41 +0800 | [diff] [blame] | 547 | unsigned char *end, |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 548 | size_t *olen ) |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 549 | { |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 550 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 551 | int ret; |
Jerry Yu | 8c02bb4 | 2021-09-03 21:09:22 +0800 | [diff] [blame] | 552 | unsigned char *extensions_len_ptr; /* Pointer to extensions length */ |
Jerry Yu | 790656a | 2021-09-01 15:51:48 +0800 | [diff] [blame] | 553 | size_t output_len; /* Length of buffer used by function */ |
| 554 | size_t extensions_len; /* Length of the list of extensions*/ |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 555 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 556 | /* Buffer management */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 557 | unsigned char *p = buf; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 558 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 559 | *olen = 0; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 560 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 561 | /* No validation needed here. It has been done by ssl_conf_check() */ |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 562 | ssl->major_ver = ssl->conf->min_major_ver; |
| 563 | ssl->minor_ver = ssl->conf->min_minor_ver; |
| 564 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 565 | /* |
| 566 | * Write legacy_version |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 567 | * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 568 | * |
| 569 | * For TLS 1.3 we use the legacy version number {0x03, 0x03} |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 570 | * instead of the true version number. |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 571 | */ |
Jerry Yu | fec982e | 2021-09-07 17:26:06 +0800 | [diff] [blame] | 572 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 573 | MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 ); |
Jerry Yu | fec982e | 2021-09-07 17:26:06 +0800 | [diff] [blame] | 574 | p += 2; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 575 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 576 | /* Write the random bytes ( random ).*/ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 577 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, CLIENT_HELLO_RANDOM_LEN ); |
| 578 | memcpy( p, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN ); |
Jerry Yu | e885b76 | 2021-08-26 17:32:34 +0800 | [diff] [blame] | 579 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 580 | p, CLIENT_HELLO_RANDOM_LEN ); |
| 581 | p += CLIENT_HELLO_RANDOM_LEN; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 582 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 583 | /* |
| 584 | * Write legacy_session_id |
| 585 | * |
| 586 | * Versions of TLS before TLS 1.3 supported a "session resumption" feature |
| 587 | * which has been merged with pre-shared keys in this version. A client |
| 588 | * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set |
| 589 | * this field to that value. In compatibility mode, this field MUST be |
| 590 | * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate |
| 591 | * a new 32-byte value. This value need not be random but SHOULD be |
| 592 | * unpredictable to avoid implementations fixating on a specific value |
| 593 | * ( also known as ossification ). Otherwise, it MUST be set as a zero-length |
| 594 | * vector ( i.e., a zero-valued single byte length field ). |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 595 | */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 596 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 ); |
| 597 | *p++ = 0; /* session id length set to zero */ |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 598 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 599 | /* Write cipher_suites */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 600 | 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] | 601 | if( ret != 0 ) |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 602 | return( ret ); |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 603 | p += output_len; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 604 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 605 | /* Write legacy_compression_methods |
| 606 | * |
| 607 | * For every TLS 1.3 ClientHello, this vector MUST contain exactly |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 608 | * one byte set to zero, which corresponds to the 'null' compression |
| 609 | * method in prior versions of TLS. |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 610 | */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 611 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 612 | *p++ = 1; |
| 613 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 614 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 615 | /* Write extensions */ |
| 616 | |
| 617 | /* Keeping track of the included extensions */ |
| 618 | ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 619 | |
| 620 | /* First write extensions, then the total length */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 621 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 622 | extensions_len_ptr = p; |
| 623 | p += 2; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 624 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 625 | /* Write supported_versions extension |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 626 | * |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 627 | * Supported Versions Extension is mandatory with TLS 1.3. |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 628 | */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 629 | ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 630 | if( ret != 0 ) |
| 631 | return( ret ); |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 632 | p += output_len; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 633 | |
| 634 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 635 | /* Write supported_groups extension |
| 636 | * |
| 637 | * It is REQUIRED for ECDHE cipher_suites. |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 638 | */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 639 | ret = ssl_tls13_write_supported_groups_ext( ssl, p, end, &output_len ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 640 | if( ret != 0 ) |
| 641 | return( ret ); |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 642 | p += output_len; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 643 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 644 | /* Write key_share extension |
| 645 | * |
| 646 | * We need to send the key shares under three conditions: |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 647 | * 1) A certificate-based ciphersuite is being offered. In this case |
| 648 | * supported_groups and supported_signature extensions have been |
| 649 | * successfully added. |
| 650 | * 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] | 651 | * psk_key_exchange_modes has been added as the last extension. |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 652 | * 3) Or, in case all ciphers are supported ( which includes #1 and #2 |
| 653 | * from above ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 654 | */ |
Jerry Yu | 56fc07f | 2021-09-01 17:48:49 +0800 | [diff] [blame] | 655 | ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 656 | if( ret != 0 ) |
| 657 | return( ret ); |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 658 | p += output_len; |
Jerry Yu | 6a64310 | 2021-08-31 14:40:36 +0800 | [diff] [blame] | 659 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 660 | /* Write signature_algorithms extension |
| 661 | * |
| 662 | * It is REQUIRED for certificate authenticated cipher_suites. |
| 663 | */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 664 | 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] | 665 | if( ret != 0 ) |
| 666 | return( ret ); |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 667 | p += output_len; |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 668 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 669 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 670 | |
| 671 | /* Add more extensions here */ |
| 672 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 673 | /* Write the length of the list of extensions. */ |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 674 | extensions_len = p - extensions_len_ptr - 2; |
Jerry Yu | 790656a | 2021-09-01 15:51:48 +0800 | [diff] [blame] | 675 | MBEDTLS_PUT_UINT16_BE( extensions_len, extensions_len_ptr, 0 ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 676 | 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] | 677 | extensions_len ) ); |
| 678 | 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] | 679 | |
Jerry Yu | bbe0952 | 2021-09-06 21:17:54 +0800 | [diff] [blame] | 680 | *olen = p - buf; |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 681 | return( 0 ); |
| 682 | } |
| 683 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 684 | static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 685 | { |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 686 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); |
| 687 | return( 0 ); |
| 688 | } |
Jerry Yu | ef6b36b | 2021-08-24 16:29:02 +0800 | [diff] [blame] | 689 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 690 | static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ) |
| 691 | { |
| 692 | int ret; |
Jerry Yu | ef6b36b | 2021-08-24 16:29:02 +0800 | [diff] [blame] | 693 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 694 | if( ssl->conf->f_rng == NULL ) |
| 695 | { |
| 696 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); |
| 697 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
| 698 | } |
Jerry Yu | ef6b36b | 2021-08-24 16:29:02 +0800 | [diff] [blame] | 699 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 700 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 701 | ssl->handshake->randbytes, |
Jerry Yu | 08906d0 | 2021-08-31 11:05:27 +0800 | [diff] [blame] | 702 | CLIENT_HELLO_RANDOM_LEN ) ) != 0 ) |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 703 | { |
Jerry Yu | 8c02bb4 | 2021-09-03 21:09:22 +0800 | [diff] [blame] | 704 | MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 705 | return( ret ); |
| 706 | } |
Jerry Yu | 6f13f64 | 2021-08-26 17:18:15 +0800 | [diff] [blame] | 707 | |
| 708 | return( 0 ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 709 | } |
| 710 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 711 | /* |
Jerry Yu | 159c5a0 | 2021-08-31 12:51:25 +0800 | [diff] [blame] | 712 | * Write ClientHello handshake message. |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 713 | */ |
| 714 | static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 715 | { |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 716 | int ret = 0; |
| 717 | unsigned char *buf; |
| 718 | size_t buf_len, msg_len; |
| 719 | |
| 720 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); |
| 721 | |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 722 | MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 723 | |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 724 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( |
| 725 | ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, |
| 726 | &buf, &buf_len ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 727 | |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 728 | MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf, |
Jerry Yu | ef387d7 | 2021-09-02 13:59:41 +0800 | [diff] [blame] | 729 | buf + buf_len, |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 730 | &msg_len ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 731 | |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 732 | mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, |
| 733 | MBEDTLS_SSL_HS_CLIENT_HELLO, |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 734 | msg_len ); |
| 735 | ssl->handshake->update_checksum( ssl, buf, msg_len ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 736 | |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 737 | MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) ); |
| 738 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl, |
| 739 | buf_len, |
| 740 | msg_len ) ); |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 741 | |
| 742 | cleanup: |
| 743 | |
| 744 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); |
| 745 | return ret; |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 746 | } |
| 747 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 748 | int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 749 | { |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 750 | int ret = 0; |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 751 | |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 752 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); |
| 753 | |
| 754 | switch( ssl->state ) |
| 755 | { |
| 756 | /* |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 757 | * ssl->state is initialized as HELLO_REQUEST. It is the same |
| 758 | * as CLIENT_HELLO state. |
Jerry Yu | 92c6b40 | 2021-08-27 16:59:09 +0800 | [diff] [blame] | 759 | */ |
| 760 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 761 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 762 | ret = ssl_tls13_write_client_hello( ssl ); |
| 763 | break; |
| 764 | |
| 765 | case MBEDTLS_SSL_SERVER_HELLO: |
| 766 | // Stop here : we haven't finished whole flow |
| 767 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 768 | mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS ); |
| 769 | break; |
| 770 | |
| 771 | default: |
| 772 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 773 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 774 | } |
| 775 | |
| 776 | return( ret ); |
| 777 | } |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 778 | |
Jerry Yu | 3cc4c2a | 2021-08-06 16:29:08 +0800 | [diff] [blame] | 779 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 780 | |
| 781 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |