Hanno Becker | 0c3bebf | 2020-06-02 06:32:43 +0100 | [diff] [blame] | 1 | TLS 1.3 Experimental Developments |
Hanno Becker | 9338f9f | 2020-05-31 07:39:50 +0100 | [diff] [blame] | 2 | ================================= |
| 3 | |
| 4 | Overview |
| 5 | -------- |
| 6 | |
| 7 | Mbed TLS doesn't support the TLS 1.3 protocol yet, but a prototype is in development. |
| 8 | Stable parts of this prototype that can be independently tested are being successively |
| 9 | upstreamed under the guard of the following macro: |
| 10 | |
| 11 | ``` |
| 12 | MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL |
| 13 | ``` |
| 14 | |
| 15 | This macro will likely be renamed to `MBEDTLS_SSL_PROTO_TLS1_3` once a minimal viable |
| 16 | implementation of the TLS 1.3 protocol is available. |
| 17 | |
Bence Szépkúti | bb0cfeb | 2021-05-28 09:42:25 +0200 | [diff] [blame] | 18 | See the [documentation of `MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL`](../../include/mbedtls/mbedtls_config.h) |
Hanno Becker | 9338f9f | 2020-05-31 07:39:50 +0100 | [diff] [blame] | 19 | for more information. |
| 20 | |
| 21 | Status |
| 22 | ------ |
| 23 | |
| 24 | The following lists which parts of the TLS 1.3 prototype have already been upstreamed |
| 25 | together with their level of testing: |
| 26 | |
| 27 | * TLS 1.3 record protection mechanisms |
| 28 | |
| 29 | The record protection routines `mbedtls_ssl_{encrypt|decrypt}_buf()` have been extended |
| 30 | to support the modified TLS 1.3 record protection mechanism, including modified computation |
| 31 | of AAD, IV, and the introduction of a flexible padding. |
| 32 | |
| 33 | Those record protection routines have unit tests in `test_suite_ssl` alongside the |
| 34 | tests for the other record protection routines. |
| 35 | |
| 36 | TODO: Add some test vectors from RFC 8448. |
Hanno Becker | 5a83d29 | 2020-06-02 06:33:00 +0100 | [diff] [blame] | 37 | |
| 38 | - The HKDF key derivation function on which the TLS 1.3 key schedule is based, |
| 39 | is already present as an independent module controlled by `MBEDTLS_HKDF_C` |
| 40 | independently of the development of the TLS 1.3 prototype. |
Hanno Becker | b11c309 | 2020-08-10 17:00:19 +0100 | [diff] [blame] | 41 | |
| 42 | - The TLS 1.3-specific HKDF-based key derivation functions (see RFC 8446): |
| 43 | * HKDF-Expand-Label |
| 44 | * Derive-Secret |
| 45 | - Secret evolution |
| 46 | * The traffic {Key,IV} generation from secret |
| 47 | Those functions are implemented in `library/ssl_tls13_keys.c` and |
| 48 | tested in `test_suite_ssl` using test vectors from RFC 8448 and |
| 49 | https://tls13.ulfheim.net/. |
Hanno Becker | 7594c68 | 2021-03-05 05:17:11 +0000 | [diff] [blame] | 50 | |
| 51 | - New TLS Message Processing Stack (MPS) |
| 52 | |
| 53 | The TLS 1.3 prototype is developed alongside a rewrite of the TLS messaging layer, |
| 54 | encompassing low-level details such as record parsing, handshake reassembly, and |
| 55 | DTLS retransmission state machine. |
| 56 | |
| 57 | MPS has the following components: |
| 58 | - Layer 1 (Datagram handling) |
| 59 | - Layer 2 (Record handling) |
| 60 | - Layer 3 (Message handling) |
| 61 | - Layer 4 (Retransmission State Machine) |
| 62 | - Reader (Abstracted pointer arithmetic and reassembly logic for incoming data) |
| 63 | - Writer (Abstracted pointer arithmetic and fragmentation logic for outgoing data) |
| 64 | |
| 65 | Of those components, the following have been upstreamed |
| 66 | as part of `MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL`: |
| 67 | |
| 68 | - Reader ([`library/mps_reader.h`](../../library/mps_reader.h)) |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 69 | |
| 70 | |
| 71 | MVP definition |
| 72 | -------------- |
| 73 | |
| 74 | The TLS 1.3 MVP implements only the client side of the protocol. |
| 75 | The TLS 1.3 MVP does not support the handling of server HelloRetryRequest and |
| 76 | CertificateRequest messages. If it receives one of those messages, it aborts |
| 77 | the handshake with an handshake_failure closure alert. |
| 78 | |
| 79 | - Supported cipher suites: depends on the library configuration. Potentially |
| 80 | all of them: |
| 81 | TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256, |
| 82 | TLS_AES_128_CCM_SHA256 and TLS_AES_128_CCM_8_SHA256. |
| 83 | |
| 84 | - Supported ClientHello extensions: |
| 85 | |
Ronald Cron | 023987f | 2021-09-27 11:59:25 +0200 | [diff] [blame] | 86 | | Extension | MVP | Prototype (1) | |
| 87 | | ---------------------------- | ------- | ------------- | |
| 88 | | server_name | no | YES | |
| 89 | | max_fragment_length | no | YES | |
| 90 | | status_request | no | no | |
| 91 | | supported_groups | YES | YES | |
| 92 | | signature_algorithms | YES | YES | |
| 93 | | use_srtp | no | no | |
| 94 | | heartbeat | no | no | |
| 95 | | apln | no | YES | |
| 96 | | signed_certificate_timestamp | no | no | |
| 97 | | client_certificate_type | no | no | |
| 98 | | server_certificate_type | no | no | |
| 99 | | padding | no | no | |
| 100 | | key_share | YES | YES | |
| 101 | | pre_shared_key | no | YES | |
| 102 | | psk_key_exchange_modes | no | YES | |
| 103 | | early_data | no | YES | |
| 104 | | cookie | no | YES | |
| 105 | | supported_versions | YES | YES | |
| 106 | | certificate_authorities | no | no | |
| 107 | | post_handshake_auth | no | no | |
| 108 | | signature_algorithms_cert | no | no | |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 109 | |
Ronald Cron | 023987f | 2021-09-27 11:59:25 +0200 | [diff] [blame] | 110 | (1) This is just for comparison. |
| 111 | |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 112 | |
| 113 | - Supported groups: depends on the library configuration. |
| 114 | Potentially all ECDHE groups: |
| 115 | secp256r1, secp384r1, secp521r1(0x0019), x25519, x448. |
| 116 | |
| 117 | - Supported signature algorithms: depends on the library configuration. |
| 118 | Potentially: |
| 119 | ecdsa_secp256r1_sha256, ecdsa_secp384r1_sha384, ecdsa_secp521r1_sha512, |
| 120 | rsa_pss_rsae_sha256. |
| 121 | |
| 122 | - Supported versions: only TLS 1.3 |
| 123 | |
| 124 | - Support of Mbed TLS SSL/TLS related (not DTLS) features: |
| 125 | |
| 126 | The TLS 1.3 MVP is compatible with all TLS 1.2 configuration options in the |
| 127 | sense that when enabling the TLS 1.3 MVP in the library there is no need to |
| 128 | modify the configuration for TLS 1.2. Mbed TLS SSL/TLS related features are |
| 129 | not supported or not applicable to the TLS 1.3 MVP: |
| 130 | |
Ronald Cron | 023987f | 2021-09-27 11:59:25 +0200 | [diff] [blame] | 131 | | Mbed TLS configuration option | Support | |
| 132 | | ---------------------------------------- | ------- | |
| 133 | | MBEDTLS_SSL_ALL_ALERT_MESSAGES | no | |
| 134 | | MBEDTLS_SSL_ASYNC_PRIVATE | no | |
| 135 | | MBEDTLS_SSL_CONTEXT_SERIALIZATION | no | |
| 136 | | MBEDTLS_SSL_DEBUG_ALL | no | |
| 137 | | MBEDTLS_SSL_ENCRYPT_THEN_MAC | n/a | |
| 138 | | MBEDTLS_SSL_EXTENDED_MASTER_SECRET | n/a | |
| 139 | | MBEDTLS_SSL_KEEP_PEER_CERTIFICATE | no | |
| 140 | | MBEDTLS_SSL_RENEGOTIATION | n/a | |
| 141 | | MBEDTLS_SSL_MAX_FRAGMENT_LENGTH | no | |
| 142 | | | | |
| 143 | | MBEDTLS_SSL_SESSION_TICKETS | no | |
| 144 | | MBEDTLS_SSL_EXPORT_KEYS | no (1) | |
| 145 | | MBEDTLS_SSL_SERVER_NAME_INDICATION | no | |
| 146 | | MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH | no | |
| 147 | | | | |
| 148 | | MBEDTLS_ECP_RESTARTABLE | no | |
| 149 | | MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED | no | |
| 150 | | | | |
| 151 | | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED | n/a (2) | |
| 152 | | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED | n/a | |
| 153 | | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED | n/a | |
| 154 | | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED | n/a | |
| 155 | | MBEDTLS_KEY_EXCHANGE_RSA_ENABLED | n/a | |
| 156 | | MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED | n/a | |
| 157 | | MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED | n/a | |
| 158 | | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED | n/a | |
| 159 | | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED | n/a | |
| 160 | | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED | n/a | |
| 161 | | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED | n/a | |
| 162 | | | | |
| 163 | | MBEDTLS_USE_PSA_CRYPTO | no | |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 164 | |
Ronald Cron | 023987f | 2021-09-27 11:59:25 +0200 | [diff] [blame] | 165 | (1) Some support has already been upstreamed but it is incomplete. |
Ronald Cron | 1fa5088 | 2021-09-27 12:06:52 +0200 | [diff] [blame^] | 166 | (2) Key exchange configuration options for TLS 1.3 will likely to be |
| 167 | organized around the notion of key exchange mode along the line |
| 168 | of the MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_NONE/PSK/PSK_EPHEMERAL/EPHEMERAL |
| 169 | runtime configuration macros. |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 170 | |
| 171 | Not in the plan yet but probably necessary for a viable client: |
| 172 | - server_name extension |
| 173 | - support for HelloRetryRequest |
| 174 | - fallback to TLS 1.2 |
| 175 | |
| 176 | Coding rules checklist for TLS 1.3 |
| 177 | ---------------------------------- |
| 178 | |
| 179 | The following coding rules are aimed to be a checklist for TLS 1.3 upstreaming |
| 180 | work to reduce review rounds and the number of comments in each round. They |
| 181 | come along (do NOT replace) the project coding rules |
| 182 | (https://tls.mbed.org/kb/development/mbedtls-coding-standards). They have been |
| 183 | established and discussed following the review of #4882 that was the |
| 184 | PR upstreaming the first part of TLS 1.3 ClientHello writing code. |
| 185 | |
| 186 | TLS 1.3 specific coding rules: |
| 187 | |
| 188 | - TLS 1.3 specific C modules, headers, static functions names are prefixed |
| 189 | with `ssl_tls1_3_`. The same applies to structures and types that are |
| 190 | internal to C modules. |
| 191 | |
| 192 | - TLS 1.3 specific exported functions, macros, structures and types are |
| 193 | prefixed with `mbedtls_ssl_tls1_3_`. |
| 194 | |
| 195 | - The names of macros and variables related to a field or structure in the |
| 196 | TLS 1.3 specification should contain as far as possible the field name as |
| 197 | it is in the specification. If the field name is `too long` and we prefer |
| 198 | to introduce some kind of abbreviation of it, use the same abbreviation |
| 199 | everywhere in the code. |
| 200 | |
| 201 | Example 1: #define CLIENT_HELLO_RANDOM_LEN 32, macro for the length of the |
| 202 | `random` field of the ClientHello message. |
| 203 | |
| 204 | Example 2 (consistent abbreviation): mbedtls_ssl_tls1_3_write_sig_alg_ext() |
| 205 | and MBEDTLS_TLS_EXT_SIG_ALG, `sig_alg` standing for |
| 206 | `signature_algorithms`. |
| 207 | |
| 208 | - Regarding vectors that are represented by a length followed by their value |
| 209 | in the data exchanged between servers and clients: |
| 210 | |
| 211 | - Use `<vector name>_len` for the name of a variable used to compute the |
| 212 | length in bytes of the vector, where <vector name> is the name of the |
| 213 | vector as defined in the TLS 1.3 specification. |
| 214 | |
| 215 | - Use `<vector_name>_len_ptr` for the name of a variable intended to hold |
| 216 | the address of the first byte of the vector length. |
| 217 | |
| 218 | - Use `<vector_name>_ptr` for the name of a variable intended to hold the |
| 219 | address of the first byte of the vector value. |
| 220 | |
| 221 | - Use `<vector_name>_end_ptr` for the name of a variable intended to hold |
| 222 | the address of the first byte past the vector value. |
| 223 | |
| 224 | Those two last idioms should lower the risk of mis-using one of the address |
| 225 | in place of the other one which could potentially lead to some nasty |
| 226 | issues. |
| 227 | |
| 228 | Example: `cipher_suites` vector of ClientHello in |
| 229 | ssl_tls1_3_write_client_hello_cipher_suites() |
| 230 | |
| 231 | size_t cipher_suites_len; |
| 232 | unsigned char *cipher_suites_len_ptr; |
| 233 | unsigned char *cipher_suites_ptr; |
| 234 | |
| 235 | - Use of MBEDTLS_BYTE_xyz, MBEDTLS_PUT/GET_xyz, MBEDTLS_SSL_CHK_BUF_PTR |
| 236 | MBEDTLS_SSL_CHK_BUF_READ_PTR macros where applicable. |
| 237 | |
| 238 | These macros were introduced after the prototype was written thus are |
| 239 | likely not to be used in prototype where we now would use them in |
| 240 | development. |
| 241 | |
| 242 | The two first types, MBEDTLS_BYTE_xyz and MBEDTLS_PUT/GET_xyz, improve |
| 243 | the readability of the code and reduce the risk of writing or reading |
| 244 | bytes in the wrong order: we should probably have only MBEDTLS_GET/PUT_*_BE |
| 245 | (BE stands for Big-Endian) macros in the TLS 1.3 code. |
| 246 | |
| 247 | The two last types, MBEDTLS_SSL_CHK_BUF_PTR and |
| 248 | MBEDTLS_SSL_CHK_BUF_READ_PTR, improve the readability of the code and |
| 249 | reduce the risk of error in the non-completely-trivial arithmetic to |
| 250 | check that we do not write or read past the end of a data buffer. The |
| 251 | usage of those macros combined with the following rule mitigate the risk |
| 252 | to read/write past the end of a data buffer. |
| 253 | |
| 254 | Examples: hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); |
| 255 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 ); |
| 256 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 ); |
| 257 | |
| 258 | - To mitigate what happened here |
| 259 | (https://github.com/ARMmbed/mbedtls/pull/4882#discussion_r701704527) from |
| 260 | happening again, use always a local variable named `p` for the reading |
| 261 | pointer in functions parsing TLS 1.3 data, and for the writing pointer in |
| 262 | functions writing data into an output buffer. The name `p` has been |
| 263 | chosen as it was already widely used in TLS code. |
| 264 | |
| 265 | - When an TLS 1.3 structure is written or read by a function or as part of |
| 266 | a function, provide as documentation the definition of the structure as |
| 267 | it is in the TLS 1.3 specification. |
| 268 | |
| 269 | General coding rules: |
| 270 | |
| 271 | - We prefer grouping `related statement lines` by not adding blank lines |
| 272 | between them. |
| 273 | |
| 274 | Example 1: |
| 275 | |
| 276 | ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len ); |
| 277 | if( ret != 0 ) |
| 278 | return( ret ); |
| 279 | buf += output_len; |
| 280 | |
| 281 | Example 2: |
| 282 | |
| 283 | MBEDTLS_SSL_CHK_BUF_PTR( cipher_suites_iter, end, 2 ); |
| 284 | MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 ); |
| 285 | cipher_suites_iter += 2; |
| 286 | |
| 287 | - Use macros for constants that are used in different functions, different |
| 288 | places in the code. When a constant is used only locally in a function |
| 289 | (like the length in bytes of the vector lengths in functions reading and |
| 290 | writing TLS handshake message) there is no need to define a macro for it. |
| 291 | |
| 292 | Example: #define CLIENT_HELLO_RANDOM_LEN 32 |
| 293 | |
| 294 | - When declaring a pointer the dereferencing operator should be prepended to |
| 295 | the pointer name not appended to the pointer type: |
| 296 | |
| 297 | Example: mbedtls_ssl_context *ssl; |
| 298 | |
| 299 | - Maximum line length is 80 characters. |
| 300 | |
| 301 | Exceptions: |
| 302 | |
| 303 | - string literals can extend beyond 80 characters as we do not want to |
| 304 | split them to ease their search in the code base. |
| 305 | |
| 306 | - A line can be more than 80 characters by a few characters if just looking |
| 307 | at the 80 first characters is enough to fully understand the line. For |
| 308 | example it is generally fine if some closure characters like ";" or ")" |
| 309 | are beyond the 80 characters limit. |
| 310 | |
| 311 | - When in successive lines, functions and macros parameters should be aligned |
| 312 | vertically. |
| 313 | |
| 314 | Example: |
| 315 | int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, |
| 316 | unsigned hs_type, |
| 317 | unsigned char **buf, |
| 318 | size_t *buf_len ); |