2021-12-09 10:09:36 +01:00
|
|
|
TLS 1.3 support
|
|
|
|
===============
|
2020-05-31 08:39:50 +02:00
|
|
|
|
|
|
|
Overview
|
|
|
|
--------
|
|
|
|
|
2022-07-01 11:25:49 +02:00
|
|
|
Mbed TLS provides a partial implementation of the TLS 1.3 protocol defined in
|
|
|
|
the "Support description" section below. The TLS 1.3 support enablement
|
2021-12-09 10:09:36 +01:00
|
|
|
is controlled by the MBEDTLS_SSL_PROTO_TLS1_3 configuration option.
|
2020-05-31 08:39:50 +02:00
|
|
|
|
2021-12-09 10:09:36 +01:00
|
|
|
The development of the TLS 1.3 protocol is based on the TLS 1.3 prototype
|
|
|
|
located at https://github.com/hannestschofenig/mbedtls. The prototype is
|
|
|
|
itself based on a version of the development branch that we aim to keep as
|
|
|
|
recent as possible (ideally the head) by merging regularly commits of the
|
2021-12-09 14:53:59 +01:00
|
|
|
development branch into the prototype. The section "Prototype upstreaming
|
|
|
|
status" below describes what remains to be upstreamed.
|
2020-05-31 08:39:50 +02:00
|
|
|
|
2021-09-20 09:05:36 +02:00
|
|
|
|
2022-07-01 11:25:49 +02:00
|
|
|
Support description
|
|
|
|
-------------------
|
2021-09-20 09:05:36 +02:00
|
|
|
|
2021-09-27 15:36:29 +02:00
|
|
|
- Overview
|
|
|
|
|
2022-07-01 11:25:49 +02:00
|
|
|
- Mbed TLS implements both the client and the server side of the TLS 1.3
|
|
|
|
protocol.
|
2021-09-27 15:36:29 +02:00
|
|
|
|
2022-07-01 11:25:49 +02:00
|
|
|
- Mbed TLS supports ECDHE key establishment.
|
2021-09-27 15:36:29 +02:00
|
|
|
|
2022-07-01 11:25:49 +02:00
|
|
|
- Mbed TLS does not support DHE key establishment.
|
2021-09-27 15:36:29 +02:00
|
|
|
|
2022-10-03 12:02:17 +02:00
|
|
|
- Mbed TLS supports pre-shared keys for key establishment, pre-shared keys
|
|
|
|
provisioned externally as well as provisioned via the ticket mechanism.
|
|
|
|
|
|
|
|
- Mbed TLS supports session resumption via the ticket mechanism.
|
|
|
|
|
|
|
|
- Mbed TLS does not support sending or receiving early data (0-RTT data).
|
2021-09-27 15:36:29 +02:00
|
|
|
|
2021-09-20 09:05:36 +02:00
|
|
|
- Supported cipher suites: depends on the library configuration. Potentially
|
|
|
|
all of them:
|
|
|
|
TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256,
|
|
|
|
TLS_AES_128_CCM_SHA256 and TLS_AES_128_CCM_8_SHA256.
|
|
|
|
|
|
|
|
- Supported ClientHello extensions:
|
|
|
|
|
2022-07-01 14:36:52 +02:00
|
|
|
| Extension | Support |
|
|
|
|
| ---------------------------- | ------- |
|
|
|
|
| server_name | YES |
|
|
|
|
| max_fragment_length | no |
|
|
|
|
| status_request | no |
|
|
|
|
| supported_groups | YES |
|
|
|
|
| signature_algorithms | YES |
|
|
|
|
| use_srtp | no |
|
|
|
|
| heartbeat | no |
|
|
|
|
| apln | YES |
|
|
|
|
| signed_certificate_timestamp | no |
|
|
|
|
| client_certificate_type | no |
|
|
|
|
| server_certificate_type | no |
|
|
|
|
| padding | no |
|
|
|
|
| key_share | YES |
|
2022-10-03 12:02:17 +02:00
|
|
|
| pre_shared_key | YES |
|
|
|
|
| psk_key_exchange_modes | YES |
|
2022-07-01 14:36:52 +02:00
|
|
|
| early_data | no |
|
|
|
|
| cookie | no |
|
|
|
|
| supported_versions | YES |
|
|
|
|
| certificate_authorities | no |
|
|
|
|
| post_handshake_auth | no |
|
|
|
|
| signature_algorithms_cert | no |
|
|
|
|
|
2021-09-27 11:59:25 +02:00
|
|
|
|
2021-09-20 09:05:36 +02:00
|
|
|
- Supported groups: depends on the library configuration.
|
2022-07-01 11:25:49 +02:00
|
|
|
Potentially all ECDHE groups:
|
|
|
|
secp256r1, x25519, secp384r1, x448 and secp521r1.
|
2021-09-27 13:36:33 +02:00
|
|
|
|
|
|
|
Finite field groups (DHE) are not supported.
|
|
|
|
|
2021-09-28 15:49:39 +02:00
|
|
|
- Supported signature algorithms (both for certificates and CertificateVerify):
|
|
|
|
depends on the library configuration.
|
|
|
|
Potentially:
|
2022-07-01 11:25:49 +02:00
|
|
|
ecdsa_secp256r1_sha256, ecdsa_secp384r1_sha384, ecdsa_secp521r1_sha512,
|
|
|
|
rsa_pkcs1_sha256, rsa_pkcs1_sha384, rsa_pkcs1_sha512, rsa_pss_rsae_sha256,
|
|
|
|
rsa_pss_rsae_sha384 and rsa_pss_rsae_sha512.
|
2021-09-28 15:49:39 +02:00
|
|
|
|
|
|
|
Note that in absence of an application profile standard specifying otherwise
|
2022-07-01 11:25:49 +02:00
|
|
|
rsa_pkcs1_sha256, rsa_pss_rsae_sha256 and ecdsa_secp256r1_sha256 are
|
|
|
|
mandatory (see section 9.1 of the specification).
|
2021-09-27 13:36:33 +02:00
|
|
|
|
2022-01-25 07:36:30 +01:00
|
|
|
- Supported versions:
|
|
|
|
|
2022-07-01 11:25:49 +02:00
|
|
|
- TLS 1.2 and TLS 1.3 with version negotiation on the client side, not server
|
|
|
|
side.
|
2022-01-25 07:36:30 +01:00
|
|
|
|
2022-07-01 11:25:49 +02:00
|
|
|
- TLS 1.2 and TLS 1.3 can be enabled in the build independently of each
|
|
|
|
other.
|
2022-01-25 07:36:30 +01:00
|
|
|
|
2022-01-27 05:55:32 +01:00
|
|
|
- If both TLS 1.3 and TLS 1.2 are enabled at build time, only one of them can
|
2022-07-01 11:25:49 +02:00
|
|
|
be configured at runtime via `mbedtls_ssl_conf_{min,max}_tls_version` for a
|
|
|
|
server endpoint. Otherwise, `mbedtls_ssl_setup` will raise
|
|
|
|
`MBEDTLS_ERR_SSL_BAD_CONFIG` error.
|
2021-09-20 09:05:36 +02:00
|
|
|
|
2021-09-27 14:22:38 +02:00
|
|
|
- Compatibility with existing SSL/TLS build options:
|
2021-09-20 09:05:36 +02:00
|
|
|
|
2022-07-01 11:25:49 +02:00
|
|
|
The TLS 1.3 implementation is compatible with nearly all TLS 1.2
|
|
|
|
configuration options in the sense that when enabling TLS 1.3 in the library
|
|
|
|
there is rarely any need to modify the configuration from that used for
|
|
|
|
TLS 1.2. There are two exceptions though: the TLS 1.3 implementation requires
|
|
|
|
MBEDTLS_PSA_CRYPTO_C and MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, so these options
|
|
|
|
must be enabled.
|
2022-06-29 17:36:12 +02:00
|
|
|
|
2022-07-01 14:36:52 +02:00
|
|
|
Most of the Mbed TLS SSL/TLS related options are not supported or not
|
|
|
|
applicable to the TLS 1.3 implementation:
|
2021-09-20 09:05:36 +02:00
|
|
|
|
2021-09-27 11:59:25 +02:00
|
|
|
| Mbed TLS configuration option | Support |
|
|
|
|
| ---------------------------------------- | ------- |
|
|
|
|
| MBEDTLS_SSL_ALL_ALERT_MESSAGES | no |
|
|
|
|
| MBEDTLS_SSL_ASYNC_PRIVATE | no |
|
|
|
|
| MBEDTLS_SSL_CONTEXT_SERIALIZATION | no |
|
|
|
|
| MBEDTLS_SSL_DEBUG_ALL | no |
|
|
|
|
| MBEDTLS_SSL_ENCRYPT_THEN_MAC | n/a |
|
|
|
|
| MBEDTLS_SSL_EXTENDED_MASTER_SECRET | n/a |
|
2022-06-29 17:36:12 +02:00
|
|
|
| MBEDTLS_SSL_KEEP_PEER_CERTIFICATE | no (1) |
|
2021-09-27 11:59:25 +02:00
|
|
|
| MBEDTLS_SSL_RENEGOTIATION | n/a |
|
|
|
|
| MBEDTLS_SSL_MAX_FRAGMENT_LENGTH | no |
|
|
|
|
| | |
|
2022-10-03 12:02:17 +02:00
|
|
|
| MBEDTLS_SSL_SESSION_TICKETS | yes |
|
2022-07-01 11:25:49 +02:00
|
|
|
| MBEDTLS_SSL_SERVER_NAME_INDICATION | yes |
|
2021-09-27 11:59:25 +02:00
|
|
|
| MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH | no |
|
|
|
|
| | |
|
|
|
|
| MBEDTLS_ECP_RESTARTABLE | no |
|
|
|
|
| MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED | no |
|
|
|
|
| | |
|
2022-07-01 14:36:52 +02:00
|
|
|
| MBEDTLS_KEY_EXCHANGE_PSK_ENABLED | n/a (2) |
|
2021-09-27 11:59:25 +02:00
|
|
|
| MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED | n/a |
|
|
|
|
| MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED | n/a |
|
|
|
|
| MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED | n/a |
|
|
|
|
| MBEDTLS_KEY_EXCHANGE_RSA_ENABLED | n/a |
|
|
|
|
| MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED | n/a |
|
|
|
|
| MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED | n/a |
|
|
|
|
| MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED | n/a |
|
|
|
|
| MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED | n/a |
|
|
|
|
| MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED | n/a |
|
|
|
|
| MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED | n/a |
|
|
|
|
| | |
|
2022-06-30 10:48:40 +02:00
|
|
|
| MBEDTLS_PSA_CRYPTO_C | no (1) |
|
2022-07-01 11:25:49 +02:00
|
|
|
| MBEDTLS_USE_PSA_CRYPTO | yes |
|
2021-09-27 11:59:25 +02:00
|
|
|
|
2022-06-30 10:48:40 +02:00
|
|
|
(1) These options must remain in their default state of enabled.
|
2022-10-04 15:48:06 +02:00
|
|
|
(2) See the TLS 1.3 specific build options section below.
|
|
|
|
|
|
|
|
- TLS 1.3 specific build options:
|
|
|
|
|
|
|
|
- MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE enables the support for middlebox
|
|
|
|
compatibility mode as defined in section D.4 of RFC 8446.
|
|
|
|
|
2022-10-20 14:22:45 +02:00
|
|
|
- MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED enables the support for
|
|
|
|
the PSK key exchange mode as defined by RFC 8446. If it is the only key
|
|
|
|
exchange mode enabled, the TLS 1.3 implementation does not contain any code
|
|
|
|
related to key exchange protocols, certificates and signatures.
|
|
|
|
|
|
|
|
- MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED enables the
|
2022-10-21 08:51:33 +02:00
|
|
|
support for the ephemeral key exchange mode. If it is the only key exchange
|
2022-10-04 15:48:06 +02:00
|
|
|
mode enabled, the TLS 1.3 implementation does not contain any code related
|
|
|
|
to PSK based key exchange. The ephemeral key exchange mode requires at least
|
|
|
|
one of the key exchange protocol allowed by the TLS 1.3 specification, the
|
|
|
|
parsing and validation of x509 certificates and at least one signature
|
|
|
|
algorithm allowed by the TLS 1.3 specification for signature computing and
|
|
|
|
verification.
|
|
|
|
|
2022-10-20 14:22:45 +02:00
|
|
|
- MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED enables the
|
|
|
|
support for the PSK ephemeral key exchange mode. If it is the only key
|
2022-10-04 15:48:06 +02:00
|
|
|
exchange mode enabled, the TLS 1.3 implementation does not contain any code
|
|
|
|
related to certificates and signatures. The PSK ephemeral key exchange
|
2022-10-20 14:22:45 +02:00
|
|
|
mode requires at least one of the key exchange protocol allowed by the
|
2022-10-04 15:48:06 +02:00
|
|
|
TLS 1.3 specification.
|
2021-09-20 09:05:36 +02:00
|
|
|
|
2021-12-09 14:35:56 +01:00
|
|
|
|
2021-12-09 14:53:59 +01:00
|
|
|
Prototype upstreaming status
|
|
|
|
----------------------------
|
2021-12-09 14:35:56 +01:00
|
|
|
|
2022-07-01 14:36:52 +02:00
|
|
|
The following parts of the TLS 1.3 prototype remain to be upstreamed:
|
2021-12-09 14:35:56 +01:00
|
|
|
|
2022-10-03 12:02:17 +02:00
|
|
|
- Sending (client) and receiving (server) early data (0-RTT data).
|
2021-12-09 14:35:56 +01:00
|
|
|
|
|
|
|
- New TLS Message Processing Stack (MPS)
|
|
|
|
|
|
|
|
The TLS 1.3 prototype is developed alongside a rewrite of the TLS messaging layer,
|
|
|
|
encompassing low-level details such as record parsing, handshake reassembly, and
|
|
|
|
DTLS retransmission state machine.
|
|
|
|
|
|
|
|
MPS has the following components:
|
|
|
|
- Layer 1 (Datagram handling)
|
|
|
|
- Layer 2 (Record handling)
|
|
|
|
- Layer 3 (Message handling)
|
|
|
|
- Layer 4 (Retransmission State Machine)
|
|
|
|
- Reader (Abstracted pointer arithmetic and reassembly logic for incoming data)
|
|
|
|
- Writer (Abstracted pointer arithmetic and fragmentation logic for outgoing data)
|
|
|
|
|
|
|
|
Of those components, the following have been upstreamed
|
|
|
|
as part of `MBEDTLS_SSL_PROTO_TLS1_3`:
|
|
|
|
|
|
|
|
- Reader ([`library/mps_reader.h`](../../library/mps_reader.h))
|
|
|
|
|
|
|
|
|
2021-09-20 09:05:36 +02:00
|
|
|
Coding rules checklist for TLS 1.3
|
|
|
|
----------------------------------
|
|
|
|
|
|
|
|
The following coding rules are aimed to be a checklist for TLS 1.3 upstreaming
|
|
|
|
work to reduce review rounds and the number of comments in each round. They
|
|
|
|
come along (do NOT replace) the project coding rules
|
2022-10-12 17:47:08 +02:00
|
|
|
(https://mbed-tls.readthedocs.io/en/latest/kb/development/mbedtls-coding-standards). They have been
|
2021-09-20 09:05:36 +02:00
|
|
|
established and discussed following the review of #4882 that was the
|
|
|
|
PR upstreaming the first part of TLS 1.3 ClientHello writing code.
|
|
|
|
|
|
|
|
TLS 1.3 specific coding rules:
|
|
|
|
|
|
|
|
- TLS 1.3 specific C modules, headers, static functions names are prefixed
|
2021-09-27 13:56:46 +02:00
|
|
|
with `ssl_tls13_`. The same applies to structures and types that are
|
2021-09-20 09:05:36 +02:00
|
|
|
internal to C modules.
|
|
|
|
|
2021-09-27 13:56:46 +02:00
|
|
|
- TLS 1.3 specific exported functions, structures and types are
|
|
|
|
prefixed with `mbedtls_ssl_tls13_`.
|
|
|
|
|
|
|
|
- Use TLS1_3 in TLS 1.3 specific macros.
|
2021-09-20 09:05:36 +02:00
|
|
|
|
|
|
|
- The names of macros and variables related to a field or structure in the
|
|
|
|
TLS 1.3 specification should contain as far as possible the field name as
|
2021-09-27 13:54:28 +02:00
|
|
|
it is in the specification. If the field name is "too long" and we prefer
|
2021-09-20 09:05:36 +02:00
|
|
|
to introduce some kind of abbreviation of it, use the same abbreviation
|
|
|
|
everywhere in the code.
|
|
|
|
|
|
|
|
Example 1: #define CLIENT_HELLO_RANDOM_LEN 32, macro for the length of the
|
|
|
|
`random` field of the ClientHello message.
|
|
|
|
|
2021-10-18 13:56:53 +02:00
|
|
|
Example 2 (consistent abbreviation): `mbedtls_ssl_tls13_write_sig_alg_ext()`
|
2021-09-27 13:54:28 +02:00
|
|
|
and `MBEDTLS_TLS_EXT_SIG_ALG`, `sig_alg` standing for
|
2021-09-20 09:05:36 +02:00
|
|
|
`signature_algorithms`.
|
|
|
|
|
|
|
|
- Regarding vectors that are represented by a length followed by their value
|
|
|
|
in the data exchanged between servers and clients:
|
|
|
|
|
|
|
|
- Use `<vector name>_len` for the name of a variable used to compute the
|
|
|
|
length in bytes of the vector, where <vector name> is the name of the
|
|
|
|
vector as defined in the TLS 1.3 specification.
|
|
|
|
|
2021-09-27 13:58:21 +02:00
|
|
|
- Use `p_<vector_name>_len` for the name of a variable intended to hold
|
2021-09-20 09:05:36 +02:00
|
|
|
the address of the first byte of the vector length.
|
|
|
|
|
2021-09-27 13:58:21 +02:00
|
|
|
- Use `<vector_name>` for the name of a variable intended to hold the
|
2021-09-20 09:05:36 +02:00
|
|
|
address of the first byte of the vector value.
|
|
|
|
|
2021-09-27 13:58:21 +02:00
|
|
|
- Use `<vector_name>_end` for the name of a variable intended to hold
|
2021-09-20 09:05:36 +02:00
|
|
|
the address of the first byte past the vector value.
|
|
|
|
|
2021-09-27 13:58:21 +02:00
|
|
|
Those idioms should lower the risk of mis-using one of the address in place
|
|
|
|
of another one which could potentially lead to some nasty issues.
|
2021-09-20 09:05:36 +02:00
|
|
|
|
|
|
|
Example: `cipher_suites` vector of ClientHello in
|
2021-10-18 13:56:53 +02:00
|
|
|
`ssl_tls13_write_client_hello_cipher_suites()`
|
2021-09-27 13:54:28 +02:00
|
|
|
```
|
|
|
|
size_t cipher_suites_len;
|
2021-09-27 13:58:21 +02:00
|
|
|
unsigned char *p_cipher_suites_len;
|
|
|
|
unsigned char *cipher_suites;
|
2021-09-27 13:54:28 +02:00
|
|
|
```
|
2021-09-20 09:05:36 +02:00
|
|
|
|
2021-09-27 13:59:38 +02:00
|
|
|
- Where applicable, use:
|
|
|
|
- the macros to extract a byte from a multi-byte integer MBEDTLS_BYTE_{0-8}.
|
|
|
|
- the macros to write in memory in big-endian order a multi-byte integer
|
|
|
|
MBEDTLS_PUT_UINT{8|16|32|64}_BE.
|
|
|
|
- the macros to read from memory a multi-byte integer in big-endian order
|
|
|
|
MBEDTLS_GET_UINT{8|16|32|64}_BE.
|
|
|
|
- the macro to check for space when writing into an output buffer
|
|
|
|
`MBEDTLS_SSL_CHK_BUF_PTR`.
|
|
|
|
- the macro to check for data when reading from an input buffer
|
|
|
|
`MBEDTLS_SSL_CHK_BUF_READ_PTR`.
|
2021-09-20 09:05:36 +02:00
|
|
|
|
|
|
|
These macros were introduced after the prototype was written thus are
|
|
|
|
likely not to be used in prototype where we now would use them in
|
|
|
|
development.
|
|
|
|
|
2021-09-27 13:59:38 +02:00
|
|
|
The three first types, MBEDTLS_BYTE_{0-8}, MBEDTLS_PUT_UINT{8|16|32|64}_BE
|
|
|
|
and MBEDTLS_GET_UINT{8|16|32|64}_BE improve the readability of the code and
|
|
|
|
reduce the risk of writing or reading bytes in the wrong order.
|
2021-09-20 09:05:36 +02:00
|
|
|
|
2021-09-27 13:54:28 +02:00
|
|
|
The two last types, `MBEDTLS_SSL_CHK_BUF_PTR` and
|
|
|
|
`MBEDTLS_SSL_CHK_BUF_READ_PTR`, improve the readability of the code and
|
2021-09-20 09:05:36 +02:00
|
|
|
reduce the risk of error in the non-completely-trivial arithmetic to
|
|
|
|
check that we do not write or read past the end of a data buffer. The
|
|
|
|
usage of those macros combined with the following rule mitigate the risk
|
|
|
|
to read/write past the end of a data buffer.
|
|
|
|
|
2021-09-27 13:54:28 +02:00
|
|
|
Examples:
|
|
|
|
```
|
|
|
|
hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
|
|
|
|
MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
|
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
|
|
|
|
```
|
2021-09-20 09:05:36 +02:00
|
|
|
|
|
|
|
- To mitigate what happened here
|
2022-03-31 15:07:01 +02:00
|
|
|
(https://github.com/Mbed-TLS/mbedtls/pull/4882#discussion_r701704527) from
|
2021-09-20 09:05:36 +02:00
|
|
|
happening again, use always a local variable named `p` for the reading
|
|
|
|
pointer in functions parsing TLS 1.3 data, and for the writing pointer in
|
2021-09-27 14:22:38 +02:00
|
|
|
functions writing data into an output buffer and only that variable. The
|
|
|
|
name `p` has been chosen as it was already widely used in TLS code.
|
2021-09-20 09:05:36 +02:00
|
|
|
|
|
|
|
- When an TLS 1.3 structure is written or read by a function or as part of
|
|
|
|
a function, provide as documentation the definition of the structure as
|
|
|
|
it is in the TLS 1.3 specification.
|
|
|
|
|
|
|
|
General coding rules:
|
|
|
|
|
2021-09-27 13:54:28 +02:00
|
|
|
- We prefer grouping "related statement lines" by not adding blank lines
|
2021-09-20 09:05:36 +02:00
|
|
|
between them.
|
|
|
|
|
|
|
|
Example 1:
|
2021-09-27 13:54:28 +02:00
|
|
|
```
|
2021-09-20 09:05:36 +02:00
|
|
|
ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
buf += output_len;
|
2021-09-27 13:54:28 +02:00
|
|
|
```
|
2021-09-20 09:05:36 +02:00
|
|
|
|
|
|
|
Example 2:
|
2021-09-27 13:54:28 +02:00
|
|
|
```
|
2021-09-20 09:05:36 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR( cipher_suites_iter, end, 2 );
|
|
|
|
MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 );
|
|
|
|
cipher_suites_iter += 2;
|
2021-09-27 13:54:28 +02:00
|
|
|
```
|
2021-09-20 09:05:36 +02:00
|
|
|
|
|
|
|
- Use macros for constants that are used in different functions, different
|
|
|
|
places in the code. When a constant is used only locally in a function
|
|
|
|
(like the length in bytes of the vector lengths in functions reading and
|
|
|
|
writing TLS handshake message) there is no need to define a macro for it.
|
|
|
|
|
2021-09-27 13:54:28 +02:00
|
|
|
Example: `#define CLIENT_HELLO_RANDOM_LEN 32`
|
2021-09-20 09:05:36 +02:00
|
|
|
|
|
|
|
- When declaring a pointer the dereferencing operator should be prepended to
|
|
|
|
the pointer name not appended to the pointer type:
|
|
|
|
|
2021-09-27 13:54:28 +02:00
|
|
|
Example: `mbedtls_ssl_context *ssl;`
|
2021-09-20 09:05:36 +02:00
|
|
|
|
|
|
|
- Maximum line length is 80 characters.
|
|
|
|
|
|
|
|
Exceptions:
|
|
|
|
|
|
|
|
- string literals can extend beyond 80 characters as we do not want to
|
|
|
|
split them to ease their search in the code base.
|
|
|
|
|
|
|
|
- A line can be more than 80 characters by a few characters if just looking
|
|
|
|
at the 80 first characters is enough to fully understand the line. For
|
|
|
|
example it is generally fine if some closure characters like ";" or ")"
|
|
|
|
are beyond the 80 characters limit.
|
|
|
|
|
2021-09-27 14:24:43 +02:00
|
|
|
If a line becomes too long due to a refactoring (for example renaming a
|
|
|
|
function to a longer name, or indenting a block more), avoid rewrapping
|
|
|
|
lines in the same commit: it makes the review harder. Make one commit with
|
|
|
|
the longer lines and another commit with just the rewrapping.
|
|
|
|
|
2021-09-20 09:05:36 +02:00
|
|
|
- When in successive lines, functions and macros parameters should be aligned
|
|
|
|
vertically.
|
|
|
|
|
|
|
|
Example:
|
2021-09-27 13:54:28 +02:00
|
|
|
```
|
2022-03-10 18:56:50 +01:00
|
|
|
int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned hs_type,
|
|
|
|
unsigned char **buf,
|
|
|
|
size_t *buf_len );
|
2021-09-27 13:54:28 +02:00
|
|
|
```
|
2021-09-27 14:24:43 +02:00
|
|
|
|
|
|
|
- When a function's parameters span several lines, group related parameters
|
|
|
|
together if possible.
|
|
|
|
|
|
|
|
For example, prefer:
|
|
|
|
|
|
|
|
```
|
2022-03-10 18:56:50 +01:00
|
|
|
mbedtls_ssl_start_handshake_msg( ssl, hs_type,
|
|
|
|
buf, buf_len );
|
2021-09-27 14:24:43 +02:00
|
|
|
```
|
|
|
|
over
|
|
|
|
```
|
2022-03-10 18:56:50 +01:00
|
|
|
mbedtls_ssl_start_handshake_msg( ssl, hs_type, buf,
|
|
|
|
buf_len );
|
2021-09-27 14:24:43 +02:00
|
|
|
```
|
|
|
|
even if it fits.
|
2022-05-31 16:05:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
Overview of handshake code organization
|
|
|
|
---------------------------------------
|
|
|
|
|
|
|
|
The TLS 1.3 handshake protocol is implemented as a state machine. The
|
2022-06-24 13:45:04 +02:00
|
|
|
functions `mbedtls_ssl_tls13_handshake_{client,server}_step` are the top level
|
2022-05-31 16:05:13 +02:00
|
|
|
functions of that implementation. They are implemented as a switch over all the
|
|
|
|
possible states of the state machine.
|
|
|
|
|
|
|
|
Most of the states are either dedicated to the processing or writing of an
|
|
|
|
handshake message.
|
|
|
|
|
|
|
|
The implementation does not go systematically through all states as this would
|
|
|
|
result in too many checks of whether something needs to be done or not in a
|
|
|
|
given state to be duplicated across several state handlers. For example, on
|
|
|
|
client side, the states related to certificate parsing and validation are
|
|
|
|
bypassed if the handshake is based on a pre-shared key and thus does not
|
|
|
|
involve certificates.
|
|
|
|
|
|
|
|
On the contrary, the implementation goes systematically though some states
|
|
|
|
even if they could be bypassed if it helps in minimizing when and where inbound
|
|
|
|
and outbound keys are updated. The `MBEDTLS_SSL_CLIENT_CERTIFICATE` state on
|
|
|
|
client side is a example of that.
|
|
|
|
|
|
|
|
The names of the handlers processing/writing an handshake message are
|
2022-06-24 13:45:04 +02:00
|
|
|
prefixed with `(mbedtls_)ssl_tls13_{process,write}`. To ease the maintenance and
|
2022-05-31 16:05:13 +02:00
|
|
|
reduce the risk of bugs, the code of the message processing and writing
|
|
|
|
handlers is split into a sequence of stages.
|
|
|
|
|
|
|
|
The sending of data to the peer only occurs in `mbedtls_ssl_handshake_step`
|
|
|
|
between the calls to the handlers and as a consequence handlers do not have to
|
|
|
|
care about the MBEDTLS_ERR_SSL_WANT_WRITE error code. Furthermore, all pending
|
|
|
|
data are flushed before to call the next handler. That way, handlers do not
|
|
|
|
have to worry about pending data when changing outbound keys.
|
|
|
|
|
|
|
|
### Message processing handlers
|
|
|
|
For message processing handlers, the stages are:
|
|
|
|
|
|
|
|
* coordination stage: check if the state should be bypassed. This stage is
|
|
|
|
optional. The check is either purely based on the reading of the value of some
|
|
|
|
fields of the SSL context or based on the reading of the type of the next
|
|
|
|
message. The latter occurs when it is not known what the next handshake message
|
|
|
|
will be, an example of that on client side being if we are going to receive a
|
|
|
|
CertificateRequest message or not. The intent is, apart from the next record
|
|
|
|
reading to not modify the SSL context as this stage may be repeated if the
|
|
|
|
next handshake message has not been received yet.
|
|
|
|
|
|
|
|
* fetching stage: at this stage we are sure of the type of the handshake
|
|
|
|
message we must receive next and we try to fetch it. If we did not go through
|
|
|
|
a coordination stage involving the next record type reading, the next
|
|
|
|
handshake message may not have been received yet, the handler returns with
|
|
|
|
`MBEDTLS_ERR_SSL_WANT_READ` without changing the current state and it will be
|
|
|
|
called again later.
|
|
|
|
|
|
|
|
* pre-processing stage: prepare the SSL context for the message parsing. This
|
|
|
|
stage is optional. Any processing that must be done before the parsing of the
|
|
|
|
message or that can be done to simplify the parsing code. Some simple and
|
|
|
|
partial parsing of the handshake message may append at that stage like in the
|
|
|
|
ServerHello message pre-processing.
|
|
|
|
|
|
|
|
* parsing stage: parse the message and restrict as much as possible any
|
|
|
|
update of the SSL context. The idea of the pre-processing/parsing/post-processing
|
|
|
|
organization is to concentrate solely on the parsing in the parsing function to
|
|
|
|
reduce the size of its code and to simplify it.
|
|
|
|
|
|
|
|
* post-processing stage: following the parsing, further update of the SSL
|
2022-06-14 18:45:44 +02:00
|
|
|
context to prepare for the next incoming and outgoing messages. This stage is
|
2022-05-31 16:05:13 +02:00
|
|
|
optional. For example, secret and key computations occur at this stage, as well
|
|
|
|
as handshake messages checksum update.
|
|
|
|
|
|
|
|
* state change: the state change is done in the main state handler to ease the
|
|
|
|
navigation of the state machine transitions.
|
|
|
|
|
|
|
|
|
|
|
|
### Message writing handlers
|
|
|
|
For message writing handlers, the stages are:
|
|
|
|
|
|
|
|
* coordination stage: check if the state should be bypassed. This stage is
|
|
|
|
optional. The check is based on the value of some fields of the SSL context.
|
|
|
|
|
|
|
|
* preparation stage: prepare for the message writing. This stage is optional.
|
|
|
|
Any processing that must be done before the writing of the message or that can
|
|
|
|
be done to simplify the writing code.
|
|
|
|
|
|
|
|
* writing stage: write the message and restrict as much as possible any update
|
|
|
|
of the SSL context. The idea of the preparation/writing/finalization
|
|
|
|
organization is to concentrate solely on the writing in the writing function to
|
|
|
|
reduce the size of its code and simplify it.
|
|
|
|
|
|
|
|
* finalization stage: following the writing, further update of the SSL
|
|
|
|
context to prepare for the next incoming and outgoing messages. This stage is
|
|
|
|
optional. For example, handshake secret and key computation occur at that
|
|
|
|
stage (ServerHello writing finalization), switching to handshake keys for
|
|
|
|
outbound message on server side as well.
|
|
|
|
|
|
|
|
* state change: the state change is done in the main state handler to ease
|
|
|
|
the navigation of the state machine transitions.
|