blob: 2a1faed00bcc9b68b526ea0f9ebece291497cea8 [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 Cron2ba0d232022-07-01 11:25:49 +02007Mbed TLS provides a partial implementation of the TLS 1.3 protocol defined in
8the "Support description" section below. The TLS 1.3 support enablement
Ronald Cron43ffc9d2021-12-09 10:09:36 +01009is 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
Ronald Cron2ba0d232022-07-01 11:25:49 +020019Support description
20-------------------
Ronald Cron3785c902021-09-20 09:05:36 +020021
Ronald Cronf164b6a2021-09-27 15:36:29 +020022- Overview
23
Ronald Cron2ba0d232022-07-01 11:25:49 +020024 - Mbed TLS implements both the client and the server side of the TLS 1.3
25 protocol.
Ronald Cronf164b6a2021-09-27 15:36:29 +020026
Ronald Cron2ba0d232022-07-01 11:25:49 +020027 - Mbed TLS supports ECDHE key establishment.
Ronald Cronf164b6a2021-09-27 15:36:29 +020028
Ronald Cron2ba0d232022-07-01 11:25:49 +020029 - Mbed TLS does not support DHE key establishment.
Ronald Cronf164b6a2021-09-27 15:36:29 +020030
Ronald Cron2ba0d232022-07-01 11:25:49 +020031 - Mbed TLS does not support pre-shared keys, including any form of
Ronald Cronf164b6a2021-09-27 15:36:29 +020032 session resumption. This implies that it does not support sending early
33 data (0-RTT data).
34
Ronald Cron3785c902021-09-20 09:05:36 +020035- Supported cipher suites: depends on the library configuration. Potentially
36 all of them:
37 TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256,
38 TLS_AES_128_CCM_SHA256 and TLS_AES_128_CCM_8_SHA256.
39
40- Supported ClientHello extensions:
41
Ronald Cron3cb707d2022-07-01 14:36:52 +020042 | Extension | Support |
43 | ---------------------------- | ------- |
44 | server_name | YES |
45 | max_fragment_length | no |
46 | status_request | no |
47 | supported_groups | YES |
48 | signature_algorithms | YES |
49 | use_srtp | no |
50 | heartbeat | no |
51 | apln | YES |
52 | signed_certificate_timestamp | no |
53 | client_certificate_type | no |
54 | server_certificate_type | no |
55 | padding | no |
56 | key_share | YES |
57 | pre_shared_key | no |
58 | psk_key_exchange_modes | no |
59 | early_data | no |
60 | cookie | no |
61 | supported_versions | YES |
62 | certificate_authorities | no |
63 | post_handshake_auth | no |
64 | signature_algorithms_cert | no |
Ronald Cron3785c902021-09-20 09:05:36 +020065
Ronald Cron023987f2021-09-27 11:59:25 +020066
Ronald Cron3785c902021-09-20 09:05:36 +020067- Supported groups: depends on the library configuration.
Ronald Cron2ba0d232022-07-01 11:25:49 +020068 Potentially all ECDHE groups:
69 secp256r1, x25519, secp384r1, x448 and secp521r1.
Ronald Cronc3b510f2021-09-27 13:36:33 +020070
71 Finite field groups (DHE) are not supported.
72
Ronald Cronfb877212021-09-28 15:49:39 +020073- Supported signature algorithms (both for certificates and CertificateVerify):
74 depends on the library configuration.
75 Potentially:
Ronald Cron2ba0d232022-07-01 11:25:49 +020076 ecdsa_secp256r1_sha256, ecdsa_secp384r1_sha384, ecdsa_secp521r1_sha512,
77 rsa_pkcs1_sha256, rsa_pkcs1_sha384, rsa_pkcs1_sha512, rsa_pss_rsae_sha256,
78 rsa_pss_rsae_sha384 and rsa_pss_rsae_sha512.
Ronald Cronc3b510f2021-09-27 13:36:33 +020079
Ronald Cronfb877212021-09-28 15:49:39 +020080 Note that in absence of an application profile standard specifying otherwise
Ronald Cron2ba0d232022-07-01 11:25:49 +020081 rsa_pkcs1_sha256, rsa_pss_rsae_sha256 and ecdsa_secp256r1_sha256 are
82 mandatory (see section 9.1 of the specification).
Ronald Cronc3b510f2021-09-27 13:36:33 +020083
Jerry Yu72a05652022-01-25 14:36:30 +080084- Supported versions:
85
Ronald Cron2ba0d232022-07-01 11:25:49 +020086 - TLS 1.2 and TLS 1.3 with version negotiation on the client side, not server
87 side.
Jerry Yu72a05652022-01-25 14:36:30 +080088
Ronald Cron2ba0d232022-07-01 11:25:49 +020089 - TLS 1.2 and TLS 1.3 can be enabled in the build independently of each
90 other.
Jerry Yu72a05652022-01-25 14:36:30 +080091
Jerry Yuadb18692022-01-27 12:55:32 +080092 - If both TLS 1.3 and TLS 1.2 are enabled at build time, only one of them can
Ronald Cron2ba0d232022-07-01 11:25:49 +020093 be configured at runtime via `mbedtls_ssl_conf_{min,max}_tls_version` for a
94 server endpoint. Otherwise, `mbedtls_ssl_setup` will raise
95 `MBEDTLS_ERR_SSL_BAD_CONFIG` error.
Ronald Cron3785c902021-09-20 09:05:36 +020096
Ronald Cron3e7c4032021-09-27 14:22:38 +020097- Compatibility with existing SSL/TLS build options:
Ronald Cron3785c902021-09-20 09:05:36 +020098
Ronald Cron2ba0d232022-07-01 11:25:49 +020099 The TLS 1.3 implementation is compatible with nearly all TLS 1.2
100 configuration options in the sense that when enabling TLS 1.3 in the library
101 there is rarely any need to modify the configuration from that used for
102 TLS 1.2. There are two exceptions though: the TLS 1.3 implementation requires
103 MBEDTLS_PSA_CRYPTO_C and MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, so these options
104 must be enabled.
Tom Cosgroveafb2fe12022-06-29 16:36:12 +0100105
Ronald Cron3cb707d2022-07-01 14:36:52 +0200106 Most of the Mbed TLS SSL/TLS related options are not supported or not
107 applicable to the TLS 1.3 implementation:
Ronald Cron3785c902021-09-20 09:05:36 +0200108
Ronald Cron023987f2021-09-27 11:59:25 +0200109 | Mbed TLS configuration option | Support |
110 | ---------------------------------------- | ------- |
111 | MBEDTLS_SSL_ALL_ALERT_MESSAGES | no |
112 | MBEDTLS_SSL_ASYNC_PRIVATE | no |
113 | MBEDTLS_SSL_CONTEXT_SERIALIZATION | no |
114 | MBEDTLS_SSL_DEBUG_ALL | no |
115 | MBEDTLS_SSL_ENCRYPT_THEN_MAC | n/a |
116 | MBEDTLS_SSL_EXTENDED_MASTER_SECRET | n/a |
Tom Cosgroveafb2fe12022-06-29 16:36:12 +0100117 | MBEDTLS_SSL_KEEP_PEER_CERTIFICATE | no (1) |
Ronald Cron023987f2021-09-27 11:59:25 +0200118 | MBEDTLS_SSL_RENEGOTIATION | n/a |
119 | MBEDTLS_SSL_MAX_FRAGMENT_LENGTH | no |
120 | | |
121 | MBEDTLS_SSL_SESSION_TICKETS | no |
Ronald Cron2ba0d232022-07-01 11:25:49 +0200122 | MBEDTLS_SSL_SERVER_NAME_INDICATION | yes |
Ronald Cron023987f2021-09-27 11:59:25 +0200123 | MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH | no |
124 | | |
125 | MBEDTLS_ECP_RESTARTABLE | no |
126 | MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED | no |
127 | | |
Ronald Cron3cb707d2022-07-01 14:36:52 +0200128 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED | n/a (2) |
Ronald Cron023987f2021-09-27 11:59:25 +0200129 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED | n/a |
130 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED | n/a |
131 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED | n/a |
132 | MBEDTLS_KEY_EXCHANGE_RSA_ENABLED | n/a |
133 | MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED | n/a |
134 | MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED | n/a |
135 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED | n/a |
136 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED | n/a |
137 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED | n/a |
138 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED | n/a |
139 | | |
Tom Cosgroved7adb3c2022-06-30 09:48:40 +0100140 | MBEDTLS_PSA_CRYPTO_C | no (1) |
Ronald Cron2ba0d232022-07-01 11:25:49 +0200141 | MBEDTLS_USE_PSA_CRYPTO | yes |
Ronald Cron3785c902021-09-20 09:05:36 +0200142
Tom Cosgroved7adb3c2022-06-30 09:48:40 +0100143 (1) These options must remain in their default state of enabled.
Ronald Crond8d2ea52022-10-04 15:48:06 +0200144 (2) See the TLS 1.3 specific build options section below.
145
146- TLS 1.3 specific build options:
147
148 - MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE enables the support for middlebox
149 compatibility mode as defined in section D.4 of RFC 8446.
150
Ronald Cron9810b6d2022-10-20 14:22:45 +0200151 - MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED enables the support for
152 the PSK key exchange mode as defined by RFC 8446. If it is the only key
153 exchange mode enabled, the TLS 1.3 implementation does not contain any code
154 related to key exchange protocols, certificates and signatures.
155
156 - MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED enables the
Ronald Cron10bf9562022-10-21 08:51:33 +0200157 support for the ephemeral key exchange mode. If it is the only key exchange
Ronald Crond8d2ea52022-10-04 15:48:06 +0200158 mode enabled, the TLS 1.3 implementation does not contain any code related
159 to PSK based key exchange. The ephemeral key exchange mode requires at least
160 one of the key exchange protocol allowed by the TLS 1.3 specification, the
161 parsing and validation of x509 certificates and at least one signature
162 algorithm allowed by the TLS 1.3 specification for signature computing and
163 verification.
164
Ronald Cron9810b6d2022-10-20 14:22:45 +0200165 - MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED enables the
166 support for the PSK ephemeral key exchange mode. If it is the only key
Ronald Crond8d2ea52022-10-04 15:48:06 +0200167 exchange mode enabled, the TLS 1.3 implementation does not contain any code
Ronald Crond8d2ea52022-10-04 15:48:06 +0200168 related to certificates and signatures. The PSK ephemeral key exchange
Ronald Cron9810b6d2022-10-20 14:22:45 +0200169 mode requires at least one of the key exchange protocol allowed by the
Ronald Crond8d2ea52022-10-04 15:48:06 +0200170 TLS 1.3 specification.
Ronald Cron3785c902021-09-20 09:05:36 +0200171
Ronald Cron653d5bc2021-12-09 14:35:56 +0100172
Ronald Cron7aa6fc12021-12-09 14:53:59 +0100173Prototype upstreaming status
174----------------------------
Ronald Cron653d5bc2021-12-09 14:35:56 +0100175
Ronald Cron3cb707d2022-07-01 14:36:52 +0200176The following parts of the TLS 1.3 prototype remain to be upstreamed:
Ronald Cron653d5bc2021-12-09 14:35:56 +0100177
Ronald Cron7aa6fc12021-12-09 14:53:59 +0100178- Pre-shared keys, session resumption and 0-RTT data (both client and server
179 side).
Ronald Cron653d5bc2021-12-09 14:35:56 +0100180
181- New TLS Message Processing Stack (MPS)
182
183 The TLS 1.3 prototype is developed alongside a rewrite of the TLS messaging layer,
184 encompassing low-level details such as record parsing, handshake reassembly, and
185 DTLS retransmission state machine.
186
187 MPS has the following components:
188 - Layer 1 (Datagram handling)
189 - Layer 2 (Record handling)
190 - Layer 3 (Message handling)
191 - Layer 4 (Retransmission State Machine)
192 - Reader (Abstracted pointer arithmetic and reassembly logic for incoming data)
193 - Writer (Abstracted pointer arithmetic and fragmentation logic for outgoing data)
194
195 Of those components, the following have been upstreamed
196 as part of `MBEDTLS_SSL_PROTO_TLS1_3`:
197
198 - Reader ([`library/mps_reader.h`](../../library/mps_reader.h))
199
200
Ronald Cron3785c902021-09-20 09:05:36 +0200201Coding rules checklist for TLS 1.3
202----------------------------------
203
204The following coding rules are aimed to be a checklist for TLS 1.3 upstreaming
205work to reduce review rounds and the number of comments in each round. They
206come along (do NOT replace) the project coding rules
Dave Rodgmanb3196842022-10-12 16:47:08 +0100207(https://mbed-tls.readthedocs.io/en/latest/kb/development/mbedtls-coding-standards). They have been
Ronald Cron3785c902021-09-20 09:05:36 +0200208established and discussed following the review of #4882 that was the
209PR upstreaming the first part of TLS 1.3 ClientHello writing code.
210
211TLS 1.3 specific coding rules:
212
213 - TLS 1.3 specific C modules, headers, static functions names are prefixed
Ronald Cronb1944662021-09-27 13:56:46 +0200214 with `ssl_tls13_`. The same applies to structures and types that are
Ronald Cron3785c902021-09-20 09:05:36 +0200215 internal to C modules.
216
Ronald Cronb1944662021-09-27 13:56:46 +0200217 - TLS 1.3 specific exported functions, structures and types are
218 prefixed with `mbedtls_ssl_tls13_`.
219
220 - Use TLS1_3 in TLS 1.3 specific macros.
Ronald Cron3785c902021-09-20 09:05:36 +0200221
222 - The names of macros and variables related to a field or structure in the
223 TLS 1.3 specification should contain as far as possible the field name as
Ronald Cron72064b32021-09-27 13:54:28 +0200224 it is in the specification. If the field name is "too long" and we prefer
Ronald Cron3785c902021-09-20 09:05:36 +0200225 to introduce some kind of abbreviation of it, use the same abbreviation
226 everywhere in the code.
227
228 Example 1: #define CLIENT_HELLO_RANDOM_LEN 32, macro for the length of the
229 `random` field of the ClientHello message.
230
Dave Rodgmanc8aaac82021-10-18 12:56:53 +0100231 Example 2 (consistent abbreviation): `mbedtls_ssl_tls13_write_sig_alg_ext()`
Ronald Cron72064b32021-09-27 13:54:28 +0200232 and `MBEDTLS_TLS_EXT_SIG_ALG`, `sig_alg` standing for
Ronald Cron3785c902021-09-20 09:05:36 +0200233 `signature_algorithms`.
234
235 - Regarding vectors that are represented by a length followed by their value
236 in the data exchanged between servers and clients:
237
238 - Use `<vector name>_len` for the name of a variable used to compute the
239 length in bytes of the vector, where <vector name> is the name of the
240 vector as defined in the TLS 1.3 specification.
241
Ronald Cron99733f02021-09-27 13:58:21 +0200242 - Use `p_<vector_name>_len` for the name of a variable intended to hold
Ronald Cron3785c902021-09-20 09:05:36 +0200243 the address of the first byte of the vector length.
244
Ronald Cron99733f02021-09-27 13:58:21 +0200245 - Use `<vector_name>` for the name of a variable intended to hold the
Ronald Cron3785c902021-09-20 09:05:36 +0200246 address of the first byte of the vector value.
247
Ronald Cron99733f02021-09-27 13:58:21 +0200248 - Use `<vector_name>_end` for the name of a variable intended to hold
Ronald Cron3785c902021-09-20 09:05:36 +0200249 the address of the first byte past the vector value.
250
Ronald Cron99733f02021-09-27 13:58:21 +0200251 Those idioms should lower the risk of mis-using one of the address in place
252 of another one which could potentially lead to some nasty issues.
Ronald Cron3785c902021-09-20 09:05:36 +0200253
254 Example: `cipher_suites` vector of ClientHello in
Dave Rodgmanc8aaac82021-10-18 12:56:53 +0100255 `ssl_tls13_write_client_hello_cipher_suites()`
Ronald Cron72064b32021-09-27 13:54:28 +0200256 ```
257 size_t cipher_suites_len;
Ronald Cron99733f02021-09-27 13:58:21 +0200258 unsigned char *p_cipher_suites_len;
259 unsigned char *cipher_suites;
Ronald Cron72064b32021-09-27 13:54:28 +0200260 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200261
Ronald Cronfecda8d2021-09-27 13:59:38 +0200262 - Where applicable, use:
263 - the macros to extract a byte from a multi-byte integer MBEDTLS_BYTE_{0-8}.
264 - the macros to write in memory in big-endian order a multi-byte integer
265 MBEDTLS_PUT_UINT{8|16|32|64}_BE.
266 - the macros to read from memory a multi-byte integer in big-endian order
267 MBEDTLS_GET_UINT{8|16|32|64}_BE.
268 - the macro to check for space when writing into an output buffer
269 `MBEDTLS_SSL_CHK_BUF_PTR`.
270 - the macro to check for data when reading from an input buffer
271 `MBEDTLS_SSL_CHK_BUF_READ_PTR`.
Ronald Cron3785c902021-09-20 09:05:36 +0200272
273 These macros were introduced after the prototype was written thus are
274 likely not to be used in prototype where we now would use them in
275 development.
276
Ronald Cronfecda8d2021-09-27 13:59:38 +0200277 The three first types, MBEDTLS_BYTE_{0-8}, MBEDTLS_PUT_UINT{8|16|32|64}_BE
278 and MBEDTLS_GET_UINT{8|16|32|64}_BE improve the readability of the code and
279 reduce the risk of writing or reading bytes in the wrong order.
Ronald Cron3785c902021-09-20 09:05:36 +0200280
Ronald Cron72064b32021-09-27 13:54:28 +0200281 The two last types, `MBEDTLS_SSL_CHK_BUF_PTR` and
282 `MBEDTLS_SSL_CHK_BUF_READ_PTR`, improve the readability of the code and
Ronald Cron3785c902021-09-20 09:05:36 +0200283 reduce the risk of error in the non-completely-trivial arithmetic to
284 check that we do not write or read past the end of a data buffer. The
285 usage of those macros combined with the following rule mitigate the risk
286 to read/write past the end of a data buffer.
287
Ronald Cron72064b32021-09-27 13:54:28 +0200288 Examples:
289 ```
290 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
291 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
292 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
293 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200294
295 - To mitigate what happened here
Dave Rodgman017a1992022-03-31 14:07:01 +0100296 (https://github.com/Mbed-TLS/mbedtls/pull/4882#discussion_r701704527) from
Ronald Cron3785c902021-09-20 09:05:36 +0200297 happening again, use always a local variable named `p` for the reading
298 pointer in functions parsing TLS 1.3 data, and for the writing pointer in
Ronald Cron3e7c4032021-09-27 14:22:38 +0200299 functions writing data into an output buffer and only that variable. The
300 name `p` has been chosen as it was already widely used in TLS code.
Ronald Cron3785c902021-09-20 09:05:36 +0200301
302 - When an TLS 1.3 structure is written or read by a function or as part of
303 a function, provide as documentation the definition of the structure as
304 it is in the TLS 1.3 specification.
305
306General coding rules:
307
Ronald Cron72064b32021-09-27 13:54:28 +0200308 - We prefer grouping "related statement lines" by not adding blank lines
Ronald Cron3785c902021-09-20 09:05:36 +0200309 between them.
310
311 Example 1:
Ronald Cron72064b32021-09-27 13:54:28 +0200312 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200313 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len );
314 if( ret != 0 )
315 return( ret );
316 buf += output_len;
Ronald Cron72064b32021-09-27 13:54:28 +0200317 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200318
319 Example 2:
Ronald Cron72064b32021-09-27 13:54:28 +0200320 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200321 MBEDTLS_SSL_CHK_BUF_PTR( cipher_suites_iter, end, 2 );
322 MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 );
323 cipher_suites_iter += 2;
Ronald Cron72064b32021-09-27 13:54:28 +0200324 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200325
326 - Use macros for constants that are used in different functions, different
327 places in the code. When a constant is used only locally in a function
328 (like the length in bytes of the vector lengths in functions reading and
329 writing TLS handshake message) there is no need to define a macro for it.
330
Ronald Cron72064b32021-09-27 13:54:28 +0200331 Example: `#define CLIENT_HELLO_RANDOM_LEN 32`
Ronald Cron3785c902021-09-20 09:05:36 +0200332
333 - When declaring a pointer the dereferencing operator should be prepended to
334 the pointer name not appended to the pointer type:
335
Ronald Cron72064b32021-09-27 13:54:28 +0200336 Example: `mbedtls_ssl_context *ssl;`
Ronald Cron3785c902021-09-20 09:05:36 +0200337
338 - Maximum line length is 80 characters.
339
340 Exceptions:
341
342 - string literals can extend beyond 80 characters as we do not want to
343 split them to ease their search in the code base.
344
345 - A line can be more than 80 characters by a few characters if just looking
346 at the 80 first characters is enough to fully understand the line. For
347 example it is generally fine if some closure characters like ";" or ")"
348 are beyond the 80 characters limit.
349
Ronald Cron847c3582021-09-27 14:24:43 +0200350 If a line becomes too long due to a refactoring (for example renaming a
351 function to a longer name, or indenting a block more), avoid rewrapping
352 lines in the same commit: it makes the review harder. Make one commit with
353 the longer lines and another commit with just the rewrapping.
354
Ronald Cron3785c902021-09-20 09:05:36 +0200355 - When in successive lines, functions and macros parameters should be aligned
356 vertically.
357
358 Example:
Ronald Cron72064b32021-09-27 13:54:28 +0200359 ```
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100360 int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl,
361 unsigned hs_type,
362 unsigned char **buf,
363 size_t *buf_len );
Ronald Cron72064b32021-09-27 13:54:28 +0200364 ```
Ronald Cron847c3582021-09-27 14:24:43 +0200365
366 - When a function's parameters span several lines, group related parameters
367 together if possible.
368
369 For example, prefer:
370
371 ```
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100372 mbedtls_ssl_start_handshake_msg( ssl, hs_type,
373 buf, buf_len );
Ronald Cron847c3582021-09-27 14:24:43 +0200374 ```
375 over
376 ```
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100377 mbedtls_ssl_start_handshake_msg( ssl, hs_type, buf,
378 buf_len );
Ronald Cron847c3582021-09-27 14:24:43 +0200379 ```
380 even if it fits.
Ronald Cron44b23b12022-05-31 16:05:13 +0200381
382
383Overview of handshake code organization
384---------------------------------------
385
386The TLS 1.3 handshake protocol is implemented as a state machine. The
Ronald Cron6b14c692022-06-24 13:45:04 +0200387functions `mbedtls_ssl_tls13_handshake_{client,server}_step` are the top level
Ronald Cron44b23b12022-05-31 16:05:13 +0200388functions of that implementation. They are implemented as a switch over all the
389possible states of the state machine.
390
391Most of the states are either dedicated to the processing or writing of an
392handshake message.
393
394The implementation does not go systematically through all states as this would
395result in too many checks of whether something needs to be done or not in a
396given state to be duplicated across several state handlers. For example, on
397client side, the states related to certificate parsing and validation are
398bypassed if the handshake is based on a pre-shared key and thus does not
399involve certificates.
400
401On the contrary, the implementation goes systematically though some states
402even if they could be bypassed if it helps in minimizing when and where inbound
403and outbound keys are updated. The `MBEDTLS_SSL_CLIENT_CERTIFICATE` state on
404client side is a example of that.
405
406The names of the handlers processing/writing an handshake message are
Ronald Cron6b14c692022-06-24 13:45:04 +0200407prefixed with `(mbedtls_)ssl_tls13_{process,write}`. To ease the maintenance and
Ronald Cron44b23b12022-05-31 16:05:13 +0200408reduce the risk of bugs, the code of the message processing and writing
409handlers is split into a sequence of stages.
410
411The sending of data to the peer only occurs in `mbedtls_ssl_handshake_step`
412between the calls to the handlers and as a consequence handlers do not have to
413care about the MBEDTLS_ERR_SSL_WANT_WRITE error code. Furthermore, all pending
414data are flushed before to call the next handler. That way, handlers do not
415have to worry about pending data when changing outbound keys.
416
417### Message processing handlers
418For message processing handlers, the stages are:
419
420* coordination stage: check if the state should be bypassed. This stage is
421optional. The check is either purely based on the reading of the value of some
422fields of the SSL context or based on the reading of the type of the next
423message. The latter occurs when it is not known what the next handshake message
424will be, an example of that on client side being if we are going to receive a
425CertificateRequest message or not. The intent is, apart from the next record
426reading to not modify the SSL context as this stage may be repeated if the
427next handshake message has not been received yet.
428
429* fetching stage: at this stage we are sure of the type of the handshake
430message we must receive next and we try to fetch it. If we did not go through
431a coordination stage involving the next record type reading, the next
432handshake message may not have been received yet, the handler returns with
433`MBEDTLS_ERR_SSL_WANT_READ` without changing the current state and it will be
434called again later.
435
436* pre-processing stage: prepare the SSL context for the message parsing. This
437stage is optional. Any processing that must be done before the parsing of the
438message or that can be done to simplify the parsing code. Some simple and
439partial parsing of the handshake message may append at that stage like in the
440ServerHello message pre-processing.
441
442* parsing stage: parse the message and restrict as much as possible any
443update of the SSL context. The idea of the pre-processing/parsing/post-processing
444organization is to concentrate solely on the parsing in the parsing function to
445reduce the size of its code and to simplify it.
446
447* post-processing stage: following the parsing, further update of the SSL
Ronald Cron139d0aa2022-06-14 18:45:44 +0200448context to prepare for the next incoming and outgoing messages. This stage is
Ronald Cron44b23b12022-05-31 16:05:13 +0200449optional. For example, secret and key computations occur at this stage, as well
450as handshake messages checksum update.
451
452* state change: the state change is done in the main state handler to ease the
453navigation of the state machine transitions.
454
455
456### Message writing handlers
457For message writing handlers, the stages are:
458
459* coordination stage: check if the state should be bypassed. This stage is
460optional. The check is based on the value of some fields of the SSL context.
461
462* preparation stage: prepare for the message writing. This stage is optional.
463Any processing that must be done before the writing of the message or that can
464be done to simplify the writing code.
465
466* writing stage: write the message and restrict as much as possible any update
467of the SSL context. The idea of the preparation/writing/finalization
468organization is to concentrate solely on the writing in the writing function to
469reduce the size of its code and simplify it.
470
471* finalization stage: following the writing, further update of the SSL
472context to prepare for the next incoming and outgoing messages. This stage is
473optional. For example, handshake secret and key computation occur at that
474stage (ServerHello writing finalization), switching to handshake keys for
475outbound message on server side as well.
476
477* state change: the state change is done in the main state handler to ease
478the navigation of the state machine transitions.