blob: defba776a706e8990bdc2298f206fee83024f823 [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 Cron3785c902021-09-20 09:05:36 +0200146- Support of Mbed TLS SSL/TLS related (not DTLS) features:
147
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
245 - Use `<vector_name>_len_ptr` for the name of a variable intended to hold
246 the address of the first byte of the vector length.
247
248 - Use `<vector_name>_ptr` for the name of a variable intended to hold the
249 address of the first byte of the vector value.
250
251 - Use `<vector_name>_end_ptr` for the name of a variable intended to hold
252 the address of the first byte past the vector value.
253
254 Those two last idioms should lower the risk of mis-using one of the address
255 in place of the other one which could potentially lead to some nasty
256 issues.
257
258 Example: `cipher_suites` vector of ClientHello in
Ronald Cron72064b32021-09-27 13:54:28 +0200259 `ssl_tls1_3_write_client_hello_cipher_suites()`
260 ```
261 size_t cipher_suites_len;
262 unsigned char *cipher_suites_len_ptr;
263 unsigned char *cipher_suites_ptr;
264 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200265
266 - Use of MBEDTLS_BYTE_xyz, MBEDTLS_PUT/GET_xyz, MBEDTLS_SSL_CHK_BUF_PTR
267 MBEDTLS_SSL_CHK_BUF_READ_PTR macros where applicable.
268
269 These macros were introduced after the prototype was written thus are
270 likely not to be used in prototype where we now would use them in
271 development.
272
273 The two first types, MBEDTLS_BYTE_xyz and MBEDTLS_PUT/GET_xyz, improve
274 the readability of the code and reduce the risk of writing or reading
275 bytes in the wrong order: we should probably have only MBEDTLS_GET/PUT_*_BE
276 (BE stands for Big-Endian) macros in the TLS 1.3 code.
277
Ronald Cron72064b32021-09-27 13:54:28 +0200278 The two last types, `MBEDTLS_SSL_CHK_BUF_PTR` and
279 `MBEDTLS_SSL_CHK_BUF_READ_PTR`, improve the readability of the code and
Ronald Cron3785c902021-09-20 09:05:36 +0200280 reduce the risk of error in the non-completely-trivial arithmetic to
281 check that we do not write or read past the end of a data buffer. The
282 usage of those macros combined with the following rule mitigate the risk
283 to read/write past the end of a data buffer.
284
Ronald Cron72064b32021-09-27 13:54:28 +0200285 Examples:
286 ```
287 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
288 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
289 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
290 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200291
292 - To mitigate what happened here
293 (https://github.com/ARMmbed/mbedtls/pull/4882#discussion_r701704527) from
294 happening again, use always a local variable named `p` for the reading
295 pointer in functions parsing TLS 1.3 data, and for the writing pointer in
296 functions writing data into an output buffer. The name `p` has been
297 chosen as it was already widely used in TLS code.
298
299 - When an TLS 1.3 structure is written or read by a function or as part of
300 a function, provide as documentation the definition of the structure as
301 it is in the TLS 1.3 specification.
302
303General coding rules:
304
Ronald Cron72064b32021-09-27 13:54:28 +0200305 - We prefer grouping "related statement lines" by not adding blank lines
Ronald Cron3785c902021-09-20 09:05:36 +0200306 between them.
307
308 Example 1:
Ronald Cron72064b32021-09-27 13:54:28 +0200309 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200310 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len );
311 if( ret != 0 )
312 return( ret );
313 buf += output_len;
Ronald Cron72064b32021-09-27 13:54:28 +0200314 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200315
316 Example 2:
Ronald Cron72064b32021-09-27 13:54:28 +0200317 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200318 MBEDTLS_SSL_CHK_BUF_PTR( cipher_suites_iter, end, 2 );
319 MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 );
320 cipher_suites_iter += 2;
Ronald Cron72064b32021-09-27 13:54:28 +0200321 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200322
323 - Use macros for constants that are used in different functions, different
324 places in the code. When a constant is used only locally in a function
325 (like the length in bytes of the vector lengths in functions reading and
326 writing TLS handshake message) there is no need to define a macro for it.
327
Ronald Cron72064b32021-09-27 13:54:28 +0200328 Example: `#define CLIENT_HELLO_RANDOM_LEN 32`
Ronald Cron3785c902021-09-20 09:05:36 +0200329
330 - When declaring a pointer the dereferencing operator should be prepended to
331 the pointer name not appended to the pointer type:
332
Ronald Cron72064b32021-09-27 13:54:28 +0200333 Example: `mbedtls_ssl_context *ssl;`
Ronald Cron3785c902021-09-20 09:05:36 +0200334
335 - Maximum line length is 80 characters.
336
337 Exceptions:
338
339 - string literals can extend beyond 80 characters as we do not want to
340 split them to ease their search in the code base.
341
342 - A line can be more than 80 characters by a few characters if just looking
343 at the 80 first characters is enough to fully understand the line. For
344 example it is generally fine if some closure characters like ";" or ")"
345 are beyond the 80 characters limit.
346
347 - When in successive lines, functions and macros parameters should be aligned
348 vertically.
349
350 Example:
Ronald Cron72064b32021-09-27 13:54:28 +0200351 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200352 int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
353 unsigned hs_type,
354 unsigned char **buf,
355 size_t *buf_len );
Ronald Cron72064b32021-09-27 13:54:28 +0200356 ```