blob: 33fe76d1a3d0800230e0fd587a90dd27a9d0e34f [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 Cron124ed8a2024-03-13 10:41:37 +010029 - Mbed TLS supports DHE key establishment.
Ronald Cronf164b6a2021-09-27 15:36:29 +020030
Ronald Cron93dcb1b2022-10-03 12:02:17 +020031 - Mbed TLS supports pre-shared keys for key establishment, pre-shared keys
32 provisioned externally as well as provisioned via the ticket mechanism.
33
34 - Mbed TLS supports session resumption via the ticket mechanism.
35
36 - Mbed TLS does not support sending or receiving early data (0-RTT data).
Ronald Cronf164b6a2021-09-27 15:36:29 +020037
Ronald Cron3785c902021-09-20 09:05:36 +020038- Supported cipher suites: depends on the library configuration. Potentially
39 all of them:
40 TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256,
41 TLS_AES_128_CCM_SHA256 and TLS_AES_128_CCM_8_SHA256.
42
43- Supported ClientHello extensions:
44
Ronald Cron3cb707d2022-07-01 14:36:52 +020045 | Extension | Support |
46 | ---------------------------- | ------- |
47 | server_name | YES |
48 | max_fragment_length | no |
49 | status_request | no |
50 | supported_groups | YES |
51 | signature_algorithms | YES |
52 | use_srtp | no |
53 | heartbeat | no |
Ronald Cron124ed8a2024-03-13 10:41:37 +010054 | alpn | YES |
Ronald Cron3cb707d2022-07-01 14:36:52 +020055 | signed_certificate_timestamp | no |
56 | client_certificate_type | no |
57 | server_certificate_type | no |
58 | padding | no |
59 | key_share | YES |
Ronald Cron93dcb1b2022-10-03 12:02:17 +020060 | pre_shared_key | YES |
61 | psk_key_exchange_modes | YES |
Ronald Cron3cb707d2022-07-01 14:36:52 +020062 | early_data | no |
63 | cookie | no |
64 | supported_versions | YES |
65 | certificate_authorities | no |
66 | post_handshake_auth | no |
67 | signature_algorithms_cert | no |
Ronald Cron3785c902021-09-20 09:05:36 +020068
Ronald Cron023987f2021-09-27 11:59:25 +020069
Ronald Cron3785c902021-09-20 09:05:36 +020070- Supported groups: depends on the library configuration.
Ronald Cron2ba0d232022-07-01 11:25:49 +020071 Potentially all ECDHE groups:
72 secp256r1, x25519, secp384r1, x448 and secp521r1.
Ronald Cronc3b510f2021-09-27 13:36:33 +020073
Ronald Cron124ed8a2024-03-13 10:41:37 +010074 Potentially all DHE groups:
75 ffdhe2048, ffdhe3072, ffdhe4096, ffdhe6144 and ffdhe8192.
Ronald Cronc3b510f2021-09-27 13:36:33 +020076
Ronald Cronfb877212021-09-28 15:49:39 +020077- Supported signature algorithms (both for certificates and CertificateVerify):
78 depends on the library configuration.
79 Potentially:
Ronald Cron2ba0d232022-07-01 11:25:49 +020080 ecdsa_secp256r1_sha256, ecdsa_secp384r1_sha384, ecdsa_secp521r1_sha512,
81 rsa_pkcs1_sha256, rsa_pkcs1_sha384, rsa_pkcs1_sha512, rsa_pss_rsae_sha256,
82 rsa_pss_rsae_sha384 and rsa_pss_rsae_sha512.
Ronald Cronc3b510f2021-09-27 13:36:33 +020083
Ronald Cronfb877212021-09-28 15:49:39 +020084 Note that in absence of an application profile standard specifying otherwise
Ronald Cron2ba0d232022-07-01 11:25:49 +020085 rsa_pkcs1_sha256, rsa_pss_rsae_sha256 and ecdsa_secp256r1_sha256 are
86 mandatory (see section 9.1 of the specification).
Ronald Cronc3b510f2021-09-27 13:36:33 +020087
Jerry Yu72a05652022-01-25 14:36:30 +080088- Supported versions:
89
Ronald Cron4d314962023-03-14 16:46:22 +010090 - TLS 1.2 and TLS 1.3 with version negotiation on client and server side.
Jerry Yu72a05652022-01-25 14:36:30 +080091
Ronald Cron2ba0d232022-07-01 11:25:49 +020092 - TLS 1.2 and TLS 1.3 can be enabled in the build independently of each
93 other.
Jerry Yu72a05652022-01-25 14:36:30 +080094
Ronald Cron3e7c4032021-09-27 14:22:38 +020095- Compatibility with existing SSL/TLS build options:
Ronald Cron3785c902021-09-20 09:05:36 +020096
Ronald Cron2ba0d232022-07-01 11:25:49 +020097 The TLS 1.3 implementation is compatible with nearly all TLS 1.2
98 configuration options in the sense that when enabling TLS 1.3 in the library
99 there is rarely any need to modify the configuration from that used for
100 TLS 1.2. There are two exceptions though: the TLS 1.3 implementation requires
101 MBEDTLS_PSA_CRYPTO_C and MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, so these options
102 must be enabled.
Tom Cosgroveafb2fe12022-06-29 16:36:12 +0100103
Ronald Cron3cb707d2022-07-01 14:36:52 +0200104 Most of the Mbed TLS SSL/TLS related options are not supported or not
105 applicable to the TLS 1.3 implementation:
Ronald Cron3785c902021-09-20 09:05:36 +0200106
Ronald Cron023987f2021-09-27 11:59:25 +0200107 | Mbed TLS configuration option | Support |
108 | ---------------------------------------- | ------- |
Ronald Cron124ed8a2024-03-13 10:41:37 +0100109 | MBEDTLS_SSL_ALL_ALERT_MESSAGES | yes |
Ronald Cron023987f2021-09-27 11:59:25 +0200110 | MBEDTLS_SSL_ASYNC_PRIVATE | no |
111 | MBEDTLS_SSL_CONTEXT_SERIALIZATION | no |
112 | MBEDTLS_SSL_DEBUG_ALL | no |
113 | MBEDTLS_SSL_ENCRYPT_THEN_MAC | n/a |
114 | MBEDTLS_SSL_EXTENDED_MASTER_SECRET | n/a |
Tom Cosgroveafb2fe12022-06-29 16:36:12 +0100115 | MBEDTLS_SSL_KEEP_PEER_CERTIFICATE | no (1) |
Ronald Cron023987f2021-09-27 11:59:25 +0200116 | MBEDTLS_SSL_RENEGOTIATION | n/a |
117 | MBEDTLS_SSL_MAX_FRAGMENT_LENGTH | no |
118 | | |
Ronald Cron93dcb1b2022-10-03 12:02:17 +0200119 | MBEDTLS_SSL_SESSION_TICKETS | yes |
Ronald Cron2ba0d232022-07-01 11:25:49 +0200120 | MBEDTLS_SSL_SERVER_NAME_INDICATION | yes |
Ronald Cron023987f2021-09-27 11:59:25 +0200121 | MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH | no |
122 | | |
123 | MBEDTLS_ECP_RESTARTABLE | no |
124 | MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED | no |
125 | | |
Ronald Cron3cb707d2022-07-01 14:36:52 +0200126 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED | n/a (2) |
Ronald Cron023987f2021-09-27 11:59:25 +0200127 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED | n/a |
128 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED | n/a |
129 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED | n/a |
130 | MBEDTLS_KEY_EXCHANGE_RSA_ENABLED | n/a |
131 | MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED | n/a |
132 | MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED | n/a |
133 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED | n/a |
134 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED | n/a |
135 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED | n/a |
136 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED | n/a |
137 | | |
Tom Cosgroved7adb3c2022-06-30 09:48:40 +0100138 | MBEDTLS_PSA_CRYPTO_C | no (1) |
Ronald Cron2ba0d232022-07-01 11:25:49 +0200139 | MBEDTLS_USE_PSA_CRYPTO | yes |
Ronald Cron3785c902021-09-20 09:05:36 +0200140
Tom Cosgroved7adb3c2022-06-30 09:48:40 +0100141 (1) These options must remain in their default state of enabled.
Ronald Crond8d2ea52022-10-04 15:48:06 +0200142 (2) See the TLS 1.3 specific build options section below.
143
144- TLS 1.3 specific build options:
145
146 - MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE enables the support for middlebox
147 compatibility mode as defined in section D.4 of RFC 8446.
148
Ronald Cron9810b6d2022-10-20 14:22:45 +0200149 - MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED enables the support for
150 the PSK key exchange mode as defined by RFC 8446. If it is the only key
151 exchange mode enabled, the TLS 1.3 implementation does not contain any code
152 related to key exchange protocols, certificates and signatures.
153
154 - MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED enables the
Ronald Cron10bf9562022-10-21 08:51:33 +0200155 support for the ephemeral key exchange mode. If it is the only key exchange
Ronald Crond8d2ea52022-10-04 15:48:06 +0200156 mode enabled, the TLS 1.3 implementation does not contain any code related
157 to PSK based key exchange. The ephemeral key exchange mode requires at least
158 one of the key exchange protocol allowed by the TLS 1.3 specification, the
159 parsing and validation of x509 certificates and at least one signature
160 algorithm allowed by the TLS 1.3 specification for signature computing and
161 verification.
162
Ronald Cron9810b6d2022-10-20 14:22:45 +0200163 - MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED enables the
164 support for the PSK ephemeral key exchange mode. If it is the only key
Ronald Crond8d2ea52022-10-04 15:48:06 +0200165 exchange mode enabled, the TLS 1.3 implementation does not contain any code
Ronald Crond8d2ea52022-10-04 15:48:06 +0200166 related to certificates and signatures. The PSK ephemeral key exchange
Ronald Cron9810b6d2022-10-20 14:22:45 +0200167 mode requires at least one of the key exchange protocol allowed by the
Ronald Crond8d2ea52022-10-04 15:48:06 +0200168 TLS 1.3 specification.
Ronald Cron3785c902021-09-20 09:05:36 +0200169
Ronald Cron653d5bc2021-12-09 14:35:56 +0100170
Ronald Cron7aa6fc12021-12-09 14:53:59 +0100171Prototype upstreaming status
172----------------------------
Ronald Cron653d5bc2021-12-09 14:35:56 +0100173
Ronald Cron3cb707d2022-07-01 14:36:52 +0200174The following parts of the TLS 1.3 prototype remain to be upstreamed:
Ronald Cron653d5bc2021-12-09 14:35:56 +0100175
Ronald Cron93dcb1b2022-10-03 12:02:17 +0200176- Sending (client) and receiving (server) early data (0-RTT data).
Ronald Cron653d5bc2021-12-09 14:35:56 +0100177
178- New TLS Message Processing Stack (MPS)
179
180 The TLS 1.3 prototype is developed alongside a rewrite of the TLS messaging layer,
181 encompassing low-level details such as record parsing, handshake reassembly, and
182 DTLS retransmission state machine.
183
184 MPS has the following components:
185 - Layer 1 (Datagram handling)
186 - Layer 2 (Record handling)
187 - Layer 3 (Message handling)
188 - Layer 4 (Retransmission State Machine)
189 - Reader (Abstracted pointer arithmetic and reassembly logic for incoming data)
190 - Writer (Abstracted pointer arithmetic and fragmentation logic for outgoing data)
191
192 Of those components, the following have been upstreamed
193 as part of `MBEDTLS_SSL_PROTO_TLS1_3`:
194
195 - Reader ([`library/mps_reader.h`](../../library/mps_reader.h))
196
197
Ronald Cron3785c902021-09-20 09:05:36 +0200198Coding rules checklist for TLS 1.3
199----------------------------------
200
201The following coding rules are aimed to be a checklist for TLS 1.3 upstreaming
202work to reduce review rounds and the number of comments in each round. They
203come along (do NOT replace) the project coding rules
Dave Rodgmanb3196842022-10-12 16:47:08 +0100204(https://mbed-tls.readthedocs.io/en/latest/kb/development/mbedtls-coding-standards). They have been
Ronald Cron3785c902021-09-20 09:05:36 +0200205established and discussed following the review of #4882 that was the
206PR upstreaming the first part of TLS 1.3 ClientHello writing code.
207
208TLS 1.3 specific coding rules:
209
210 - TLS 1.3 specific C modules, headers, static functions names are prefixed
Ronald Cronb1944662021-09-27 13:56:46 +0200211 with `ssl_tls13_`. The same applies to structures and types that are
Ronald Cron3785c902021-09-20 09:05:36 +0200212 internal to C modules.
213
Ronald Cronb1944662021-09-27 13:56:46 +0200214 - TLS 1.3 specific exported functions, structures and types are
215 prefixed with `mbedtls_ssl_tls13_`.
216
217 - Use TLS1_3 in TLS 1.3 specific macros.
Ronald Cron3785c902021-09-20 09:05:36 +0200218
219 - The names of macros and variables related to a field or structure in the
220 TLS 1.3 specification should contain as far as possible the field name as
Ronald Cron72064b32021-09-27 13:54:28 +0200221 it is in the specification. If the field name is "too long" and we prefer
Ronald Cron3785c902021-09-20 09:05:36 +0200222 to introduce some kind of abbreviation of it, use the same abbreviation
223 everywhere in the code.
224
225 Example 1: #define CLIENT_HELLO_RANDOM_LEN 32, macro for the length of the
226 `random` field of the ClientHello message.
227
Dave Rodgmanc8aaac82021-10-18 12:56:53 +0100228 Example 2 (consistent abbreviation): `mbedtls_ssl_tls13_write_sig_alg_ext()`
Ronald Cron72064b32021-09-27 13:54:28 +0200229 and `MBEDTLS_TLS_EXT_SIG_ALG`, `sig_alg` standing for
Ronald Cron3785c902021-09-20 09:05:36 +0200230 `signature_algorithms`.
231
232 - Regarding vectors that are represented by a length followed by their value
233 in the data exchanged between servers and clients:
234
235 - Use `<vector name>_len` for the name of a variable used to compute the
236 length in bytes of the vector, where <vector name> is the name of the
237 vector as defined in the TLS 1.3 specification.
238
Ronald Cron99733f02021-09-27 13:58:21 +0200239 - Use `p_<vector_name>_len` for the name of a variable intended to hold
Ronald Cron3785c902021-09-20 09:05:36 +0200240 the address of the first byte of the vector length.
241
Ronald Cron99733f02021-09-27 13:58:21 +0200242 - Use `<vector_name>` for the name of a variable intended to hold the
Ronald Cron3785c902021-09-20 09:05:36 +0200243 address of the first byte of the vector value.
244
Ronald Cron99733f02021-09-27 13:58:21 +0200245 - Use `<vector_name>_end` for the name of a variable intended to hold
Ronald Cron3785c902021-09-20 09:05:36 +0200246 the address of the first byte past the vector value.
247
Ronald Cron99733f02021-09-27 13:58:21 +0200248 Those idioms should lower the risk of mis-using one of the address in place
249 of another one which could potentially lead to some nasty issues.
Ronald Cron3785c902021-09-20 09:05:36 +0200250
251 Example: `cipher_suites` vector of ClientHello in
Dave Rodgmanc8aaac82021-10-18 12:56:53 +0100252 `ssl_tls13_write_client_hello_cipher_suites()`
Ronald Cron72064b32021-09-27 13:54:28 +0200253 ```
254 size_t cipher_suites_len;
Ronald Cron99733f02021-09-27 13:58:21 +0200255 unsigned char *p_cipher_suites_len;
256 unsigned char *cipher_suites;
Ronald Cron72064b32021-09-27 13:54:28 +0200257 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200258
Ronald Cronfecda8d2021-09-27 13:59:38 +0200259 - Where applicable, use:
260 - the macros to extract a byte from a multi-byte integer MBEDTLS_BYTE_{0-8}.
261 - the macros to write in memory in big-endian order a multi-byte integer
262 MBEDTLS_PUT_UINT{8|16|32|64}_BE.
263 - the macros to read from memory a multi-byte integer in big-endian order
264 MBEDTLS_GET_UINT{8|16|32|64}_BE.
265 - the macro to check for space when writing into an output buffer
266 `MBEDTLS_SSL_CHK_BUF_PTR`.
267 - the macro to check for data when reading from an input buffer
268 `MBEDTLS_SSL_CHK_BUF_READ_PTR`.
Ronald Cron3785c902021-09-20 09:05:36 +0200269
270 These macros were introduced after the prototype was written thus are
271 likely not to be used in prototype where we now would use them in
272 development.
273
Ronald Cronfecda8d2021-09-27 13:59:38 +0200274 The three first types, MBEDTLS_BYTE_{0-8}, MBEDTLS_PUT_UINT{8|16|32|64}_BE
275 and MBEDTLS_GET_UINT{8|16|32|64}_BE improve the readability of the code and
276 reduce the risk of writing or reading bytes in the wrong order.
Ronald Cron3785c902021-09-20 09:05:36 +0200277
Ronald Cron72064b32021-09-27 13:54:28 +0200278 The two last types, `MBEDTLS_SSL_CHK_BUF_PTR` and
279 `MBEDTLS_SSL_CHK_BUF_READ_PTR`, improve the readability of the code and
Ronald Cron3785c902021-09-20 09:05:36 +0200280 reduce the risk of error in the non-completely-trivial arithmetic to
281 check that we do not write or read past the end of a data buffer. The
282 usage of those macros combined with the following rule mitigate the risk
283 to read/write past the end of a data buffer.
284
Ronald Cron72064b32021-09-27 13:54:28 +0200285 Examples:
286 ```
287 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
288 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
289 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
290 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200291
292 - To mitigate what happened here
Dave Rodgman017a1992022-03-31 14:07:01 +0100293 (https://github.com/Mbed-TLS/mbedtls/pull/4882#discussion_r701704527) from
Ronald Cron3785c902021-09-20 09:05:36 +0200294 happening again, use always a local variable named `p` for the reading
295 pointer in functions parsing TLS 1.3 data, and for the writing pointer in
Ronald Cron3e7c4032021-09-27 14:22:38 +0200296 functions writing data into an output buffer and only that variable. The
297 name `p` has been chosen as it was already widely used in TLS code.
Ronald Cron3785c902021-09-20 09:05:36 +0200298
299 - When an TLS 1.3 structure is written or read by a function or as part of
300 a function, provide as documentation the definition of the structure as
301 it is in the TLS 1.3 specification.
302
303General coding rules:
304
Ronald Cron72064b32021-09-27 13:54:28 +0200305 - We prefer grouping "related statement lines" by not adding blank lines
Ronald Cron3785c902021-09-20 09:05:36 +0200306 between them.
307
308 Example 1:
Ronald Cron72064b32021-09-27 13:54:28 +0200309 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200310 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len );
311 if( ret != 0 )
312 return( ret );
313 buf += output_len;
Ronald Cron72064b32021-09-27 13:54:28 +0200314 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200315
316 Example 2:
Ronald Cron72064b32021-09-27 13:54:28 +0200317 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200318 MBEDTLS_SSL_CHK_BUF_PTR( cipher_suites_iter, end, 2 );
319 MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 );
320 cipher_suites_iter += 2;
Ronald Cron72064b32021-09-27 13:54:28 +0200321 ```
Ronald Cron3785c902021-09-20 09:05:36 +0200322
323 - Use macros for constants that are used in different functions, different
324 places in the code. When a constant is used only locally in a function
325 (like the length in bytes of the vector lengths in functions reading and
326 writing TLS handshake message) there is no need to define a macro for it.
327
Ronald Cron72064b32021-09-27 13:54:28 +0200328 Example: `#define CLIENT_HELLO_RANDOM_LEN 32`
Ronald Cron3785c902021-09-20 09:05:36 +0200329
330 - When declaring a pointer the dereferencing operator should be prepended to
331 the pointer name not appended to the pointer type:
332
Ronald Cron72064b32021-09-27 13:54:28 +0200333 Example: `mbedtls_ssl_context *ssl;`
Ronald Cron3785c902021-09-20 09:05:36 +0200334
335 - Maximum line length is 80 characters.
336
337 Exceptions:
338
339 - string literals can extend beyond 80 characters as we do not want to
340 split them to ease their search in the code base.
341
342 - A line can be more than 80 characters by a few characters if just looking
343 at the 80 first characters is enough to fully understand the line. For
344 example it is generally fine if some closure characters like ";" or ")"
345 are beyond the 80 characters limit.
346
Ronald Cron847c3582021-09-27 14:24:43 +0200347 If a line becomes too long due to a refactoring (for example renaming a
348 function to a longer name, or indenting a block more), avoid rewrapping
349 lines in the same commit: it makes the review harder. Make one commit with
350 the longer lines and another commit with just the rewrapping.
351
Ronald Cron3785c902021-09-20 09:05:36 +0200352 - When in successive lines, functions and macros parameters should be aligned
353 vertically.
354
355 Example:
Ronald Cron72064b32021-09-27 13:54:28 +0200356 ```
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100357 int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl,
358 unsigned hs_type,
359 unsigned char **buf,
360 size_t *buf_len );
Ronald Cron72064b32021-09-27 13:54:28 +0200361 ```
Ronald Cron847c3582021-09-27 14:24:43 +0200362
363 - When a function's parameters span several lines, group related parameters
364 together if possible.
365
366 For example, prefer:
367
368 ```
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100369 mbedtls_ssl_start_handshake_msg( ssl, hs_type,
370 buf, buf_len );
Ronald Cron847c3582021-09-27 14:24:43 +0200371 ```
372 over
373 ```
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100374 mbedtls_ssl_start_handshake_msg( ssl, hs_type, buf,
375 buf_len );
Ronald Cron847c3582021-09-27 14:24:43 +0200376 ```
377 even if it fits.
Ronald Cron44b23b12022-05-31 16:05:13 +0200378
379
380Overview of handshake code organization
381---------------------------------------
382
383The TLS 1.3 handshake protocol is implemented as a state machine. The
Ronald Cron6b14c692022-06-24 13:45:04 +0200384functions `mbedtls_ssl_tls13_handshake_{client,server}_step` are the top level
Ronald Cron44b23b12022-05-31 16:05:13 +0200385functions of that implementation. They are implemented as a switch over all the
386possible states of the state machine.
387
388Most of the states are either dedicated to the processing or writing of an
389handshake message.
390
391The implementation does not go systematically through all states as this would
392result in too many checks of whether something needs to be done or not in a
393given state to be duplicated across several state handlers. For example, on
394client side, the states related to certificate parsing and validation are
395bypassed if the handshake is based on a pre-shared key and thus does not
396involve certificates.
397
398On the contrary, the implementation goes systematically though some states
399even if they could be bypassed if it helps in minimizing when and where inbound
400and outbound keys are updated. The `MBEDTLS_SSL_CLIENT_CERTIFICATE` state on
401client side is a example of that.
402
403The names of the handlers processing/writing an handshake message are
Ronald Cron6b14c692022-06-24 13:45:04 +0200404prefixed with `(mbedtls_)ssl_tls13_{process,write}`. To ease the maintenance and
Ronald Cron44b23b12022-05-31 16:05:13 +0200405reduce the risk of bugs, the code of the message processing and writing
406handlers is split into a sequence of stages.
407
408The sending of data to the peer only occurs in `mbedtls_ssl_handshake_step`
409between the calls to the handlers and as a consequence handlers do not have to
410care about the MBEDTLS_ERR_SSL_WANT_WRITE error code. Furthermore, all pending
411data are flushed before to call the next handler. That way, handlers do not
412have to worry about pending data when changing outbound keys.
413
414### Message processing handlers
415For message processing handlers, the stages are:
416
417* coordination stage: check if the state should be bypassed. This stage is
418optional. The check is either purely based on the reading of the value of some
419fields of the SSL context or based on the reading of the type of the next
420message. The latter occurs when it is not known what the next handshake message
421will be, an example of that on client side being if we are going to receive a
422CertificateRequest message or not. The intent is, apart from the next record
423reading to not modify the SSL context as this stage may be repeated if the
424next handshake message has not been received yet.
425
426* fetching stage: at this stage we are sure of the type of the handshake
427message we must receive next and we try to fetch it. If we did not go through
428a coordination stage involving the next record type reading, the next
429handshake message may not have been received yet, the handler returns with
430`MBEDTLS_ERR_SSL_WANT_READ` without changing the current state and it will be
431called again later.
432
433* pre-processing stage: prepare the SSL context for the message parsing. This
434stage is optional. Any processing that must be done before the parsing of the
435message or that can be done to simplify the parsing code. Some simple and
436partial parsing of the handshake message may append at that stage like in the
437ServerHello message pre-processing.
438
439* parsing stage: parse the message and restrict as much as possible any
440update of the SSL context. The idea of the pre-processing/parsing/post-processing
441organization is to concentrate solely on the parsing in the parsing function to
442reduce the size of its code and to simplify it.
443
444* post-processing stage: following the parsing, further update of the SSL
Ronald Cron139d0aa2022-06-14 18:45:44 +0200445context to prepare for the next incoming and outgoing messages. This stage is
Ronald Cron44b23b12022-05-31 16:05:13 +0200446optional. For example, secret and key computations occur at this stage, as well
447as handshake messages checksum update.
448
449* state change: the state change is done in the main state handler to ease the
450navigation of the state machine transitions.
451
452
453### Message writing handlers
454For message writing handlers, the stages are:
455
456* coordination stage: check if the state should be bypassed. This stage is
457optional. The check is based on the value of some fields of the SSL context.
458
459* preparation stage: prepare for the message writing. This stage is optional.
460Any processing that must be done before the writing of the message or that can
461be done to simplify the writing code.
462
463* writing stage: write the message and restrict as much as possible any update
464of the SSL context. The idea of the preparation/writing/finalization
465organization is to concentrate solely on the writing in the writing function to
466reduce the size of its code and simplify it.
467
468* finalization stage: following the writing, further update of the SSL
469context to prepare for the next incoming and outgoing messages. This stage is
470optional. For example, handshake secret and key computation occur at that
471stage (ServerHello writing finalization), switching to handshake keys for
472outbound message on server side as well.
473
474* state change: the state change is done in the main state handler to ease
475the navigation of the state machine transitions.
Ronald Cron4a8c9e22022-10-26 18:49:09 +0200476
477
478Writing and reading early or 0-RTT data
479---------------------------------------
480
481An application function to write and send a buffer of data to a server through
482TLS may plausibly look like:
483
484```
485int write_data( mbedtls_ssl_context *ssl,
486 const unsigned char *data_to_write,
487 size_t data_to_write_len,
488 size_t *data_written )
489{
490 *data_written = 0;
491
492 while( *data_written < data_to_write_len )
493 {
494 ret = mbedtls_ssl_write( ssl, data_to_write + *data_written,
495 data_to_write_len - *data_written );
496
497 if( ret < 0 &&
498 ret != MBEDTLS_ERR_SSL_WANT_READ &&
499 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
500 {
501 return( ret );
502 }
503
504 *data_written += ret;
505 }
506
507 return( 0 );
508}
509```
510where ssl is the SSL context to use, data_to_write the address of the data
511buffer and data_to_write_len the number of data bytes. The handshake may
512not be completed, not even started for the SSL context ssl when the function is
513called and in that case the mbedtls_ssl_write() API takes care transparently of
514completing the handshake before to write and send data to the server. The
515mbedtls_ssl_write() may not been able to write and send all data in one go thus
516the need for a loop calling it as long as there are still data to write and
517send.
518
519An application function to write and send early data and only early data,
520data sent during the first flight of client messages while the handshake is in
521its initial phase, would look completely similar but the call to
522mbedtls_ssl_write_early_data() instead of mbedtls_ssl_write().
523```
524int write_early_data( mbedtls_ssl_context *ssl,
525 const unsigned char *data_to_write,
526 size_t data_to_write_len,
527 size_t *data_written )
528{
529 *data_written = 0;
530
531 while( *data_written < data_to_write_len )
532 {
533 ret = mbedtls_ssl_write_early_data( ssl, data_to_write + *data_written,
534 data_to_write_len - *data_written );
535
536 if( ret < 0 &&
537 ret != MBEDTLS_ERR_SSL_WANT_READ &&
538 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
539 {
540 return( ret );
541 }
542
543 *data_written += ret;
544 }
545
546 return( 0 );
547}
548```
549Note that compared to write_data(), write_early_data() can also return
550MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA and that should be handled
551specifically by the user of write_early_data(). A fresh SSL context (typically
552just after a call to mbedtls_ssl_setup() or mbedtls_ssl_session_reset()) would
553be expected when calling `write_early_data`.
554
555All together, code to write and send a buffer of data as long as possible as
556early data and then as standard post-handshake application data could
557plausibly look like:
558
559```
560ret = write_early_data( ssl, data_to_write, data_to_write_len,
561 &early_data_written );
562if( ret < 0 &&
563 ret != MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA )
564{
565 goto error;
566}
567
568ret = write_data( ssl, data_to_write + early_data_written,
569 data_to_write_len - early_data_written, &data_written );
570if( ret < 0 )
571 goto error;
572
573data_written += early_data_written;
574```
575
576Finally, taking into account that the server may reject early data, application
577code to write and send a buffer of data could plausibly look like:
578```
579ret = write_early_data( ssl, data_to_write, data_to_write_len,
580 &early_data_written );
581if( ret < 0 &&
582 ret != MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA )
583{
584 goto error;
585}
586
587/*
588 * Make sure the handshake is completed as it is a requisite to
589 * mbedtls_ssl_get_early_data_status().
590 */
591while( !mbedtls_ssl_is_handshake_over( ssl ) )
592{
593 ret = mbedtls_ssl_handshake( ssl );
594 if( ret < 0 &&
595 ret != MBEDTLS_ERR_SSL_WANT_READ &&
596 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
597 {
598 goto error;
599 }
600}
601
602ret = mbedtls_ssl_get_early_data_status( ssl );
603if( ret < 0 )
604 goto error;
605
606if( ret == MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED )
607 early_data_written = 0;
608
609ret = write_data( ssl, data_to_write + early_data_written,
610 data_to_write_len - early_data_written, &data_written );
611if( ret < 0 )
612 goto error;
613
614data_written += early_data_written;
615```
616
617Basically, the same holds for reading early data on the server side without the
618complication of possible rejection. An application function to read early data
619into a given buffer could plausibly look like:
620```
621int read_early_data( mbedtls_ssl_context *ssl,
622 unsigned char *buffer,
623 size_t buffer_size,
624 size_t *data_len )
625{
626 *data_len = 0;
627
628 while( *data_len < buffer_size )
629 {
630 ret = mbedtls_ssl_read_early_data( ssl, buffer + *data_len,
631 buffer_size - *data_len );
632
633 if( ret < 0 &&
634 ret != MBEDTLS_ERR_SSL_WANT_READ &&
635 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
636 {
637 return( ret );
638 }
639
640 *data_len += ret;
641 }
642
643 return( 0 );
644}
645```
646with again calls to read_early_data() expected to be done with a fresh SSL
647context.