blob: 1eef961a80e10f3253052875a2d0f0d1bc3463ce [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
77the 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 Cron023987f2021-09-27 11:59:25 +020086 | 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 Cron3785c902021-09-20 09:05:36 +0200109
Ronald Cron023987f2021-09-27 11:59:25 +0200110 (1) This is just for comparison.
111
Ronald Cron3785c902021-09-20 09:05:36 +0200112
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 Cron023987f2021-09-27 11:59:25 +0200131 | 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 Cron3785c902021-09-20 09:05:36 +0200164
Ronald Cron023987f2021-09-27 11:59:25 +0200165 (1) Some support has already been upstreamed but it is incomplete.
166 (2) Make sense in TLS 1.3 context but their current definition is TLS 1.2 only.
Ronald Cron3785c902021-09-20 09:05:36 +0200167
168Not in the plan yet but probably necessary for a viable client:
169- server_name extension
170- support for HelloRetryRequest
171- fallback to TLS 1.2
172
173Coding rules checklist for TLS 1.3
174----------------------------------
175
176The following coding rules are aimed to be a checklist for TLS 1.3 upstreaming
177work to reduce review rounds and the number of comments in each round. They
178come along (do NOT replace) the project coding rules
179(https://tls.mbed.org/kb/development/mbedtls-coding-standards). They have been
180established and discussed following the review of #4882 that was the
181PR upstreaming the first part of TLS 1.3 ClientHello writing code.
182
183TLS 1.3 specific coding rules:
184
185 - TLS 1.3 specific C modules, headers, static functions names are prefixed
186 with `ssl_tls1_3_`. The same applies to structures and types that are
187 internal to C modules.
188
189 - TLS 1.3 specific exported functions, macros, structures and types are
190 prefixed with `mbedtls_ssl_tls1_3_`.
191
192 - The names of macros and variables related to a field or structure in the
193 TLS 1.3 specification should contain as far as possible the field name as
194 it is in the specification. If the field name is `too long` and we prefer
195 to introduce some kind of abbreviation of it, use the same abbreviation
196 everywhere in the code.
197
198 Example 1: #define CLIENT_HELLO_RANDOM_LEN 32, macro for the length of the
199 `random` field of the ClientHello message.
200
201 Example 2 (consistent abbreviation): mbedtls_ssl_tls1_3_write_sig_alg_ext()
202 and MBEDTLS_TLS_EXT_SIG_ALG, `sig_alg` standing for
203 `signature_algorithms`.
204
205 - Regarding vectors that are represented by a length followed by their value
206 in the data exchanged between servers and clients:
207
208 - Use `<vector name>_len` for the name of a variable used to compute the
209 length in bytes of the vector, where <vector name> is the name of the
210 vector as defined in the TLS 1.3 specification.
211
212 - Use `<vector_name>_len_ptr` for the name of a variable intended to hold
213 the address of the first byte of the vector length.
214
215 - Use `<vector_name>_ptr` for the name of a variable intended to hold the
216 address of the first byte of the vector value.
217
218 - Use `<vector_name>_end_ptr` for the name of a variable intended to hold
219 the address of the first byte past the vector value.
220
221 Those two last idioms should lower the risk of mis-using one of the address
222 in place of the other one which could potentially lead to some nasty
223 issues.
224
225 Example: `cipher_suites` vector of ClientHello in
226 ssl_tls1_3_write_client_hello_cipher_suites()
227
228 size_t cipher_suites_len;
229 unsigned char *cipher_suites_len_ptr;
230 unsigned char *cipher_suites_ptr;
231
232 - Use of MBEDTLS_BYTE_xyz, MBEDTLS_PUT/GET_xyz, MBEDTLS_SSL_CHK_BUF_PTR
233 MBEDTLS_SSL_CHK_BUF_READ_PTR macros where applicable.
234
235 These macros were introduced after the prototype was written thus are
236 likely not to be used in prototype where we now would use them in
237 development.
238
239 The two first types, MBEDTLS_BYTE_xyz and MBEDTLS_PUT/GET_xyz, improve
240 the readability of the code and reduce the risk of writing or reading
241 bytes in the wrong order: we should probably have only MBEDTLS_GET/PUT_*_BE
242 (BE stands for Big-Endian) macros in the TLS 1.3 code.
243
244 The two last types, MBEDTLS_SSL_CHK_BUF_PTR and
245 MBEDTLS_SSL_CHK_BUF_READ_PTR, improve the readability of the code and
246 reduce the risk of error in the non-completely-trivial arithmetic to
247 check that we do not write or read past the end of a data buffer. The
248 usage of those macros combined with the following rule mitigate the risk
249 to read/write past the end of a data buffer.
250
251 Examples: hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
252 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
253 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
254
255 - To mitigate what happened here
256 (https://github.com/ARMmbed/mbedtls/pull/4882#discussion_r701704527) from
257 happening again, use always a local variable named `p` for the reading
258 pointer in functions parsing TLS 1.3 data, and for the writing pointer in
259 functions writing data into an output buffer. The name `p` has been
260 chosen as it was already widely used in TLS code.
261
262 - When an TLS 1.3 structure is written or read by a function or as part of
263 a function, provide as documentation the definition of the structure as
264 it is in the TLS 1.3 specification.
265
266General coding rules:
267
268 - We prefer grouping `related statement lines` by not adding blank lines
269 between them.
270
271 Example 1:
272
273 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len );
274 if( ret != 0 )
275 return( ret );
276 buf += output_len;
277
278 Example 2:
279
280 MBEDTLS_SSL_CHK_BUF_PTR( cipher_suites_iter, end, 2 );
281 MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 );
282 cipher_suites_iter += 2;
283
284 - Use macros for constants that are used in different functions, different
285 places in the code. When a constant is used only locally in a function
286 (like the length in bytes of the vector lengths in functions reading and
287 writing TLS handshake message) there is no need to define a macro for it.
288
289 Example: #define CLIENT_HELLO_RANDOM_LEN 32
290
291 - When declaring a pointer the dereferencing operator should be prepended to
292 the pointer name not appended to the pointer type:
293
294 Example: mbedtls_ssl_context *ssl;
295
296 - Maximum line length is 80 characters.
297
298 Exceptions:
299
300 - string literals can extend beyond 80 characters as we do not want to
301 split them to ease their search in the code base.
302
303 - A line can be more than 80 characters by a few characters if just looking
304 at the 80 first characters is enough to fully understand the line. For
305 example it is generally fine if some closure characters like ";" or ")"
306 are beyond the 80 characters limit.
307
308 - When in successive lines, functions and macros parameters should be aligned
309 vertically.
310
311 Example:
312 int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
313 unsigned hs_type,
314 unsigned char **buf,
315 size_t *buf_len );