blob: 6904c500db2e7f6ab9bac62e63dd456ce9cc334d [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 Crona9bdc8f2024-03-15 15:52:04 +01007Mbed TLS provides an implementation of the TLS 1.3 protocol. The TLS 1.3 support
8may be enabled using the MBEDTLS_SSL_PROTO_TLS1_3 configuration option.
Ronald Cron3785c902021-09-20 09:05:36 +02009
Ronald Cron2ba0d232022-07-01 11:25:49 +020010Support description
11-------------------
Ronald Cron3785c902021-09-20 09:05:36 +020012
Ronald Cronf164b6a2021-09-27 15:36:29 +020013- Overview
14
Ronald Cron2ba0d232022-07-01 11:25:49 +020015 - Mbed TLS implements both the client and the server side of the TLS 1.3
16 protocol.
Ronald Cronf164b6a2021-09-27 15:36:29 +020017
Ronald Cron2ba0d232022-07-01 11:25:49 +020018 - Mbed TLS supports ECDHE key establishment.
Ronald Cronf164b6a2021-09-27 15:36:29 +020019
Ronald Cron124ed8a2024-03-13 10:41:37 +010020 - Mbed TLS supports DHE key establishment.
Ronald Cronf164b6a2021-09-27 15:36:29 +020021
Ronald Cron93dcb1b2022-10-03 12:02:17 +020022 - Mbed TLS supports pre-shared keys for key establishment, pre-shared keys
23 provisioned externally as well as provisioned via the ticket mechanism.
24
25 - Mbed TLS supports session resumption via the ticket mechanism.
26
Ronald Cron1b606d82024-03-13 10:46:21 +010027 - Mbed TLS supports sending and receiving early data (0-RTT data).
Ronald Cronf164b6a2021-09-27 15:36:29 +020028
Ronald Cron3785c902021-09-20 09:05:36 +020029- Supported cipher suites: depends on the library configuration. Potentially
30 all of them:
31 TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256,
32 TLS_AES_128_CCM_SHA256 and TLS_AES_128_CCM_8_SHA256.
33
34- Supported ClientHello extensions:
35
Ronald Cron3cb707d2022-07-01 14:36:52 +020036 | Extension | Support |
37 | ---------------------------- | ------- |
38 | server_name | YES |
39 | max_fragment_length | no |
40 | status_request | no |
41 | supported_groups | YES |
42 | signature_algorithms | YES |
43 | use_srtp | no |
44 | heartbeat | no |
Ronald Cron124ed8a2024-03-13 10:41:37 +010045 | alpn | YES |
Ronald Cron3cb707d2022-07-01 14:36:52 +020046 | signed_certificate_timestamp | no |
47 | client_certificate_type | no |
48 | server_certificate_type | no |
49 | padding | no |
50 | key_share | YES |
Ronald Cron93dcb1b2022-10-03 12:02:17 +020051 | pre_shared_key | YES |
52 | psk_key_exchange_modes | YES |
Ronald Cron1b606d82024-03-13 10:46:21 +010053 | early_data | YES |
Ronald Cron3cb707d2022-07-01 14:36:52 +020054 | cookie | no |
55 | supported_versions | YES |
56 | certificate_authorities | no |
57 | post_handshake_auth | no |
58 | signature_algorithms_cert | no |
Ronald Cron3785c902021-09-20 09:05:36 +020059
Ronald Cron023987f2021-09-27 11:59:25 +020060
Ronald Cron3785c902021-09-20 09:05:36 +020061- Supported groups: depends on the library configuration.
Ronald Cron2ba0d232022-07-01 11:25:49 +020062 Potentially all ECDHE groups:
63 secp256r1, x25519, secp384r1, x448 and secp521r1.
Ronald Cronc3b510f2021-09-27 13:36:33 +020064
Ronald Cron124ed8a2024-03-13 10:41:37 +010065 Potentially all DHE groups:
66 ffdhe2048, ffdhe3072, ffdhe4096, ffdhe6144 and ffdhe8192.
Ronald Cronc3b510f2021-09-27 13:36:33 +020067
Ronald Cronfb877212021-09-28 15:49:39 +020068- Supported signature algorithms (both for certificates and CertificateVerify):
69 depends on the library configuration.
70 Potentially:
Ronald Cron2ba0d232022-07-01 11:25:49 +020071 ecdsa_secp256r1_sha256, ecdsa_secp384r1_sha384, ecdsa_secp521r1_sha512,
72 rsa_pkcs1_sha256, rsa_pkcs1_sha384, rsa_pkcs1_sha512, rsa_pss_rsae_sha256,
73 rsa_pss_rsae_sha384 and rsa_pss_rsae_sha512.
Ronald Cronc3b510f2021-09-27 13:36:33 +020074
Ronald Cronfb877212021-09-28 15:49:39 +020075 Note that in absence of an application profile standard specifying otherwise
Ronald Cron2ba0d232022-07-01 11:25:49 +020076 rsa_pkcs1_sha256, rsa_pss_rsae_sha256 and ecdsa_secp256r1_sha256 are
77 mandatory (see section 9.1 of the specification).
Ronald Cronc3b510f2021-09-27 13:36:33 +020078
Jerry Yu72a05652022-01-25 14:36:30 +080079- Supported versions:
80
Ronald Cron4d314962023-03-14 16:46:22 +010081 - TLS 1.2 and TLS 1.3 with version negotiation on client and server side.
Jerry Yu72a05652022-01-25 14:36:30 +080082
Ronald Cron2ba0d232022-07-01 11:25:49 +020083 - TLS 1.2 and TLS 1.3 can be enabled in the build independently of each
84 other.
Jerry Yu72a05652022-01-25 14:36:30 +080085
Ronald Cron3e7c4032021-09-27 14:22:38 +020086- Compatibility with existing SSL/TLS build options:
Ronald Cron3785c902021-09-20 09:05:36 +020087
Ronald Cron2ba0d232022-07-01 11:25:49 +020088 The TLS 1.3 implementation is compatible with nearly all TLS 1.2
89 configuration options in the sense that when enabling TLS 1.3 in the library
90 there is rarely any need to modify the configuration from that used for
91 TLS 1.2. There are two exceptions though: the TLS 1.3 implementation requires
92 MBEDTLS_PSA_CRYPTO_C and MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, so these options
93 must be enabled.
Tom Cosgroveafb2fe12022-06-29 16:36:12 +010094
Ronald Cron3cb707d2022-07-01 14:36:52 +020095 Most of the Mbed TLS SSL/TLS related options are not supported or not
96 applicable to the TLS 1.3 implementation:
Ronald Cron3785c902021-09-20 09:05:36 +020097
Ronald Cron023987f2021-09-27 11:59:25 +020098 | Mbed TLS configuration option | Support |
99 | ---------------------------------------- | ------- |
Ronald Cron124ed8a2024-03-13 10:41:37 +0100100 | MBEDTLS_SSL_ALL_ALERT_MESSAGES | yes |
Ronald Cron023987f2021-09-27 11:59:25 +0200101 | MBEDTLS_SSL_ASYNC_PRIVATE | no |
102 | MBEDTLS_SSL_CONTEXT_SERIALIZATION | no |
103 | MBEDTLS_SSL_DEBUG_ALL | no |
104 | MBEDTLS_SSL_ENCRYPT_THEN_MAC | n/a |
105 | MBEDTLS_SSL_EXTENDED_MASTER_SECRET | n/a |
Tom Cosgroveafb2fe12022-06-29 16:36:12 +0100106 | MBEDTLS_SSL_KEEP_PEER_CERTIFICATE | no (1) |
Ronald Cron023987f2021-09-27 11:59:25 +0200107 | MBEDTLS_SSL_RENEGOTIATION | n/a |
108 | MBEDTLS_SSL_MAX_FRAGMENT_LENGTH | no |
109 | | |
Ronald Cron93dcb1b2022-10-03 12:02:17 +0200110 | MBEDTLS_SSL_SESSION_TICKETS | yes |
Ronald Cron2ba0d232022-07-01 11:25:49 +0200111 | MBEDTLS_SSL_SERVER_NAME_INDICATION | yes |
Ronald Cron023987f2021-09-27 11:59:25 +0200112 | MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH | no |
113 | | |
114 | MBEDTLS_ECP_RESTARTABLE | no |
115 | MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED | no |
116 | | |
Ronald Cron3cb707d2022-07-01 14:36:52 +0200117 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED | n/a (2) |
Ronald Cron023987f2021-09-27 11:59:25 +0200118 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED | n/a |
119 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED | n/a |
Ronald Cron023987f2021-09-27 11:59:25 +0200120 | MBEDTLS_KEY_EXCHANGE_RSA_ENABLED | n/a |
121 | MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED | n/a |
122 | MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED | n/a |
123 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED | n/a |
124 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED | n/a |
125 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED | n/a |
126 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED | n/a |
127 | | |
Tom Cosgroved7adb3c2022-06-30 09:48:40 +0100128 | MBEDTLS_PSA_CRYPTO_C | no (1) |
Ronald Cron2ba0d232022-07-01 11:25:49 +0200129 | MBEDTLS_USE_PSA_CRYPTO | yes |
Ronald Cron3785c902021-09-20 09:05:36 +0200130
Tom Cosgroved7adb3c2022-06-30 09:48:40 +0100131 (1) These options must remain in their default state of enabled.
Ronald Crond8d2ea52022-10-04 15:48:06 +0200132 (2) See the TLS 1.3 specific build options section below.
133
134- TLS 1.3 specific build options:
135
136 - MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE enables the support for middlebox
137 compatibility mode as defined in section D.4 of RFC 8446.
138
Ronald Cron9810b6d2022-10-20 14:22:45 +0200139 - MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED enables the support for
140 the PSK key exchange mode as defined by RFC 8446. If it is the only key
141 exchange mode enabled, the TLS 1.3 implementation does not contain any code
142 related to key exchange protocols, certificates and signatures.
143
144 - MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED enables the
Ronald Cron10bf9562022-10-21 08:51:33 +0200145 support for the ephemeral key exchange mode. If it is the only key exchange
Ronald Crond8d2ea52022-10-04 15:48:06 +0200146 mode enabled, the TLS 1.3 implementation does not contain any code related
147 to PSK based key exchange. The ephemeral key exchange mode requires at least
148 one of the key exchange protocol allowed by the TLS 1.3 specification, the
149 parsing and validation of x509 certificates and at least one signature
150 algorithm allowed by the TLS 1.3 specification for signature computing and
151 verification.
152
Ronald Cron9810b6d2022-10-20 14:22:45 +0200153 - MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED enables the
154 support for the PSK ephemeral key exchange mode. If it is the only key
Ronald Crond8d2ea52022-10-04 15:48:06 +0200155 exchange mode enabled, the TLS 1.3 implementation does not contain any code
Ronald Crond8d2ea52022-10-04 15:48:06 +0200156 related to certificates and signatures. The PSK ephemeral key exchange
Ronald Cron9810b6d2022-10-20 14:22:45 +0200157 mode requires at least one of the key exchange protocol allowed by the
Ronald Crond8d2ea52022-10-04 15:48:06 +0200158 TLS 1.3 specification.
Ronald Cron3785c902021-09-20 09:05:36 +0200159
Ronald Cron653d5bc2021-12-09 14:35:56 +0100160
Ronald Cron3785c902021-09-20 09:05:36 +0200161Coding rules checklist for TLS 1.3
162----------------------------------
163
164The following coding rules are aimed to be a checklist for TLS 1.3 upstreaming
165work to reduce review rounds and the number of comments in each round. They
166come along (do NOT replace) the project coding rules
Dave Rodgmanb3196842022-10-12 16:47:08 +0100167(https://mbed-tls.readthedocs.io/en/latest/kb/development/mbedtls-coding-standards). They have been
Ronald Cron3785c902021-09-20 09:05:36 +0200168established and discussed following the review of #4882 that was the
169PR upstreaming the first part of TLS 1.3 ClientHello writing code.
170
171TLS 1.3 specific coding rules:
172
173 - TLS 1.3 specific C modules, headers, static functions names are prefixed
Ronald Cronb1944662021-09-27 13:56:46 +0200174 with `ssl_tls13_`. The same applies to structures and types that are
Ronald Cron3785c902021-09-20 09:05:36 +0200175 internal to C modules.
176
Ronald Cronb1944662021-09-27 13:56:46 +0200177 - TLS 1.3 specific exported functions, structures and types are
178 prefixed with `mbedtls_ssl_tls13_`.
179
180 - Use TLS1_3 in TLS 1.3 specific macros.
Ronald Cron3785c902021-09-20 09:05:36 +0200181
182 - The names of macros and variables related to a field or structure in the
183 TLS 1.3 specification should contain as far as possible the field name as
Ronald Cron72064b32021-09-27 13:54:28 +0200184 it is in the specification. If the field name is "too long" and we prefer
Ronald Cron3785c902021-09-20 09:05:36 +0200185 to introduce some kind of abbreviation of it, use the same abbreviation
186 everywhere in the code.
187
188 Example 1: #define CLIENT_HELLO_RANDOM_LEN 32, macro for the length of the
189 `random` field of the ClientHello message.
190
Dave Rodgmanc8aaac82021-10-18 12:56:53 +0100191 Example 2 (consistent abbreviation): `mbedtls_ssl_tls13_write_sig_alg_ext()`
Ronald Cron72064b32021-09-27 13:54:28 +0200192 and `MBEDTLS_TLS_EXT_SIG_ALG`, `sig_alg` standing for
Ronald Cron3785c902021-09-20 09:05:36 +0200193 `signature_algorithms`.
194
195 - Regarding vectors that are represented by a length followed by their value
196 in the data exchanged between servers and clients:
197
198 - Use `<vector name>_len` for the name of a variable used to compute the
199 length in bytes of the vector, where <vector name> is the name of the
200 vector as defined in the TLS 1.3 specification.
201
Ronald Cron99733f02021-09-27 13:58:21 +0200202 - Use `p_<vector_name>_len` for the name of a variable intended to hold
Ronald Cron3785c902021-09-20 09:05:36 +0200203 the address of the first byte of the vector length.
204
Ronald Cron99733f02021-09-27 13:58:21 +0200205 - Use `<vector_name>` for the name of a variable intended to hold the
Ronald Cron3785c902021-09-20 09:05:36 +0200206 address of the first byte of the vector value.
207
Ronald Cron99733f02021-09-27 13:58:21 +0200208 - Use `<vector_name>_end` for the name of a variable intended to hold
Ronald Cron3785c902021-09-20 09:05:36 +0200209 the address of the first byte past the vector value.
210
Ronald Cron99733f02021-09-27 13:58:21 +0200211 Those idioms should lower the risk of mis-using one of the address in place
212 of another one which could potentially lead to some nasty issues.
Ronald Cron3785c902021-09-20 09:05:36 +0200213
214 Example: `cipher_suites` vector of ClientHello in
Dave Rodgmanc8aaac82021-10-18 12:56:53 +0100215 `ssl_tls13_write_client_hello_cipher_suites()`
Ronald Cron72064b32021-09-27 13:54:28 +0200216 ```
217 size_t cipher_suites_len;
Ronald Cron99733f02021-09-27 13:58:21 +0200218 unsigned char *p_cipher_suites_len;
219 unsigned char *cipher_suites;
Ronald Cron72064b32021-09-27 13:54:28 +0200220 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200221
Ronald Cronfecda8d2021-09-27 13:59:38 +0200222 - Where applicable, use:
223 - the macros to extract a byte from a multi-byte integer MBEDTLS_BYTE_{0-8}.
224 - the macros to write in memory in big-endian order a multi-byte integer
225 MBEDTLS_PUT_UINT{8|16|32|64}_BE.
226 - the macros to read from memory a multi-byte integer in big-endian order
227 MBEDTLS_GET_UINT{8|16|32|64}_BE.
228 - the macro to check for space when writing into an output buffer
229 `MBEDTLS_SSL_CHK_BUF_PTR`.
230 - the macro to check for data when reading from an input buffer
231 `MBEDTLS_SSL_CHK_BUF_READ_PTR`.
Ronald Cron3785c902021-09-20 09:05:36 +0200232
Ronald Cronfecda8d2021-09-27 13:59:38 +0200233 The three first types, MBEDTLS_BYTE_{0-8}, MBEDTLS_PUT_UINT{8|16|32|64}_BE
234 and MBEDTLS_GET_UINT{8|16|32|64}_BE improve the readability of the code and
235 reduce the risk of writing or reading bytes in the wrong order.
Ronald Cron3785c902021-09-20 09:05:36 +0200236
Ronald Cron72064b32021-09-27 13:54:28 +0200237 The two last types, `MBEDTLS_SSL_CHK_BUF_PTR` and
238 `MBEDTLS_SSL_CHK_BUF_READ_PTR`, improve the readability of the code and
Ronald Cron3785c902021-09-20 09:05:36 +0200239 reduce the risk of error in the non-completely-trivial arithmetic to
240 check that we do not write or read past the end of a data buffer. The
241 usage of those macros combined with the following rule mitigate the risk
242 to read/write past the end of a data buffer.
243
Ronald Cron72064b32021-09-27 13:54:28 +0200244 Examples:
245 ```
246 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
247 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
248 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
249 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200250
251 - To mitigate what happened here
Dave Rodgman017a1992022-03-31 14:07:01 +0100252 (https://github.com/Mbed-TLS/mbedtls/pull/4882#discussion_r701704527) from
Ronald Cron3785c902021-09-20 09:05:36 +0200253 happening again, use always a local variable named `p` for the reading
254 pointer in functions parsing TLS 1.3 data, and for the writing pointer in
Ronald Cron3e7c4032021-09-27 14:22:38 +0200255 functions writing data into an output buffer and only that variable. The
256 name `p` has been chosen as it was already widely used in TLS code.
Ronald Cron3785c902021-09-20 09:05:36 +0200257
258 - When an TLS 1.3 structure is written or read by a function or as part of
259 a function, provide as documentation the definition of the structure as
260 it is in the TLS 1.3 specification.
261
262General coding rules:
263
Ronald Cron72064b32021-09-27 13:54:28 +0200264 - We prefer grouping "related statement lines" by not adding blank lines
Ronald Cron3785c902021-09-20 09:05:36 +0200265 between them.
266
267 Example 1:
Ronald Cron72064b32021-09-27 13:54:28 +0200268 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200269 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len );
270 if( ret != 0 )
271 return( ret );
272 buf += output_len;
Ronald Cron72064b32021-09-27 13:54:28 +0200273 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200274
275 Example 2:
Ronald Cron72064b32021-09-27 13:54:28 +0200276 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200277 MBEDTLS_SSL_CHK_BUF_PTR( cipher_suites_iter, end, 2 );
278 MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 );
279 cipher_suites_iter += 2;
Ronald Cron72064b32021-09-27 13:54:28 +0200280 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200281
282 - Use macros for constants that are used in different functions, different
283 places in the code. When a constant is used only locally in a function
284 (like the length in bytes of the vector lengths in functions reading and
285 writing TLS handshake message) there is no need to define a macro for it.
286
Ronald Cron72064b32021-09-27 13:54:28 +0200287 Example: `#define CLIENT_HELLO_RANDOM_LEN 32`
Ronald Cron3785c902021-09-20 09:05:36 +0200288
289 - When declaring a pointer the dereferencing operator should be prepended to
290 the pointer name not appended to the pointer type:
291
Ronald Cron72064b32021-09-27 13:54:28 +0200292 Example: `mbedtls_ssl_context *ssl;`
Ronald Cron3785c902021-09-20 09:05:36 +0200293
294 - Maximum line length is 80 characters.
295
296 Exceptions:
297
298 - string literals can extend beyond 80 characters as we do not want to
299 split them to ease their search in the code base.
300
301 - A line can be more than 80 characters by a few characters if just looking
302 at the 80 first characters is enough to fully understand the line. For
303 example it is generally fine if some closure characters like ";" or ")"
304 are beyond the 80 characters limit.
305
Ronald Cron847c3582021-09-27 14:24:43 +0200306 If a line becomes too long due to a refactoring (for example renaming a
307 function to a longer name, or indenting a block more), avoid rewrapping
308 lines in the same commit: it makes the review harder. Make one commit with
309 the longer lines and another commit with just the rewrapping.
310
Ronald Cron3785c902021-09-20 09:05:36 +0200311 - When in successive lines, functions and macros parameters should be aligned
312 vertically.
313
314 Example:
Ronald Cron72064b32021-09-27 13:54:28 +0200315 ```
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100316 int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl,
317 unsigned hs_type,
318 unsigned char **buf,
319 size_t *buf_len );
Ronald Cron72064b32021-09-27 13:54:28 +0200320 ```
Ronald Cron847c3582021-09-27 14:24:43 +0200321
322 - When a function's parameters span several lines, group related parameters
323 together if possible.
324
325 For example, prefer:
326
327 ```
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100328 mbedtls_ssl_start_handshake_msg( ssl, hs_type,
329 buf, buf_len );
Ronald Cron847c3582021-09-27 14:24:43 +0200330 ```
331 over
332 ```
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100333 mbedtls_ssl_start_handshake_msg( ssl, hs_type, buf,
334 buf_len );
Ronald Cron847c3582021-09-27 14:24:43 +0200335 ```
336 even if it fits.
Ronald Cron44b23b12022-05-31 16:05:13 +0200337
338
339Overview of handshake code organization
340---------------------------------------
341
342The TLS 1.3 handshake protocol is implemented as a state machine. The
Ronald Cron6b14c692022-06-24 13:45:04 +0200343functions `mbedtls_ssl_tls13_handshake_{client,server}_step` are the top level
Ronald Cron44b23b12022-05-31 16:05:13 +0200344functions of that implementation. They are implemented as a switch over all the
345possible states of the state machine.
346
347Most of the states are either dedicated to the processing or writing of an
348handshake message.
349
350The implementation does not go systematically through all states as this would
351result in too many checks of whether something needs to be done or not in a
352given state to be duplicated across several state handlers. For example, on
353client side, the states related to certificate parsing and validation are
354bypassed if the handshake is based on a pre-shared key and thus does not
355involve certificates.
356
357On the contrary, the implementation goes systematically though some states
358even if they could be bypassed if it helps in minimizing when and where inbound
359and outbound keys are updated. The `MBEDTLS_SSL_CLIENT_CERTIFICATE` state on
360client side is a example of that.
361
362The names of the handlers processing/writing an handshake message are
Ronald Cron6b14c692022-06-24 13:45:04 +0200363prefixed with `(mbedtls_)ssl_tls13_{process,write}`. To ease the maintenance and
Ronald Cron44b23b12022-05-31 16:05:13 +0200364reduce the risk of bugs, the code of the message processing and writing
365handlers is split into a sequence of stages.
366
367The sending of data to the peer only occurs in `mbedtls_ssl_handshake_step`
368between the calls to the handlers and as a consequence handlers do not have to
369care about the MBEDTLS_ERR_SSL_WANT_WRITE error code. Furthermore, all pending
370data are flushed before to call the next handler. That way, handlers do not
371have to worry about pending data when changing outbound keys.
372
373### Message processing handlers
374For message processing handlers, the stages are:
375
376* coordination stage: check if the state should be bypassed. This stage is
377optional. The check is either purely based on the reading of the value of some
378fields of the SSL context or based on the reading of the type of the next
379message. The latter occurs when it is not known what the next handshake message
380will be, an example of that on client side being if we are going to receive a
381CertificateRequest message or not. The intent is, apart from the next record
382reading to not modify the SSL context as this stage may be repeated if the
383next handshake message has not been received yet.
384
385* fetching stage: at this stage we are sure of the type of the handshake
386message we must receive next and we try to fetch it. If we did not go through
387a coordination stage involving the next record type reading, the next
388handshake message may not have been received yet, the handler returns with
389`MBEDTLS_ERR_SSL_WANT_READ` without changing the current state and it will be
390called again later.
391
392* pre-processing stage: prepare the SSL context for the message parsing. This
393stage is optional. Any processing that must be done before the parsing of the
394message or that can be done to simplify the parsing code. Some simple and
395partial parsing of the handshake message may append at that stage like in the
396ServerHello message pre-processing.
397
398* parsing stage: parse the message and restrict as much as possible any
399update of the SSL context. The idea of the pre-processing/parsing/post-processing
400organization is to concentrate solely on the parsing in the parsing function to
401reduce the size of its code and to simplify it.
402
403* post-processing stage: following the parsing, further update of the SSL
Ronald Cron139d0aa2022-06-14 18:45:44 +0200404context to prepare for the next incoming and outgoing messages. This stage is
Ronald Cron44b23b12022-05-31 16:05:13 +0200405optional. For example, secret and key computations occur at this stage, as well
406as handshake messages checksum update.
407
408* state change: the state change is done in the main state handler to ease the
409navigation of the state machine transitions.
410
411
412### Message writing handlers
413For message writing handlers, the stages are:
414
415* coordination stage: check if the state should be bypassed. This stage is
416optional. The check is based on the value of some fields of the SSL context.
417
418* preparation stage: prepare for the message writing. This stage is optional.
419Any processing that must be done before the writing of the message or that can
420be done to simplify the writing code.
421
422* writing stage: write the message and restrict as much as possible any update
423of the SSL context. The idea of the preparation/writing/finalization
424organization is to concentrate solely on the writing in the writing function to
425reduce the size of its code and simplify it.
426
427* finalization stage: following the writing, further update of the SSL
428context to prepare for the next incoming and outgoing messages. This stage is
429optional. For example, handshake secret and key computation occur at that
430stage (ServerHello writing finalization), switching to handshake keys for
431outbound message on server side as well.
432
433* state change: the state change is done in the main state handler to ease
434the navigation of the state machine transitions.