blob: 1a8c03ca0b3a94a5637b4138e3288ea15a323657 [file] [log] [blame] [view]
Ronald Cron43ffc9d2021-12-09 10:09:36 +01001TLS 1.3 support
2===============
Hanno Becker9338f9f2020-05-31 07:39:50 +01003
4Overview
5--------
6
Ronald Cron43ffc9d2021-12-09 10:09:36 +01007Mbed TLS provides a minimum viable implementation of the TLS 1.3 protocol
8defined in the "MVP definition" section below. The TLS 1.3 support enablement
9is controlled by the MBEDTLS_SSL_PROTO_TLS1_3 configuration option.
Hanno Becker9338f9f2020-05-31 07:39:50 +010010
Ronald Cron43ffc9d2021-12-09 10:09:36 +010011The development of the TLS 1.3 protocol is based on the TLS 1.3 prototype
12located at https://github.com/hannestschofenig/mbedtls. The prototype is
13itself based on a version of the development branch that we aim to keep as
14recent as possible (ideally the head) by merging regularly commits of the
Ronald Cron7aa6fc12021-12-09 14:53:59 +010015development branch into the prototype. The section "Prototype upstreaming
16status" below describes what remains to be upstreamed.
Hanno Becker9338f9f2020-05-31 07:39:50 +010017
Ronald Cron3785c902021-09-20 09:05:36 +020018
19MVP definition
20--------------
21
Ronald Cronf164b6a2021-09-27 15:36:29 +020022- Overview
23
24 - The TLS 1.3 MVP implements only the client side of the protocol.
25
26 - The TLS 1.3 MVP supports ECDHE key establishment.
27
28 - The TLS 1.3 MVP does not support DHE key establishment.
29
30 - The TLS 1.3 MVP does not support pre-shared keys, including any form of
31 session resumption. This implies that it does not support sending early
32 data (0-RTT data).
33
34 - The TLS 1.3 MVP supports the authentication of the server by the client
35 but does not support authentication of the client by the server. In terms
36 of TLS 1.3 authentication messages, this means that the TLS 1.3 MVP
37 supports the processing of the Certificate and CertificateVerify messages
38 but not of the CertificateRequest message.
39
40 - The TLS 1.3 MVP does not support the handling of server HelloRetryRequest
41 message. In practice, this means that the handshake will fail if the MVP
42 does not provide in its ClientHello the shared secret associated to the
43 group selected by the server for key establishement. For more information,
44 see the comment associated to the `key_share` extension below.
45
46 - If the TLS 1.3 MVP receives a HelloRetryRequest or a CertificateRequest
47 message, it aborts the handshake with an handshake_failure closure alert
48 and the `mbedtls_ssl_handshake()` returns in error with the
49 `MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE` error code.
Ronald Cron3785c902021-09-20 09:05:36 +020050
51- Supported cipher suites: depends on the library configuration. Potentially
52 all of them:
53 TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256,
54 TLS_AES_128_CCM_SHA256 and TLS_AES_128_CCM_8_SHA256.
55
56- Supported ClientHello extensions:
57
Ronald Cron023987f2021-09-27 11:59:25 +020058 | Extension | MVP | Prototype (1) |
59 | ---------------------------- | ------- | ------------- |
Ronald Cron85e51082021-09-27 12:13:16 +020060 | server_name | YES | YES |
Ronald Cron023987f2021-09-27 11:59:25 +020061 | max_fragment_length | no | YES |
62 | status_request | no | no |
63 | supported_groups | YES | YES |
64 | signature_algorithms | YES | YES |
65 | use_srtp | no | no |
66 | heartbeat | no | no |
67 | apln | no | YES |
68 | signed_certificate_timestamp | no | no |
69 | client_certificate_type | no | no |
70 | server_certificate_type | no | no |
71 | padding | no | no |
Ronald Cron3160d702021-09-27 13:27:21 +020072 | key_share | YES (2) | YES |
Ronald Cron023987f2021-09-27 11:59:25 +020073 | pre_shared_key | no | YES |
74 | psk_key_exchange_modes | no | YES |
75 | early_data | no | YES |
76 | cookie | no | YES |
Ronald Cron3160d702021-09-27 13:27:21 +020077 | supported_versions | YES (3) | YES |
Ronald Cron023987f2021-09-27 11:59:25 +020078 | certificate_authorities | no | no |
79 | post_handshake_auth | no | no |
80 | signature_algorithms_cert | no | no |
Ronald Cron3785c902021-09-20 09:05:36 +020081
Ronald Cron023987f2021-09-27 11:59:25 +020082 (1) This is just for comparison.
83
Paul Elliott66491c72021-11-26 20:24:43 +000084 (2) The MVP sends only one shared secret corresponding to the configured
Paul Elliottc0d335b2021-12-02 16:38:05 +000085 preferred group. This could end up with connection failure if the
86 server does not support our preferred curve, as the MVP does not implement
Paul Elliott89c8e092021-11-30 10:54:52 +000087 HelloRetryRequest. The preferred group is the group of the first curve in
Paul Elliottc0d335b2021-12-02 16:38:05 +000088 the list of allowed curves as defined by the configuration. The allowed
89 curves are by default ordered as follows: `x25519`, `secp256r1`,
90 `secp384r1` and finally `secp521r1`. Note that, in the absence of an
91 application profile standard specifying otherwise, section 9.1 of the
92 specification rather promotes curve `secp256r1` to be supported over
Paul Elliottcce0f5a2021-12-03 16:13:30 +000093 curve `x25519`. The MVP would, however, rather keep the preference order
Paul Elliottc0d335b2021-12-02 16:38:05 +000094 currently promoted by Mbed TLS as this applies to TLS 1.2 as well, and
95 changing the order only for TLS1.3 would be potentially difficult.
96 In the unlikely event a server does not support curve `x25519` but does
97 support curve `secp256r1`, curve `secp256r1` can be set as the preferred
98 curve through the `mbedtls_ssl_conf_curves()` API.
Ronald Cron3160d702021-09-27 13:27:21 +020099
Paul Elliott66491c72021-11-26 20:24:43 +0000100 (3) The MVP proposes only TLS 1.3 and does not support version negotiation.
Ronald Cron3160d702021-09-27 13:27:21 +0200101 Out-of-protocol fallback is supported though if the Mbed TLS library
102 has been built to support both TLS 1.3 and TLS 1.2: just set the
103 maximum of the minor version of the SSL configuration to
104 MBEDTLS_SSL_MINOR_VERSION_3 (`mbedtls_ssl_conf_min_version()` API) and
105 re-initiate a server handshake.
Ronald Cron3785c902021-09-20 09:05:36 +0200106
107- Supported groups: depends on the library configuration.
Ronald Cron8ee9ed62021-09-28 14:46:43 +0200108 Potentially all ECDHE groups but x448:
109 secp256r1, x25519, secp384r1 and secp521r1.
Ronald Cronc3b510f2021-09-27 13:36:33 +0200110
111 Finite field groups (DHE) are not supported.
112
Ronald Cronfb877212021-09-28 15:49:39 +0200113- Supported signature algorithms (both for certificates and CertificateVerify):
114 depends on the library configuration.
115 Potentially:
116 rsa_pkcs1_sha256, rsa_pss_rsae_sha256, ecdsa_secp256r1_sha256,
117 ecdsa_secp384r1_sha384 and ecdsa_secp521r1_sha512.
Ronald Cronc3b510f2021-09-27 13:36:33 +0200118
Ronald Cronfb877212021-09-28 15:49:39 +0200119 Note that in absence of an application profile standard specifying otherwise
120 the three first ones in the list above are mandatory (see section 9.1 of the
121 specification).
Ronald Cronc3b510f2021-09-27 13:36:33 +0200122
Jerry Yu72a05652022-01-25 14:36:30 +0800123- Supported versions:
124
Jerry Yuadb18692022-01-27 12:55:32 +0800125 - TLS 1.2 and TLS 1.3 but version negotiation is not supported.
Jerry Yu72a05652022-01-25 14:36:30 +0800126
Jerry Yubd192872022-02-08 10:20:53 +0800127 - TLS 1.3 cannot be enabled in the build (MBEDTLS_SSL_PROTO_TLS1_3
128 configuration option) without TLS 1.2 (MBEDTLS_SSL_PROTO_TLS1_2 configuration
129 option).
Jerry Yu72a05652022-01-25 14:36:30 +0800130
Jerry Yuadb18692022-01-27 12:55:32 +0800131 - TLS 1.2 can be enabled in the build independently of TLS 1.3.
Jerry Yu72a05652022-01-25 14:36:30 +0800132
Jerry Yuadb18692022-01-27 12:55:32 +0800133 - If both TLS 1.3 and TLS 1.2 are enabled at build time, only one of them can
134 be configured at runtime via `mbedtls_ssl_conf_{min,max}_version`. Otherwise,
135 `mbedtls_ssl_setup` will raise `MBEDTLS_ERR_SSL_BAD_CONFIG` error.
Ronald Cron3785c902021-09-20 09:05:36 +0200136
Ronald Cron3e7c4032021-09-27 14:22:38 +0200137- Compatibility with existing SSL/TLS build options:
Ronald Cron3785c902021-09-20 09:05:36 +0200138
Tom Cosgroveafb2fe12022-06-29 16:36:12 +0100139 The TLS 1.3 MVP is compatible with nearly all TLS 1.2 configuration options
140 in the sense that when enabling the TLS 1.3 MVP in the library there is rarely
141 any need to modify the configuration from that used for TLS 1.2.
142
143 The two exceptions to this are:
144
145 - The TLS 1.3 MVP is not compatible with MBEDTLS_USE_PSA_CRYPTO, so this option
146 must be disabled.
147 - The TLS 1.3 MVP requires MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, so this option
148 must be enabled.
Ronald Cron4279bac2022-02-11 15:08:48 +0100149
150 Mbed TLS SSL/TLS related features are not supported or not applicable to the
151 TLS 1.3 MVP:
Ronald Cron3785c902021-09-20 09:05:36 +0200152
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 |
Tom Cosgroveafb2fe12022-06-29 16:36:12 +0100161 | MBEDTLS_SSL_KEEP_PEER_CERTIFICATE | no (1) |
Ronald Cron023987f2021-09-27 11:59:25 +0200162 | MBEDTLS_SSL_RENEGOTIATION | n/a |
163 | MBEDTLS_SSL_MAX_FRAGMENT_LENGTH | no |
164 | | |
165 | MBEDTLS_SSL_SESSION_TICKETS | no |
Ronald Cron023987f2021-09-27 11:59:25 +0200166 | MBEDTLS_SSL_SERVER_NAME_INDICATION | no |
167 | MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH | no |
168 | | |
169 | MBEDTLS_ECP_RESTARTABLE | no |
170 | MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED | no |
171 | | |
172 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED | n/a (2) |
173 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED | n/a |
174 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED | n/a |
175 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED | n/a |
176 | MBEDTLS_KEY_EXCHANGE_RSA_ENABLED | n/a |
177 | MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED | n/a |
178 | MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED | n/a |
179 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED | n/a |
180 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED | n/a |
181 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED | n/a |
182 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED | n/a |
183 | | |
Ronald Cron87829e52022-02-14 16:31:33 +0100184 | MBEDTLS_USE_PSA_CRYPTO | no |
Ronald Cron3785c902021-09-20 09:05:36 +0200185
Tom Cosgroveafb2fe12022-06-29 16:36:12 +0100186 (1) This option must remain in its default state of enabled.
Ronald Cron1fa50882021-09-27 12:06:52 +0200187 (2) Key exchange configuration options for TLS 1.3 will likely to be
188 organized around the notion of key exchange mode along the line
Xiaofei Bai746f9482021-11-12 08:53:56 +0000189 of the MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE/PSK/PSK_EPHEMERAL/EPHEMERAL
Ronald Cron1fa50882021-09-27 12:06:52 +0200190 runtime configuration macros.
Ronald Cron3785c902021-09-20 09:05:36 +0200191
Ronald Cron660c7232021-09-27 13:40:53 +0200192- Quality considerations
193 - Standard Mbed TLS review bar
194 - Interoperability testing with OpenSSL and GnuTLS. Test with all the
Ronald Cron7fc96c12021-09-28 15:54:57 +0200195 cipher suites and signature algorithms supported by OpenSSL/GnuTLS server.
Ronald Cron660c7232021-09-27 13:40:53 +0200196 - Negative testing against OpenSSL/GnuTLS servers with which the
Ronald Cron7fc96c12021-09-28 15:54:57 +0200197 handshake fails due to incompatibility with the capabilities of the
Ronald Cron660c7232021-09-27 13:40:53 +0200198 MVP: TLS 1.2 or 1.1 server, server sending an HelloRetryRequest message in
199 response to the MVP ClientHello, server sending a CertificateRequest
200 message ...
201
Ronald Cron653d5bc2021-12-09 14:35:56 +0100202
Ronald Cron7aa6fc12021-12-09 14:53:59 +0100203Prototype upstreaming status
204----------------------------
Ronald Cron653d5bc2021-12-09 14:35:56 +0100205
Ronald Cronb1822ef2021-12-10 14:28:13 +0100206The following summarizes which parts of the TLS 1.3 prototype remain to be
207upstreamed:
Ronald Cron653d5bc2021-12-09 14:35:56 +0100208
Ronald Cron7aa6fc12021-12-09 14:53:59 +0100209- Ephemeral only handshake on client side: client authentication,
210 HelloRetryRequest support, version negotiation.
Ronald Cron653d5bc2021-12-09 14:35:56 +0100211
Ronald Cron7aa6fc12021-12-09 14:53:59 +0100212- Ephemeral only handshake server side.
Ronald Cron653d5bc2021-12-09 14:35:56 +0100213
Ronald Cron7aa6fc12021-12-09 14:53:59 +0100214- Pre-shared keys, session resumption and 0-RTT data (both client and server
215 side).
Ronald Cron653d5bc2021-12-09 14:35:56 +0100216
217- New TLS Message Processing Stack (MPS)
218
219 The TLS 1.3 prototype is developed alongside a rewrite of the TLS messaging layer,
220 encompassing low-level details such as record parsing, handshake reassembly, and
221 DTLS retransmission state machine.
222
223 MPS has the following components:
224 - Layer 1 (Datagram handling)
225 - Layer 2 (Record handling)
226 - Layer 3 (Message handling)
227 - Layer 4 (Retransmission State Machine)
228 - Reader (Abstracted pointer arithmetic and reassembly logic for incoming data)
229 - Writer (Abstracted pointer arithmetic and fragmentation logic for outgoing data)
230
231 Of those components, the following have been upstreamed
232 as part of `MBEDTLS_SSL_PROTO_TLS1_3`:
233
234 - Reader ([`library/mps_reader.h`](../../library/mps_reader.h))
235
236
Ronald Cron3785c902021-09-20 09:05:36 +0200237Coding rules checklist for TLS 1.3
238----------------------------------
239
240The following coding rules are aimed to be a checklist for TLS 1.3 upstreaming
241work to reduce review rounds and the number of comments in each round. They
242come along (do NOT replace) the project coding rules
243(https://tls.mbed.org/kb/development/mbedtls-coding-standards). They have been
244established and discussed following the review of #4882 that was the
245PR upstreaming the first part of TLS 1.3 ClientHello writing code.
246
247TLS 1.3 specific coding rules:
248
249 - TLS 1.3 specific C modules, headers, static functions names are prefixed
Ronald Cronb1944662021-09-27 13:56:46 +0200250 with `ssl_tls13_`. The same applies to structures and types that are
Ronald Cron3785c902021-09-20 09:05:36 +0200251 internal to C modules.
252
Ronald Cronb1944662021-09-27 13:56:46 +0200253 - TLS 1.3 specific exported functions, structures and types are
254 prefixed with `mbedtls_ssl_tls13_`.
255
256 - Use TLS1_3 in TLS 1.3 specific macros.
Ronald Cron3785c902021-09-20 09:05:36 +0200257
258 - The names of macros and variables related to a field or structure in the
259 TLS 1.3 specification should contain as far as possible the field name as
Ronald Cron72064b32021-09-27 13:54:28 +0200260 it is in the specification. If the field name is "too long" and we prefer
Ronald Cron3785c902021-09-20 09:05:36 +0200261 to introduce some kind of abbreviation of it, use the same abbreviation
262 everywhere in the code.
263
264 Example 1: #define CLIENT_HELLO_RANDOM_LEN 32, macro for the length of the
265 `random` field of the ClientHello message.
266
Dave Rodgmanc8aaac82021-10-18 12:56:53 +0100267 Example 2 (consistent abbreviation): `mbedtls_ssl_tls13_write_sig_alg_ext()`
Ronald Cron72064b32021-09-27 13:54:28 +0200268 and `MBEDTLS_TLS_EXT_SIG_ALG`, `sig_alg` standing for
Ronald Cron3785c902021-09-20 09:05:36 +0200269 `signature_algorithms`.
270
271 - Regarding vectors that are represented by a length followed by their value
272 in the data exchanged between servers and clients:
273
274 - Use `<vector name>_len` for the name of a variable used to compute the
275 length in bytes of the vector, where <vector name> is the name of the
276 vector as defined in the TLS 1.3 specification.
277
Ronald Cron99733f02021-09-27 13:58:21 +0200278 - Use `p_<vector_name>_len` for the name of a variable intended to hold
Ronald Cron3785c902021-09-20 09:05:36 +0200279 the address of the first byte of the vector length.
280
Ronald Cron99733f02021-09-27 13:58:21 +0200281 - Use `<vector_name>` for the name of a variable intended to hold the
Ronald Cron3785c902021-09-20 09:05:36 +0200282 address of the first byte of the vector value.
283
Ronald Cron99733f02021-09-27 13:58:21 +0200284 - Use `<vector_name>_end` for the name of a variable intended to hold
Ronald Cron3785c902021-09-20 09:05:36 +0200285 the address of the first byte past the vector value.
286
Ronald Cron99733f02021-09-27 13:58:21 +0200287 Those idioms should lower the risk of mis-using one of the address in place
288 of another one which could potentially lead to some nasty issues.
Ronald Cron3785c902021-09-20 09:05:36 +0200289
290 Example: `cipher_suites` vector of ClientHello in
Dave Rodgmanc8aaac82021-10-18 12:56:53 +0100291 `ssl_tls13_write_client_hello_cipher_suites()`
Ronald Cron72064b32021-09-27 13:54:28 +0200292 ```
293 size_t cipher_suites_len;
Ronald Cron99733f02021-09-27 13:58:21 +0200294 unsigned char *p_cipher_suites_len;
295 unsigned char *cipher_suites;
Ronald Cron72064b32021-09-27 13:54:28 +0200296 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200297
Ronald Cronfecda8d2021-09-27 13:59:38 +0200298 - Where applicable, use:
299 - the macros to extract a byte from a multi-byte integer MBEDTLS_BYTE_{0-8}.
300 - the macros to write in memory in big-endian order a multi-byte integer
301 MBEDTLS_PUT_UINT{8|16|32|64}_BE.
302 - the macros to read from memory a multi-byte integer in big-endian order
303 MBEDTLS_GET_UINT{8|16|32|64}_BE.
304 - the macro to check for space when writing into an output buffer
305 `MBEDTLS_SSL_CHK_BUF_PTR`.
306 - the macro to check for data when reading from an input buffer
307 `MBEDTLS_SSL_CHK_BUF_READ_PTR`.
Ronald Cron3785c902021-09-20 09:05:36 +0200308
309 These macros were introduced after the prototype was written thus are
310 likely not to be used in prototype where we now would use them in
311 development.
312
Ronald Cronfecda8d2021-09-27 13:59:38 +0200313 The three first types, MBEDTLS_BYTE_{0-8}, MBEDTLS_PUT_UINT{8|16|32|64}_BE
314 and MBEDTLS_GET_UINT{8|16|32|64}_BE improve the readability of the code and
315 reduce the risk of writing or reading bytes in the wrong order.
Ronald Cron3785c902021-09-20 09:05:36 +0200316
Ronald Cron72064b32021-09-27 13:54:28 +0200317 The two last types, `MBEDTLS_SSL_CHK_BUF_PTR` and
318 `MBEDTLS_SSL_CHK_BUF_READ_PTR`, improve the readability of the code and
Ronald Cron3785c902021-09-20 09:05:36 +0200319 reduce the risk of error in the non-completely-trivial arithmetic to
320 check that we do not write or read past the end of a data buffer. The
321 usage of those macros combined with the following rule mitigate the risk
322 to read/write past the end of a data buffer.
323
Ronald Cron72064b32021-09-27 13:54:28 +0200324 Examples:
325 ```
326 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
327 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
328 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
329 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200330
331 - To mitigate what happened here
Dave Rodgman017a1992022-03-31 14:07:01 +0100332 (https://github.com/Mbed-TLS/mbedtls/pull/4882#discussion_r701704527) from
Ronald Cron3785c902021-09-20 09:05:36 +0200333 happening again, use always a local variable named `p` for the reading
334 pointer in functions parsing TLS 1.3 data, and for the writing pointer in
Ronald Cron3e7c4032021-09-27 14:22:38 +0200335 functions writing data into an output buffer and only that variable. The
336 name `p` has been chosen as it was already widely used in TLS code.
Ronald Cron3785c902021-09-20 09:05:36 +0200337
338 - When an TLS 1.3 structure is written or read by a function or as part of
339 a function, provide as documentation the definition of the structure as
340 it is in the TLS 1.3 specification.
341
342General coding rules:
343
Ronald Cron72064b32021-09-27 13:54:28 +0200344 - We prefer grouping "related statement lines" by not adding blank lines
Ronald Cron3785c902021-09-20 09:05:36 +0200345 between them.
346
347 Example 1:
Ronald Cron72064b32021-09-27 13:54:28 +0200348 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200349 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len );
350 if( ret != 0 )
351 return( ret );
352 buf += output_len;
Ronald Cron72064b32021-09-27 13:54:28 +0200353 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200354
355 Example 2:
Ronald Cron72064b32021-09-27 13:54:28 +0200356 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200357 MBEDTLS_SSL_CHK_BUF_PTR( cipher_suites_iter, end, 2 );
358 MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 );
359 cipher_suites_iter += 2;
Ronald Cron72064b32021-09-27 13:54:28 +0200360 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200361
362 - Use macros for constants that are used in different functions, different
363 places in the code. When a constant is used only locally in a function
364 (like the length in bytes of the vector lengths in functions reading and
365 writing TLS handshake message) there is no need to define a macro for it.
366
Ronald Cron72064b32021-09-27 13:54:28 +0200367 Example: `#define CLIENT_HELLO_RANDOM_LEN 32`
Ronald Cron3785c902021-09-20 09:05:36 +0200368
369 - When declaring a pointer the dereferencing operator should be prepended to
370 the pointer name not appended to the pointer type:
371
Ronald Cron72064b32021-09-27 13:54:28 +0200372 Example: `mbedtls_ssl_context *ssl;`
Ronald Cron3785c902021-09-20 09:05:36 +0200373
374 - Maximum line length is 80 characters.
375
376 Exceptions:
377
378 - string literals can extend beyond 80 characters as we do not want to
379 split them to ease their search in the code base.
380
381 - A line can be more than 80 characters by a few characters if just looking
382 at the 80 first characters is enough to fully understand the line. For
383 example it is generally fine if some closure characters like ";" or ")"
384 are beyond the 80 characters limit.
385
Ronald Cron847c3582021-09-27 14:24:43 +0200386 If a line becomes too long due to a refactoring (for example renaming a
387 function to a longer name, or indenting a block more), avoid rewrapping
388 lines in the same commit: it makes the review harder. Make one commit with
389 the longer lines and another commit with just the rewrapping.
390
Ronald Cron3785c902021-09-20 09:05:36 +0200391 - When in successive lines, functions and macros parameters should be aligned
392 vertically.
393
394 Example:
Ronald Cron72064b32021-09-27 13:54:28 +0200395 ```
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100396 int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl,
397 unsigned hs_type,
398 unsigned char **buf,
399 size_t *buf_len );
Ronald Cron72064b32021-09-27 13:54:28 +0200400 ```
Ronald Cron847c3582021-09-27 14:24:43 +0200401
402 - When a function's parameters span several lines, group related parameters
403 together if possible.
404
405 For example, prefer:
406
407 ```
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100408 mbedtls_ssl_start_handshake_msg( ssl, hs_type,
409 buf, buf_len );
Ronald Cron847c3582021-09-27 14:24:43 +0200410 ```
411 over
412 ```
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100413 mbedtls_ssl_start_handshake_msg( ssl, hs_type, buf,
414 buf_len );
Ronald Cron847c3582021-09-27 14:24:43 +0200415 ```
416 even if it fits.
Ronald Cron44b23b12022-05-31 16:05:13 +0200417
418
419Overview of handshake code organization
420---------------------------------------
421
422The TLS 1.3 handshake protocol is implemented as a state machine. The
Ronald Cron6b14c692022-06-24 13:45:04 +0200423functions `mbedtls_ssl_tls13_handshake_{client,server}_step` are the top level
Ronald Cron44b23b12022-05-31 16:05:13 +0200424functions of that implementation. They are implemented as a switch over all the
425possible states of the state machine.
426
427Most of the states are either dedicated to the processing or writing of an
428handshake message.
429
430The implementation does not go systematically through all states as this would
431result in too many checks of whether something needs to be done or not in a
432given state to be duplicated across several state handlers. For example, on
433client side, the states related to certificate parsing and validation are
434bypassed if the handshake is based on a pre-shared key and thus does not
435involve certificates.
436
437On the contrary, the implementation goes systematically though some states
438even if they could be bypassed if it helps in minimizing when and where inbound
439and outbound keys are updated. The `MBEDTLS_SSL_CLIENT_CERTIFICATE` state on
440client side is a example of that.
441
442The names of the handlers processing/writing an handshake message are
Ronald Cron6b14c692022-06-24 13:45:04 +0200443prefixed with `(mbedtls_)ssl_tls13_{process,write}`. To ease the maintenance and
Ronald Cron44b23b12022-05-31 16:05:13 +0200444reduce the risk of bugs, the code of the message processing and writing
445handlers is split into a sequence of stages.
446
447The sending of data to the peer only occurs in `mbedtls_ssl_handshake_step`
448between the calls to the handlers and as a consequence handlers do not have to
449care about the MBEDTLS_ERR_SSL_WANT_WRITE error code. Furthermore, all pending
450data are flushed before to call the next handler. That way, handlers do not
451have to worry about pending data when changing outbound keys.
452
453### Message processing handlers
454For message processing handlers, the stages are:
455
456* coordination stage: check if the state should be bypassed. This stage is
457optional. The check is either purely based on the reading of the value of some
458fields of the SSL context or based on the reading of the type of the next
459message. The latter occurs when it is not known what the next handshake message
460will be, an example of that on client side being if we are going to receive a
461CertificateRequest message or not. The intent is, apart from the next record
462reading to not modify the SSL context as this stage may be repeated if the
463next handshake message has not been received yet.
464
465* fetching stage: at this stage we are sure of the type of the handshake
466message we must receive next and we try to fetch it. If we did not go through
467a coordination stage involving the next record type reading, the next
468handshake message may not have been received yet, the handler returns with
469`MBEDTLS_ERR_SSL_WANT_READ` without changing the current state and it will be
470called again later.
471
472* pre-processing stage: prepare the SSL context for the message parsing. This
473stage is optional. Any processing that must be done before the parsing of the
474message or that can be done to simplify the parsing code. Some simple and
475partial parsing of the handshake message may append at that stage like in the
476ServerHello message pre-processing.
477
478* parsing stage: parse the message and restrict as much as possible any
479update of the SSL context. The idea of the pre-processing/parsing/post-processing
480organization is to concentrate solely on the parsing in the parsing function to
481reduce the size of its code and to simplify it.
482
483* post-processing stage: following the parsing, further update of the SSL
Ronald Cron139d0aa2022-06-14 18:45:44 +0200484context to prepare for the next incoming and outgoing messages. This stage is
Ronald Cron44b23b12022-05-31 16:05:13 +0200485optional. For example, secret and key computations occur at this stage, as well
486as handshake messages checksum update.
487
488* state change: the state change is done in the main state handler to ease the
489navigation of the state machine transitions.
490
491
492### Message writing handlers
493For message writing handlers, the stages are:
494
495* coordination stage: check if the state should be bypassed. This stage is
496optional. The check is based on the value of some fields of the SSL context.
497
498* preparation stage: prepare for the message writing. This stage is optional.
499Any processing that must be done before the writing of the message or that can
500be done to simplify the writing code.
501
502* writing stage: write the message and restrict as much as possible any update
503of the SSL context. The idea of the preparation/writing/finalization
504organization is to concentrate solely on the writing in the writing function to
505reduce the size of its code and simplify it.
506
507* finalization stage: following the writing, further update of the SSL
508context to prepare for the next incoming and outgoing messages. This stage is
509optional. For example, handshake secret and key computation occur at that
510stage (ServerHello writing finalization), switching to handshake keys for
511outbound message on server side as well.
512
513* state change: the state change is done in the main state handler to ease
514the navigation of the state machine transitions.