blob: 314928fe8d458c233a579358170b484869ec27c7 [file] [log] [blame] [view]
Hanno Becker0c3bebf2020-06-02 06:32:43 +01001TLS 1.3 Experimental Developments
Hanno Becker9338f9f2020-05-31 07:39:50 +01002=================================
3
4Overview
5--------
6
7Mbed TLS doesn't support the TLS 1.3 protocol yet, but a prototype is in development.
8Stable parts of this prototype that can be independently tested are being successively
9upstreamed under the guard of the following macro:
10
11```
12MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL
13```
14
15This macro will likely be renamed to `MBEDTLS_SSL_PROTO_TLS1_3` once a minimal viable
16implementation of the TLS 1.3 protocol is available.
17
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020018See the [documentation of `MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL`](../../include/mbedtls/mbedtls_config.h)
Hanno Becker9338f9f2020-05-31 07:39:50 +010019for more information.
20
21Status
22------
23
24The following lists which parts of the TLS 1.3 prototype have already been upstreamed
25together 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 Becker5a83d292020-06-02 06:33:00 +010037
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 Beckerb11c3092020-08-10 17:00:19 +010041
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 Becker7594c682021-03-05 05:17:11 +000050
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 Cron3785c902021-09-20 09:05:36 +020069
70
71MVP definition
72--------------
73
74The TLS 1.3 MVP implements only the client side of the protocol.
75The TLS 1.3 MVP does not support the handling of server HelloRetryRequest and
76CertificateRequest messages. If it receives one of those messages, it aborts
Ronald Cron004df8a2021-09-27 12:12:00 +020077the handshake with an handshake_failure closure alert and the
78`mbedtls_ssl_handshake()` returns in error with the
79`MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE` error code.
Ronald Cron3785c902021-09-20 09:05:36 +020080
81- Supported cipher suites: depends on the library configuration. Potentially
82 all of them:
83 TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256,
84 TLS_AES_128_CCM_SHA256 and TLS_AES_128_CCM_8_SHA256.
85
86- Supported ClientHello extensions:
87
Ronald Cron023987f2021-09-27 11:59:25 +020088 | Extension | MVP | Prototype (1) |
89 | ---------------------------- | ------- | ------------- |
Ronald Cron85e51082021-09-27 12:13:16 +020090 | server_name | YES | YES |
Ronald Cron023987f2021-09-27 11:59:25 +020091 | max_fragment_length | no | YES |
92 | status_request | no | no |
93 | supported_groups | YES | YES |
94 | signature_algorithms | YES | YES |
95 | use_srtp | no | no |
96 | heartbeat | no | no |
97 | apln | no | YES |
98 | signed_certificate_timestamp | no | no |
99 | client_certificate_type | no | no |
100 | server_certificate_type | no | no |
101 | padding | no | no |
Ronald Cron3160d702021-09-27 13:27:21 +0200102 | key_share | YES (2) | YES |
Ronald Cron023987f2021-09-27 11:59:25 +0200103 | pre_shared_key | no | YES |
104 | psk_key_exchange_modes | no | YES |
105 | early_data | no | YES |
106 | cookie | no | YES |
Ronald Cron3160d702021-09-27 13:27:21 +0200107 | supported_versions | YES (3) | YES |
Ronald Cron023987f2021-09-27 11:59:25 +0200108 | certificate_authorities | no | no |
109 | post_handshake_auth | no | no |
110 | signature_algorithms_cert | no | no |
Ronald Cron3785c902021-09-20 09:05:36 +0200111
Ronald Cron023987f2021-09-27 11:59:25 +0200112 (1) This is just for comparison.
113
Ronald Cron3160d702021-09-27 13:27:21 +0200114 (2) The MVP sends one shared secret corresponding to the configured preferred
115 group. The preferred group is the group of the first curve in the list of
116 allowed curves as defined by the configuration. By default, it is the
117 mandatory group as defined by section 9.1 of the specification,
118 `secp256r1`. The list of allowed curves can be set through the
119 `mbedtls_ssl_conf_curves()` API.
120
121 (3) The MVP proposes only TLS 1.3 and does not support version negociation.
122 Out-of-protocol fallback is supported though if the Mbed TLS library
123 has been built to support both TLS 1.3 and TLS 1.2: just set the
124 maximum of the minor version of the SSL configuration to
125 MBEDTLS_SSL_MINOR_VERSION_3 (`mbedtls_ssl_conf_min_version()` API) and
126 re-initiate a server handshake.
Ronald Cron3785c902021-09-20 09:05:36 +0200127
128- Supported groups: depends on the library configuration.
Ronald Cronc3b510f2021-09-27 13:36:33 +0200129 Minimally (as defined in section 9.1 of the TLS 1.3 specification):
130 secp256r1 and x25519.
Ronald Cron3785c902021-09-20 09:05:36 +0200131
Ronald Cronc3b510f2021-09-27 13:36:33 +0200132 Furthermore, depending on the library configuration, potentially:
133 secp384r1 and secp521r1.
134
135 Finite field groups (DHE) are not supported.
136
137- Supported signature algorithms(both for certificates and CertificateVerify):
138 Minimally (as defined in section 9.1 of the TLS 1.3 specification):
139 rsa_pkcs1_sha256, rsa_pss_rsae_sha256 and ecdsa_secp256r1_sha256
140
141 Furthermore, depending on the library configuration, potentially:
142 ecdsa_secp384r1_sha384 and ecdsa_secp521r1_sha512
143
144- Supported versions: only TLS 1.3, version negotiation is not supported.
Ronald Cron3785c902021-09-20 09:05:36 +0200145
Ronald Cron3e7c4032021-09-27 14:22:38 +0200146- Compatibility with existing SSL/TLS build options:
Ronald Cron3785c902021-09-20 09:05:36 +0200147
148 The TLS 1.3 MVP is compatible with all TLS 1.2 configuration options in the
149 sense that when enabling the TLS 1.3 MVP in the library there is no need to
150 modify the configuration for TLS 1.2. Mbed TLS SSL/TLS related features are
151 not supported or not applicable to the TLS 1.3 MVP:
152
Ronald Cron023987f2021-09-27 11:59:25 +0200153 | Mbed TLS configuration option | Support |
154 | ---------------------------------------- | ------- |
155 | MBEDTLS_SSL_ALL_ALERT_MESSAGES | no |
156 | MBEDTLS_SSL_ASYNC_PRIVATE | no |
157 | MBEDTLS_SSL_CONTEXT_SERIALIZATION | no |
158 | MBEDTLS_SSL_DEBUG_ALL | no |
159 | MBEDTLS_SSL_ENCRYPT_THEN_MAC | n/a |
160 | MBEDTLS_SSL_EXTENDED_MASTER_SECRET | n/a |
161 | MBEDTLS_SSL_KEEP_PEER_CERTIFICATE | no |
162 | MBEDTLS_SSL_RENEGOTIATION | n/a |
163 | MBEDTLS_SSL_MAX_FRAGMENT_LENGTH | no |
164 | | |
165 | MBEDTLS_SSL_SESSION_TICKETS | no |
166 | MBEDTLS_SSL_EXPORT_KEYS | no (1) |
167 | MBEDTLS_SSL_SERVER_NAME_INDICATION | no |
168 | MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH | no |
169 | | |
170 | MBEDTLS_ECP_RESTARTABLE | no |
171 | MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED | no |
172 | | |
173 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED | n/a (2) |
174 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED | n/a |
175 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED | n/a |
176 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED | n/a |
177 | MBEDTLS_KEY_EXCHANGE_RSA_ENABLED | n/a |
178 | MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED | n/a |
179 | MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED | n/a |
180 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED | n/a |
181 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED | n/a |
182 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED | n/a |
183 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED | n/a |
184 | | |
185 | MBEDTLS_USE_PSA_CRYPTO | no |
Ronald Cron3785c902021-09-20 09:05:36 +0200186
Ronald Cron023987f2021-09-27 11:59:25 +0200187 (1) Some support has already been upstreamed but it is incomplete.
Ronald Cron1fa50882021-09-27 12:06:52 +0200188 (2) Key exchange configuration options for TLS 1.3 will likely to be
189 organized around the notion of key exchange mode along the line
190 of the MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_NONE/PSK/PSK_EPHEMERAL/EPHEMERAL
191 runtime configuration macros.
Ronald Cron3785c902021-09-20 09:05:36 +0200192
Ronald Cron660c7232021-09-27 13:40:53 +0200193- Quality considerations
194 - Standard Mbed TLS review bar
195 - Interoperability testing with OpenSSL and GnuTLS. Test with all the
196 cipher suites supported by OpenSSL/GnuTLS server with and without
197 certificate base authentication.
198 - Negative testing against OpenSSL/GnuTLS servers with which the
199 handshake fails due to imcompatibility with the capabilities of the
200 MVP: TLS 1.2 or 1.1 server, server sending an HelloRetryRequest message in
201 response to the MVP ClientHello, server sending a CertificateRequest
202 message ...
203
Ronald Cron3785c902021-09-20 09:05:36 +0200204Coding rules checklist for TLS 1.3
205----------------------------------
206
207The following coding rules are aimed to be a checklist for TLS 1.3 upstreaming
208work to reduce review rounds and the number of comments in each round. They
209come along (do NOT replace) the project coding rules
210(https://tls.mbed.org/kb/development/mbedtls-coding-standards). They have been
211established and discussed following the review of #4882 that was the
212PR upstreaming the first part of TLS 1.3 ClientHello writing code.
213
214TLS 1.3 specific coding rules:
215
216 - TLS 1.3 specific C modules, headers, static functions names are prefixed
Ronald Cronb1944662021-09-27 13:56:46 +0200217 with `ssl_tls13_`. The same applies to structures and types that are
Ronald Cron3785c902021-09-20 09:05:36 +0200218 internal to C modules.
219
Ronald Cronb1944662021-09-27 13:56:46 +0200220 - TLS 1.3 specific exported functions, structures and types are
221 prefixed with `mbedtls_ssl_tls13_`.
222
223 - Use TLS1_3 in TLS 1.3 specific macros.
Ronald Cron3785c902021-09-20 09:05:36 +0200224
225 - The names of macros and variables related to a field or structure in the
226 TLS 1.3 specification should contain as far as possible the field name as
Ronald Cron72064b32021-09-27 13:54:28 +0200227 it is in the specification. If the field name is "too long" and we prefer
Ronald Cron3785c902021-09-20 09:05:36 +0200228 to introduce some kind of abbreviation of it, use the same abbreviation
229 everywhere in the code.
230
231 Example 1: #define CLIENT_HELLO_RANDOM_LEN 32, macro for the length of the
232 `random` field of the ClientHello message.
233
Ronald Cron72064b32021-09-27 13:54:28 +0200234 Example 2 (consistent abbreviation): `mbedtls_ssl_tls1_3_write_sig_alg_ext()`
235 and `MBEDTLS_TLS_EXT_SIG_ALG`, `sig_alg` standing for
Ronald Cron3785c902021-09-20 09:05:36 +0200236 `signature_algorithms`.
237
238 - Regarding vectors that are represented by a length followed by their value
239 in the data exchanged between servers and clients:
240
241 - Use `<vector name>_len` for the name of a variable used to compute the
242 length in bytes of the vector, where <vector name> is the name of the
243 vector as defined in the TLS 1.3 specification.
244
Ronald Cron99733f02021-09-27 13:58:21 +0200245 - Use `p_<vector_name>_len` for the name of a variable intended to hold
Ronald Cron3785c902021-09-20 09:05:36 +0200246 the address of the first byte of the vector length.
247
Ronald Cron99733f02021-09-27 13:58:21 +0200248 - Use `<vector_name>` for the name of a variable intended to hold the
Ronald Cron3785c902021-09-20 09:05:36 +0200249 address of the first byte of the vector value.
250
Ronald Cron99733f02021-09-27 13:58:21 +0200251 - Use `<vector_name>_end` for the name of a variable intended to hold
Ronald Cron3785c902021-09-20 09:05:36 +0200252 the address of the first byte past the vector value.
253
Ronald Cron99733f02021-09-27 13:58:21 +0200254 Those idioms should lower the risk of mis-using one of the address in place
255 of another one which could potentially lead to some nasty issues.
Ronald Cron3785c902021-09-20 09:05:36 +0200256
257 Example: `cipher_suites` vector of ClientHello in
Ronald Cron72064b32021-09-27 13:54:28 +0200258 `ssl_tls1_3_write_client_hello_cipher_suites()`
259 ```
260 size_t cipher_suites_len;
Ronald Cron99733f02021-09-27 13:58:21 +0200261 unsigned char *p_cipher_suites_len;
262 unsigned char *cipher_suites;
Ronald Cron72064b32021-09-27 13:54:28 +0200263 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200264
Ronald Cronfecda8d2021-09-27 13:59:38 +0200265 - Where applicable, use:
266 - the macros to extract a byte from a multi-byte integer MBEDTLS_BYTE_{0-8}.
267 - the macros to write in memory in big-endian order a multi-byte integer
268 MBEDTLS_PUT_UINT{8|16|32|64}_BE.
269 - the macros to read from memory a multi-byte integer in big-endian order
270 MBEDTLS_GET_UINT{8|16|32|64}_BE.
271 - the macro to check for space when writing into an output buffer
272 `MBEDTLS_SSL_CHK_BUF_PTR`.
273 - the macro to check for data when reading from an input buffer
274 `MBEDTLS_SSL_CHK_BUF_READ_PTR`.
Ronald Cron3785c902021-09-20 09:05:36 +0200275
276 These macros were introduced after the prototype was written thus are
277 likely not to be used in prototype where we now would use them in
278 development.
279
Ronald Cronfecda8d2021-09-27 13:59:38 +0200280 The three first types, MBEDTLS_BYTE_{0-8}, MBEDTLS_PUT_UINT{8|16|32|64}_BE
281 and MBEDTLS_GET_UINT{8|16|32|64}_BE improve the readability of the code and
282 reduce the risk of writing or reading bytes in the wrong order.
Ronald Cron3785c902021-09-20 09:05:36 +0200283
Ronald Cron72064b32021-09-27 13:54:28 +0200284 The two last types, `MBEDTLS_SSL_CHK_BUF_PTR` and
285 `MBEDTLS_SSL_CHK_BUF_READ_PTR`, improve the readability of the code and
Ronald Cron3785c902021-09-20 09:05:36 +0200286 reduce the risk of error in the non-completely-trivial arithmetic to
287 check that we do not write or read past the end of a data buffer. The
288 usage of those macros combined with the following rule mitigate the risk
289 to read/write past the end of a data buffer.
290
Ronald Cron72064b32021-09-27 13:54:28 +0200291 Examples:
292 ```
293 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
294 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
295 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
296 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200297
298 - To mitigate what happened here
299 (https://github.com/ARMmbed/mbedtls/pull/4882#discussion_r701704527) from
300 happening again, use always a local variable named `p` for the reading
301 pointer in functions parsing TLS 1.3 data, and for the writing pointer in
Ronald Cron3e7c4032021-09-27 14:22:38 +0200302 functions writing data into an output buffer and only that variable. The
303 name `p` has been chosen as it was already widely used in TLS code.
Ronald Cron3785c902021-09-20 09:05:36 +0200304
305 - When an TLS 1.3 structure is written or read by a function or as part of
306 a function, provide as documentation the definition of the structure as
307 it is in the TLS 1.3 specification.
308
309General coding rules:
310
Ronald Cron72064b32021-09-27 13:54:28 +0200311 - We prefer grouping "related statement lines" by not adding blank lines
Ronald Cron3785c902021-09-20 09:05:36 +0200312 between them.
313
314 Example 1:
Ronald Cron72064b32021-09-27 13:54:28 +0200315 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200316 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len );
317 if( ret != 0 )
318 return( ret );
319 buf += output_len;
Ronald Cron72064b32021-09-27 13:54:28 +0200320 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200321
322 Example 2:
Ronald Cron72064b32021-09-27 13:54:28 +0200323 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200324 MBEDTLS_SSL_CHK_BUF_PTR( cipher_suites_iter, end, 2 );
325 MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 );
326 cipher_suites_iter += 2;
Ronald Cron72064b32021-09-27 13:54:28 +0200327 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200328
329 - Use macros for constants that are used in different functions, different
330 places in the code. When a constant is used only locally in a function
331 (like the length in bytes of the vector lengths in functions reading and
332 writing TLS handshake message) there is no need to define a macro for it.
333
Ronald Cron72064b32021-09-27 13:54:28 +0200334 Example: `#define CLIENT_HELLO_RANDOM_LEN 32`
Ronald Cron3785c902021-09-20 09:05:36 +0200335
336 - When declaring a pointer the dereferencing operator should be prepended to
337 the pointer name not appended to the pointer type:
338
Ronald Cron72064b32021-09-27 13:54:28 +0200339 Example: `mbedtls_ssl_context *ssl;`
Ronald Cron3785c902021-09-20 09:05:36 +0200340
341 - Maximum line length is 80 characters.
342
343 Exceptions:
344
345 - string literals can extend beyond 80 characters as we do not want to
346 split them to ease their search in the code base.
347
348 - A line can be more than 80 characters by a few characters if just looking
349 at the 80 first characters is enough to fully understand the line. For
350 example it is generally fine if some closure characters like ";" or ")"
351 are beyond the 80 characters limit.
352
353 - When in successive lines, functions and macros parameters should be aligned
354 vertically.
355
356 Example:
Ronald Cron72064b32021-09-27 13:54:28 +0200357 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200358 int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
359 unsigned hs_type,
360 unsigned char **buf,
361 size_t *buf_len );
Ronald Cron72064b32021-09-27 13:54:28 +0200362 ```