Ronald Cron | 43ffc9d | 2021-12-09 10:09:36 +0100 | [diff] [blame] | 1 | TLS 1.3 support |
| 2 | =============== |
Hanno Becker | 9338f9f | 2020-05-31 07:39:50 +0100 | [diff] [blame] | 3 | |
| 4 | Overview |
| 5 | -------- |
| 6 | |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 7 | Mbed TLS provides a partial implementation of the TLS 1.3 protocol defined in |
| 8 | the "Support description" section below. The TLS 1.3 support enablement |
Ronald Cron | 43ffc9d | 2021-12-09 10:09:36 +0100 | [diff] [blame] | 9 | is controlled by the MBEDTLS_SSL_PROTO_TLS1_3 configuration option. |
Hanno Becker | 9338f9f | 2020-05-31 07:39:50 +0100 | [diff] [blame] | 10 | |
Ronald Cron | 43ffc9d | 2021-12-09 10:09:36 +0100 | [diff] [blame] | 11 | The development of the TLS 1.3 protocol is based on the TLS 1.3 prototype |
| 12 | located at https://github.com/hannestschofenig/mbedtls. The prototype is |
| 13 | itself based on a version of the development branch that we aim to keep as |
| 14 | recent as possible (ideally the head) by merging regularly commits of the |
Ronald Cron | 7aa6fc1 | 2021-12-09 14:53:59 +0100 | [diff] [blame] | 15 | development branch into the prototype. The section "Prototype upstreaming |
| 16 | status" below describes what remains to be upstreamed. |
Hanno Becker | 9338f9f | 2020-05-31 07:39:50 +0100 | [diff] [blame] | 17 | |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 18 | |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 19 | Support description |
| 20 | ------------------- |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 21 | |
Ronald Cron | f164b6a | 2021-09-27 15:36:29 +0200 | [diff] [blame] | 22 | - Overview |
| 23 | |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 24 | - Mbed TLS implements both the client and the server side of the TLS 1.3 |
| 25 | protocol. |
Ronald Cron | f164b6a | 2021-09-27 15:36:29 +0200 | [diff] [blame] | 26 | |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 27 | - Mbed TLS supports ECDHE key establishment. |
Ronald Cron | f164b6a | 2021-09-27 15:36:29 +0200 | [diff] [blame] | 28 | |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 29 | - Mbed TLS does not support DHE key establishment. |
Ronald Cron | f164b6a | 2021-09-27 15:36:29 +0200 | [diff] [blame] | 30 | |
Ronald Cron | 93dcb1b | 2022-10-03 12:02:17 +0200 | [diff] [blame] | 31 | - 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 Cron | f164b6a | 2021-09-27 15:36:29 +0200 | [diff] [blame] | 37 | |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 38 | - 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 Cron | 3cb707d | 2022-07-01 14:36:52 +0200 | [diff] [blame] | 45 | | 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 | |
| 54 | | apln | YES | |
| 55 | | signed_certificate_timestamp | no | |
| 56 | | client_certificate_type | no | |
| 57 | | server_certificate_type | no | |
| 58 | | padding | no | |
| 59 | | key_share | YES | |
Ronald Cron | 93dcb1b | 2022-10-03 12:02:17 +0200 | [diff] [blame] | 60 | | pre_shared_key | YES | |
| 61 | | psk_key_exchange_modes | YES | |
Ronald Cron | 3cb707d | 2022-07-01 14:36:52 +0200 | [diff] [blame] | 62 | | 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 Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 68 | |
Ronald Cron | 023987f | 2021-09-27 11:59:25 +0200 | [diff] [blame] | 69 | |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 70 | - Supported groups: depends on the library configuration. |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 71 | Potentially all ECDHE groups: |
| 72 | secp256r1, x25519, secp384r1, x448 and secp521r1. |
Ronald Cron | c3b510f | 2021-09-27 13:36:33 +0200 | [diff] [blame] | 73 | |
| 74 | Finite field groups (DHE) are not supported. |
| 75 | |
Ronald Cron | fb87721 | 2021-09-28 15:49:39 +0200 | [diff] [blame] | 76 | - Supported signature algorithms (both for certificates and CertificateVerify): |
| 77 | depends on the library configuration. |
| 78 | Potentially: |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 79 | ecdsa_secp256r1_sha256, ecdsa_secp384r1_sha384, ecdsa_secp521r1_sha512, |
| 80 | rsa_pkcs1_sha256, rsa_pkcs1_sha384, rsa_pkcs1_sha512, rsa_pss_rsae_sha256, |
| 81 | rsa_pss_rsae_sha384 and rsa_pss_rsae_sha512. |
Ronald Cron | c3b510f | 2021-09-27 13:36:33 +0200 | [diff] [blame] | 82 | |
Ronald Cron | fb87721 | 2021-09-28 15:49:39 +0200 | [diff] [blame] | 83 | Note that in absence of an application profile standard specifying otherwise |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 84 | rsa_pkcs1_sha256, rsa_pss_rsae_sha256 and ecdsa_secp256r1_sha256 are |
| 85 | mandatory (see section 9.1 of the specification). |
Ronald Cron | c3b510f | 2021-09-27 13:36:33 +0200 | [diff] [blame] | 86 | |
Jerry Yu | 72a0565 | 2022-01-25 14:36:30 +0800 | [diff] [blame] | 87 | - Supported versions: |
| 88 | |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 89 | - TLS 1.2 and TLS 1.3 with version negotiation on the client side, not server |
| 90 | side. |
Jerry Yu | 72a0565 | 2022-01-25 14:36:30 +0800 | [diff] [blame] | 91 | |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 92 | - TLS 1.2 and TLS 1.3 can be enabled in the build independently of each |
| 93 | other. |
Jerry Yu | 72a0565 | 2022-01-25 14:36:30 +0800 | [diff] [blame] | 94 | |
Jerry Yu | adb1869 | 2022-01-27 12:55:32 +0800 | [diff] [blame] | 95 | - If both TLS 1.3 and TLS 1.2 are enabled at build time, only one of them can |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 96 | be configured at runtime via `mbedtls_ssl_conf_{min,max}_tls_version` for a |
| 97 | server endpoint. Otherwise, `mbedtls_ssl_setup` will raise |
| 98 | `MBEDTLS_ERR_SSL_BAD_CONFIG` error. |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 99 | |
Ronald Cron | 3e7c403 | 2021-09-27 14:22:38 +0200 | [diff] [blame] | 100 | - Compatibility with existing SSL/TLS build options: |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 101 | |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 102 | The TLS 1.3 implementation is compatible with nearly all TLS 1.2 |
| 103 | configuration options in the sense that when enabling TLS 1.3 in the library |
| 104 | there is rarely any need to modify the configuration from that used for |
| 105 | TLS 1.2. There are two exceptions though: the TLS 1.3 implementation requires |
| 106 | MBEDTLS_PSA_CRYPTO_C and MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, so these options |
| 107 | must be enabled. |
Tom Cosgrove | afb2fe1 | 2022-06-29 16:36:12 +0100 | [diff] [blame] | 108 | |
Ronald Cron | 3cb707d | 2022-07-01 14:36:52 +0200 | [diff] [blame] | 109 | Most of the Mbed TLS SSL/TLS related options are not supported or not |
| 110 | applicable to the TLS 1.3 implementation: |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 111 | |
Ronald Cron | 023987f | 2021-09-27 11:59:25 +0200 | [diff] [blame] | 112 | | Mbed TLS configuration option | Support | |
| 113 | | ---------------------------------------- | ------- | |
| 114 | | MBEDTLS_SSL_ALL_ALERT_MESSAGES | no | |
| 115 | | MBEDTLS_SSL_ASYNC_PRIVATE | no | |
| 116 | | MBEDTLS_SSL_CONTEXT_SERIALIZATION | no | |
| 117 | | MBEDTLS_SSL_DEBUG_ALL | no | |
| 118 | | MBEDTLS_SSL_ENCRYPT_THEN_MAC | n/a | |
| 119 | | MBEDTLS_SSL_EXTENDED_MASTER_SECRET | n/a | |
Tom Cosgrove | afb2fe1 | 2022-06-29 16:36:12 +0100 | [diff] [blame] | 120 | | MBEDTLS_SSL_KEEP_PEER_CERTIFICATE | no (1) | |
Ronald Cron | 023987f | 2021-09-27 11:59:25 +0200 | [diff] [blame] | 121 | | MBEDTLS_SSL_RENEGOTIATION | n/a | |
| 122 | | MBEDTLS_SSL_MAX_FRAGMENT_LENGTH | no | |
| 123 | | | | |
Ronald Cron | 93dcb1b | 2022-10-03 12:02:17 +0200 | [diff] [blame] | 124 | | MBEDTLS_SSL_SESSION_TICKETS | yes | |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 125 | | MBEDTLS_SSL_SERVER_NAME_INDICATION | yes | |
Ronald Cron | 023987f | 2021-09-27 11:59:25 +0200 | [diff] [blame] | 126 | | MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH | no | |
| 127 | | | | |
| 128 | | MBEDTLS_ECP_RESTARTABLE | no | |
| 129 | | MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED | no | |
| 130 | | | | |
Ronald Cron | 3cb707d | 2022-07-01 14:36:52 +0200 | [diff] [blame] | 131 | | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED | n/a (2) | |
Ronald Cron | 023987f | 2021-09-27 11:59:25 +0200 | [diff] [blame] | 132 | | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED | n/a | |
| 133 | | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED | n/a | |
| 134 | | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED | n/a | |
| 135 | | MBEDTLS_KEY_EXCHANGE_RSA_ENABLED | n/a | |
| 136 | | MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED | n/a | |
| 137 | | MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED | n/a | |
| 138 | | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED | n/a | |
| 139 | | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED | n/a | |
| 140 | | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED | n/a | |
| 141 | | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED | n/a | |
| 142 | | | | |
Tom Cosgrove | d7adb3c | 2022-06-30 09:48:40 +0100 | [diff] [blame] | 143 | | MBEDTLS_PSA_CRYPTO_C | no (1) | |
Ronald Cron | 2ba0d23 | 2022-07-01 11:25:49 +0200 | [diff] [blame] | 144 | | MBEDTLS_USE_PSA_CRYPTO | yes | |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 145 | |
Tom Cosgrove | d7adb3c | 2022-06-30 09:48:40 +0100 | [diff] [blame] | 146 | (1) These options must remain in their default state of enabled. |
Ronald Cron | d8d2ea5 | 2022-10-04 15:48:06 +0200 | [diff] [blame] | 147 | (2) See the TLS 1.3 specific build options section below. |
| 148 | |
| 149 | - TLS 1.3 specific build options: |
| 150 | |
| 151 | - MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE enables the support for middlebox |
| 152 | compatibility mode as defined in section D.4 of RFC 8446. |
| 153 | |
Ronald Cron | 9810b6d | 2022-10-20 14:22:45 +0200 | [diff] [blame] | 154 | - MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED enables the support for |
| 155 | the PSK key exchange mode as defined by RFC 8446. If it is the only key |
| 156 | exchange mode enabled, the TLS 1.3 implementation does not contain any code |
| 157 | related to key exchange protocols, certificates and signatures. |
| 158 | |
| 159 | - MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED enables the |
Ronald Cron | 10bf956 | 2022-10-21 08:51:33 +0200 | [diff] [blame] | 160 | support for the ephemeral key exchange mode. If it is the only key exchange |
Ronald Cron | d8d2ea5 | 2022-10-04 15:48:06 +0200 | [diff] [blame] | 161 | mode enabled, the TLS 1.3 implementation does not contain any code related |
| 162 | to PSK based key exchange. The ephemeral key exchange mode requires at least |
| 163 | one of the key exchange protocol allowed by the TLS 1.3 specification, the |
| 164 | parsing and validation of x509 certificates and at least one signature |
| 165 | algorithm allowed by the TLS 1.3 specification for signature computing and |
| 166 | verification. |
| 167 | |
Ronald Cron | 9810b6d | 2022-10-20 14:22:45 +0200 | [diff] [blame] | 168 | - MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED enables the |
| 169 | support for the PSK ephemeral key exchange mode. If it is the only key |
Ronald Cron | d8d2ea5 | 2022-10-04 15:48:06 +0200 | [diff] [blame] | 170 | exchange mode enabled, the TLS 1.3 implementation does not contain any code |
Ronald Cron | d8d2ea5 | 2022-10-04 15:48:06 +0200 | [diff] [blame] | 171 | related to certificates and signatures. The PSK ephemeral key exchange |
Ronald Cron | 9810b6d | 2022-10-20 14:22:45 +0200 | [diff] [blame] | 172 | mode requires at least one of the key exchange protocol allowed by the |
Ronald Cron | d8d2ea5 | 2022-10-04 15:48:06 +0200 | [diff] [blame] | 173 | TLS 1.3 specification. |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 174 | |
Ronald Cron | 653d5bc | 2021-12-09 14:35:56 +0100 | [diff] [blame] | 175 | |
Ronald Cron | 7aa6fc1 | 2021-12-09 14:53:59 +0100 | [diff] [blame] | 176 | Prototype upstreaming status |
| 177 | ---------------------------- |
Ronald Cron | 653d5bc | 2021-12-09 14:35:56 +0100 | [diff] [blame] | 178 | |
Ronald Cron | 3cb707d | 2022-07-01 14:36:52 +0200 | [diff] [blame] | 179 | The following parts of the TLS 1.3 prototype remain to be upstreamed: |
Ronald Cron | 653d5bc | 2021-12-09 14:35:56 +0100 | [diff] [blame] | 180 | |
Ronald Cron | 93dcb1b | 2022-10-03 12:02:17 +0200 | [diff] [blame] | 181 | - Sending (client) and receiving (server) early data (0-RTT data). |
Ronald Cron | 653d5bc | 2021-12-09 14:35:56 +0100 | [diff] [blame] | 182 | |
| 183 | - New TLS Message Processing Stack (MPS) |
| 184 | |
| 185 | The TLS 1.3 prototype is developed alongside a rewrite of the TLS messaging layer, |
| 186 | encompassing low-level details such as record parsing, handshake reassembly, and |
| 187 | DTLS retransmission state machine. |
| 188 | |
| 189 | MPS has the following components: |
| 190 | - Layer 1 (Datagram handling) |
| 191 | - Layer 2 (Record handling) |
| 192 | - Layer 3 (Message handling) |
| 193 | - Layer 4 (Retransmission State Machine) |
| 194 | - Reader (Abstracted pointer arithmetic and reassembly logic for incoming data) |
| 195 | - Writer (Abstracted pointer arithmetic and fragmentation logic for outgoing data) |
| 196 | |
| 197 | Of those components, the following have been upstreamed |
| 198 | as part of `MBEDTLS_SSL_PROTO_TLS1_3`: |
| 199 | |
| 200 | - Reader ([`library/mps_reader.h`](../../library/mps_reader.h)) |
| 201 | |
| 202 | |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 203 | Coding rules checklist for TLS 1.3 |
| 204 | ---------------------------------- |
| 205 | |
| 206 | The following coding rules are aimed to be a checklist for TLS 1.3 upstreaming |
| 207 | work to reduce review rounds and the number of comments in each round. They |
| 208 | come along (do NOT replace) the project coding rules |
Dave Rodgman | b319684 | 2022-10-12 16:47:08 +0100 | [diff] [blame] | 209 | (https://mbed-tls.readthedocs.io/en/latest/kb/development/mbedtls-coding-standards). They have been |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 210 | established and discussed following the review of #4882 that was the |
| 211 | PR upstreaming the first part of TLS 1.3 ClientHello writing code. |
| 212 | |
| 213 | TLS 1.3 specific coding rules: |
| 214 | |
| 215 | - TLS 1.3 specific C modules, headers, static functions names are prefixed |
Ronald Cron | b194466 | 2021-09-27 13:56:46 +0200 | [diff] [blame] | 216 | with `ssl_tls13_`. The same applies to structures and types that are |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 217 | internal to C modules. |
| 218 | |
Ronald Cron | b194466 | 2021-09-27 13:56:46 +0200 | [diff] [blame] | 219 | - TLS 1.3 specific exported functions, structures and types are |
| 220 | prefixed with `mbedtls_ssl_tls13_`. |
| 221 | |
| 222 | - Use TLS1_3 in TLS 1.3 specific macros. |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 223 | |
| 224 | - The names of macros and variables related to a field or structure in the |
| 225 | TLS 1.3 specification should contain as far as possible the field name as |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 226 | it is in the specification. If the field name is "too long" and we prefer |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 227 | to introduce some kind of abbreviation of it, use the same abbreviation |
| 228 | everywhere in the code. |
| 229 | |
| 230 | Example 1: #define CLIENT_HELLO_RANDOM_LEN 32, macro for the length of the |
| 231 | `random` field of the ClientHello message. |
| 232 | |
Dave Rodgman | c8aaac8 | 2021-10-18 12:56:53 +0100 | [diff] [blame] | 233 | Example 2 (consistent abbreviation): `mbedtls_ssl_tls13_write_sig_alg_ext()` |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 234 | and `MBEDTLS_TLS_EXT_SIG_ALG`, `sig_alg` standing for |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 235 | `signature_algorithms`. |
| 236 | |
| 237 | - Regarding vectors that are represented by a length followed by their value |
| 238 | in the data exchanged between servers and clients: |
| 239 | |
| 240 | - Use `<vector name>_len` for the name of a variable used to compute the |
| 241 | length in bytes of the vector, where <vector name> is the name of the |
| 242 | vector as defined in the TLS 1.3 specification. |
| 243 | |
Ronald Cron | 99733f0 | 2021-09-27 13:58:21 +0200 | [diff] [blame] | 244 | - Use `p_<vector_name>_len` for the name of a variable intended to hold |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 245 | the address of the first byte of the vector length. |
| 246 | |
Ronald Cron | 99733f0 | 2021-09-27 13:58:21 +0200 | [diff] [blame] | 247 | - Use `<vector_name>` for the name of a variable intended to hold the |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 248 | address of the first byte of the vector value. |
| 249 | |
Ronald Cron | 99733f0 | 2021-09-27 13:58:21 +0200 | [diff] [blame] | 250 | - Use `<vector_name>_end` for the name of a variable intended to hold |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 251 | the address of the first byte past the vector value. |
| 252 | |
Ronald Cron | 99733f0 | 2021-09-27 13:58:21 +0200 | [diff] [blame] | 253 | Those idioms should lower the risk of mis-using one of the address in place |
| 254 | of another one which could potentially lead to some nasty issues. |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 255 | |
| 256 | Example: `cipher_suites` vector of ClientHello in |
Dave Rodgman | c8aaac8 | 2021-10-18 12:56:53 +0100 | [diff] [blame] | 257 | `ssl_tls13_write_client_hello_cipher_suites()` |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 258 | ``` |
| 259 | size_t cipher_suites_len; |
Ronald Cron | 99733f0 | 2021-09-27 13:58:21 +0200 | [diff] [blame] | 260 | unsigned char *p_cipher_suites_len; |
| 261 | unsigned char *cipher_suites; |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 262 | ``` |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 263 | |
Ronald Cron | fecda8d | 2021-09-27 13:59:38 +0200 | [diff] [blame] | 264 | - Where applicable, use: |
| 265 | - the macros to extract a byte from a multi-byte integer MBEDTLS_BYTE_{0-8}. |
| 266 | - the macros to write in memory in big-endian order a multi-byte integer |
| 267 | MBEDTLS_PUT_UINT{8|16|32|64}_BE. |
| 268 | - the macros to read from memory a multi-byte integer in big-endian order |
| 269 | MBEDTLS_GET_UINT{8|16|32|64}_BE. |
| 270 | - the macro to check for space when writing into an output buffer |
| 271 | `MBEDTLS_SSL_CHK_BUF_PTR`. |
| 272 | - the macro to check for data when reading from an input buffer |
| 273 | `MBEDTLS_SSL_CHK_BUF_READ_PTR`. |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 274 | |
| 275 | These macros were introduced after the prototype was written thus are |
| 276 | likely not to be used in prototype where we now would use them in |
| 277 | development. |
| 278 | |
Ronald Cron | fecda8d | 2021-09-27 13:59:38 +0200 | [diff] [blame] | 279 | The three first types, MBEDTLS_BYTE_{0-8}, MBEDTLS_PUT_UINT{8|16|32|64}_BE |
| 280 | and MBEDTLS_GET_UINT{8|16|32|64}_BE improve the readability of the code and |
| 281 | reduce the risk of writing or reading bytes in the wrong order. |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 282 | |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 283 | The two last types, `MBEDTLS_SSL_CHK_BUF_PTR` and |
| 284 | `MBEDTLS_SSL_CHK_BUF_READ_PTR`, improve the readability of the code and |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 285 | reduce the risk of error in the non-completely-trivial arithmetic to |
| 286 | check that we do not write or read past the end of a data buffer. The |
| 287 | usage of those macros combined with the following rule mitigate the risk |
| 288 | to read/write past the end of a data buffer. |
| 289 | |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 290 | Examples: |
| 291 | ``` |
| 292 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); |
| 293 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 ); |
| 294 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 ); |
| 295 | ``` |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 296 | |
| 297 | - To mitigate what happened here |
Dave Rodgman | 017a199 | 2022-03-31 14:07:01 +0100 | [diff] [blame] | 298 | (https://github.com/Mbed-TLS/mbedtls/pull/4882#discussion_r701704527) from |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 299 | happening again, use always a local variable named `p` for the reading |
| 300 | pointer in functions parsing TLS 1.3 data, and for the writing pointer in |
Ronald Cron | 3e7c403 | 2021-09-27 14:22:38 +0200 | [diff] [blame] | 301 | functions writing data into an output buffer and only that variable. The |
| 302 | name `p` has been chosen as it was already widely used in TLS code. |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 303 | |
| 304 | - When an TLS 1.3 structure is written or read by a function or as part of |
| 305 | a function, provide as documentation the definition of the structure as |
| 306 | it is in the TLS 1.3 specification. |
| 307 | |
| 308 | General coding rules: |
| 309 | |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 310 | - We prefer grouping "related statement lines" by not adding blank lines |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 311 | between them. |
| 312 | |
| 313 | Example 1: |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 314 | ``` |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 315 | ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len ); |
| 316 | if( ret != 0 ) |
| 317 | return( ret ); |
| 318 | buf += output_len; |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 319 | ``` |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 320 | |
| 321 | Example 2: |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 322 | ``` |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 323 | MBEDTLS_SSL_CHK_BUF_PTR( cipher_suites_iter, end, 2 ); |
| 324 | MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 ); |
| 325 | cipher_suites_iter += 2; |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 326 | ``` |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 327 | |
| 328 | - Use macros for constants that are used in different functions, different |
| 329 | places in the code. When a constant is used only locally in a function |
| 330 | (like the length in bytes of the vector lengths in functions reading and |
| 331 | writing TLS handshake message) there is no need to define a macro for it. |
| 332 | |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 333 | Example: `#define CLIENT_HELLO_RANDOM_LEN 32` |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 334 | |
| 335 | - When declaring a pointer the dereferencing operator should be prepended to |
| 336 | the pointer name not appended to the pointer type: |
| 337 | |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 338 | Example: `mbedtls_ssl_context *ssl;` |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 339 | |
| 340 | - Maximum line length is 80 characters. |
| 341 | |
| 342 | Exceptions: |
| 343 | |
| 344 | - string literals can extend beyond 80 characters as we do not want to |
| 345 | split them to ease their search in the code base. |
| 346 | |
| 347 | - A line can be more than 80 characters by a few characters if just looking |
| 348 | at the 80 first characters is enough to fully understand the line. For |
| 349 | example it is generally fine if some closure characters like ";" or ")" |
| 350 | are beyond the 80 characters limit. |
| 351 | |
Ronald Cron | 847c358 | 2021-09-27 14:24:43 +0200 | [diff] [blame] | 352 | If a line becomes too long due to a refactoring (for example renaming a |
| 353 | function to a longer name, or indenting a block more), avoid rewrapping |
| 354 | lines in the same commit: it makes the review harder. Make one commit with |
| 355 | the longer lines and another commit with just the rewrapping. |
| 356 | |
Ronald Cron | 3785c90 | 2021-09-20 09:05:36 +0200 | [diff] [blame] | 357 | - When in successive lines, functions and macros parameters should be aligned |
| 358 | vertically. |
| 359 | |
| 360 | Example: |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 361 | ``` |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 362 | int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl, |
| 363 | unsigned hs_type, |
| 364 | unsigned char **buf, |
| 365 | size_t *buf_len ); |
Ronald Cron | 72064b3 | 2021-09-27 13:54:28 +0200 | [diff] [blame] | 366 | ``` |
Ronald Cron | 847c358 | 2021-09-27 14:24:43 +0200 | [diff] [blame] | 367 | |
| 368 | - When a function's parameters span several lines, group related parameters |
| 369 | together if possible. |
| 370 | |
| 371 | For example, prefer: |
| 372 | |
| 373 | ``` |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 374 | mbedtls_ssl_start_handshake_msg( ssl, hs_type, |
| 375 | buf, buf_len ); |
Ronald Cron | 847c358 | 2021-09-27 14:24:43 +0200 | [diff] [blame] | 376 | ``` |
| 377 | over |
| 378 | ``` |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 379 | mbedtls_ssl_start_handshake_msg( ssl, hs_type, buf, |
| 380 | buf_len ); |
Ronald Cron | 847c358 | 2021-09-27 14:24:43 +0200 | [diff] [blame] | 381 | ``` |
| 382 | even if it fits. |
Ronald Cron | 44b23b1 | 2022-05-31 16:05:13 +0200 | [diff] [blame] | 383 | |
| 384 | |
| 385 | Overview of handshake code organization |
| 386 | --------------------------------------- |
| 387 | |
| 388 | The TLS 1.3 handshake protocol is implemented as a state machine. The |
Ronald Cron | 6b14c69 | 2022-06-24 13:45:04 +0200 | [diff] [blame] | 389 | functions `mbedtls_ssl_tls13_handshake_{client,server}_step` are the top level |
Ronald Cron | 44b23b1 | 2022-05-31 16:05:13 +0200 | [diff] [blame] | 390 | functions of that implementation. They are implemented as a switch over all the |
| 391 | possible states of the state machine. |
| 392 | |
| 393 | Most of the states are either dedicated to the processing or writing of an |
| 394 | handshake message. |
| 395 | |
| 396 | The implementation does not go systematically through all states as this would |
| 397 | result in too many checks of whether something needs to be done or not in a |
| 398 | given state to be duplicated across several state handlers. For example, on |
| 399 | client side, the states related to certificate parsing and validation are |
| 400 | bypassed if the handshake is based on a pre-shared key and thus does not |
| 401 | involve certificates. |
| 402 | |
| 403 | On the contrary, the implementation goes systematically though some states |
| 404 | even if they could be bypassed if it helps in minimizing when and where inbound |
| 405 | and outbound keys are updated. The `MBEDTLS_SSL_CLIENT_CERTIFICATE` state on |
| 406 | client side is a example of that. |
| 407 | |
| 408 | The names of the handlers processing/writing an handshake message are |
Ronald Cron | 6b14c69 | 2022-06-24 13:45:04 +0200 | [diff] [blame] | 409 | prefixed with `(mbedtls_)ssl_tls13_{process,write}`. To ease the maintenance and |
Ronald Cron | 44b23b1 | 2022-05-31 16:05:13 +0200 | [diff] [blame] | 410 | reduce the risk of bugs, the code of the message processing and writing |
| 411 | handlers is split into a sequence of stages. |
| 412 | |
| 413 | The sending of data to the peer only occurs in `mbedtls_ssl_handshake_step` |
| 414 | between the calls to the handlers and as a consequence handlers do not have to |
| 415 | care about the MBEDTLS_ERR_SSL_WANT_WRITE error code. Furthermore, all pending |
| 416 | data are flushed before to call the next handler. That way, handlers do not |
| 417 | have to worry about pending data when changing outbound keys. |
| 418 | |
| 419 | ### Message processing handlers |
| 420 | For message processing handlers, the stages are: |
| 421 | |
| 422 | * coordination stage: check if the state should be bypassed. This stage is |
| 423 | optional. The check is either purely based on the reading of the value of some |
| 424 | fields of the SSL context or based on the reading of the type of the next |
| 425 | message. The latter occurs when it is not known what the next handshake message |
| 426 | will be, an example of that on client side being if we are going to receive a |
| 427 | CertificateRequest message or not. The intent is, apart from the next record |
| 428 | reading to not modify the SSL context as this stage may be repeated if the |
| 429 | next handshake message has not been received yet. |
| 430 | |
| 431 | * fetching stage: at this stage we are sure of the type of the handshake |
| 432 | message we must receive next and we try to fetch it. If we did not go through |
| 433 | a coordination stage involving the next record type reading, the next |
| 434 | handshake message may not have been received yet, the handler returns with |
| 435 | `MBEDTLS_ERR_SSL_WANT_READ` without changing the current state and it will be |
| 436 | called again later. |
| 437 | |
| 438 | * pre-processing stage: prepare the SSL context for the message parsing. This |
| 439 | stage is optional. Any processing that must be done before the parsing of the |
| 440 | message or that can be done to simplify the parsing code. Some simple and |
| 441 | partial parsing of the handshake message may append at that stage like in the |
| 442 | ServerHello message pre-processing. |
| 443 | |
| 444 | * parsing stage: parse the message and restrict as much as possible any |
| 445 | update of the SSL context. The idea of the pre-processing/parsing/post-processing |
| 446 | organization is to concentrate solely on the parsing in the parsing function to |
| 447 | reduce the size of its code and to simplify it. |
| 448 | |
| 449 | * post-processing stage: following the parsing, further update of the SSL |
Ronald Cron | 139d0aa | 2022-06-14 18:45:44 +0200 | [diff] [blame] | 450 | context to prepare for the next incoming and outgoing messages. This stage is |
Ronald Cron | 44b23b1 | 2022-05-31 16:05:13 +0200 | [diff] [blame] | 451 | optional. For example, secret and key computations occur at this stage, as well |
| 452 | as handshake messages checksum update. |
| 453 | |
| 454 | * state change: the state change is done in the main state handler to ease the |
| 455 | navigation of the state machine transitions. |
| 456 | |
| 457 | |
| 458 | ### Message writing handlers |
| 459 | For message writing handlers, the stages are: |
| 460 | |
| 461 | * coordination stage: check if the state should be bypassed. This stage is |
| 462 | optional. The check is based on the value of some fields of the SSL context. |
| 463 | |
| 464 | * preparation stage: prepare for the message writing. This stage is optional. |
| 465 | Any processing that must be done before the writing of the message or that can |
| 466 | be done to simplify the writing code. |
| 467 | |
| 468 | * writing stage: write the message and restrict as much as possible any update |
| 469 | of the SSL context. The idea of the preparation/writing/finalization |
| 470 | organization is to concentrate solely on the writing in the writing function to |
| 471 | reduce the size of its code and simplify it. |
| 472 | |
| 473 | * finalization stage: following the writing, further update of the SSL |
| 474 | context to prepare for the next incoming and outgoing messages. This stage is |
| 475 | optional. For example, handshake secret and key computation occur at that |
| 476 | stage (ServerHello writing finalization), switching to handshake keys for |
| 477 | outbound message on server side as well. |
| 478 | |
| 479 | * state change: the state change is done in the main state handler to ease |
| 480 | the navigation of the state machine transitions. |
Ronald Cron | 4a8c9e2 | 2022-10-26 18:49:09 +0200 | [diff] [blame] | 481 | |
| 482 | |
| 483 | Writing and reading early or 0-RTT data |
| 484 | --------------------------------------- |
| 485 | |
| 486 | An application function to write and send a buffer of data to a server through |
| 487 | TLS may plausibly look like: |
| 488 | |
| 489 | ``` |
| 490 | int write_data( mbedtls_ssl_context *ssl, |
| 491 | const unsigned char *data_to_write, |
| 492 | size_t data_to_write_len, |
| 493 | size_t *data_written ) |
| 494 | { |
| 495 | *data_written = 0; |
| 496 | |
| 497 | while( *data_written < data_to_write_len ) |
| 498 | { |
| 499 | ret = mbedtls_ssl_write( ssl, data_to_write + *data_written, |
| 500 | data_to_write_len - *data_written ); |
| 501 | |
| 502 | if( ret < 0 && |
| 503 | ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 504 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 505 | { |
| 506 | return( ret ); |
| 507 | } |
| 508 | |
| 509 | *data_written += ret; |
| 510 | } |
| 511 | |
| 512 | return( 0 ); |
| 513 | } |
| 514 | ``` |
| 515 | where ssl is the SSL context to use, data_to_write the address of the data |
| 516 | buffer and data_to_write_len the number of data bytes. The handshake may |
| 517 | not be completed, not even started for the SSL context ssl when the function is |
| 518 | called and in that case the mbedtls_ssl_write() API takes care transparently of |
| 519 | completing the handshake before to write and send data to the server. The |
| 520 | mbedtls_ssl_write() may not been able to write and send all data in one go thus |
| 521 | the need for a loop calling it as long as there are still data to write and |
| 522 | send. |
| 523 | |
| 524 | An application function to write and send early data and only early data, |
| 525 | data sent during the first flight of client messages while the handshake is in |
| 526 | its initial phase, would look completely similar but the call to |
| 527 | mbedtls_ssl_write_early_data() instead of mbedtls_ssl_write(). |
| 528 | ``` |
| 529 | int write_early_data( mbedtls_ssl_context *ssl, |
| 530 | const unsigned char *data_to_write, |
| 531 | size_t data_to_write_len, |
| 532 | size_t *data_written ) |
| 533 | { |
| 534 | *data_written = 0; |
| 535 | |
| 536 | while( *data_written < data_to_write_len ) |
| 537 | { |
| 538 | ret = mbedtls_ssl_write_early_data( ssl, data_to_write + *data_written, |
| 539 | data_to_write_len - *data_written ); |
| 540 | |
| 541 | if( ret < 0 && |
| 542 | ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 543 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 544 | { |
| 545 | return( ret ); |
| 546 | } |
| 547 | |
| 548 | *data_written += ret; |
| 549 | } |
| 550 | |
| 551 | return( 0 ); |
| 552 | } |
| 553 | ``` |
| 554 | Note that compared to write_data(), write_early_data() can also return |
| 555 | MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA and that should be handled |
| 556 | specifically by the user of write_early_data(). A fresh SSL context (typically |
| 557 | just after a call to mbedtls_ssl_setup() or mbedtls_ssl_session_reset()) would |
| 558 | be expected when calling `write_early_data`. |
| 559 | |
| 560 | All together, code to write and send a buffer of data as long as possible as |
| 561 | early data and then as standard post-handshake application data could |
| 562 | plausibly look like: |
| 563 | |
| 564 | ``` |
| 565 | ret = write_early_data( ssl, data_to_write, data_to_write_len, |
| 566 | &early_data_written ); |
| 567 | if( ret < 0 && |
| 568 | ret != MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA ) |
| 569 | { |
| 570 | goto error; |
| 571 | } |
| 572 | |
| 573 | ret = write_data( ssl, data_to_write + early_data_written, |
| 574 | data_to_write_len - early_data_written, &data_written ); |
| 575 | if( ret < 0 ) |
| 576 | goto error; |
| 577 | |
| 578 | data_written += early_data_written; |
| 579 | ``` |
| 580 | |
| 581 | Finally, taking into account that the server may reject early data, application |
| 582 | code to write and send a buffer of data could plausibly look like: |
| 583 | ``` |
| 584 | ret = write_early_data( ssl, data_to_write, data_to_write_len, |
| 585 | &early_data_written ); |
| 586 | if( ret < 0 && |
| 587 | ret != MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA ) |
| 588 | { |
| 589 | goto error; |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | * Make sure the handshake is completed as it is a requisite to |
| 594 | * mbedtls_ssl_get_early_data_status(). |
| 595 | */ |
| 596 | while( !mbedtls_ssl_is_handshake_over( ssl ) ) |
| 597 | { |
| 598 | ret = mbedtls_ssl_handshake( ssl ); |
| 599 | if( ret < 0 && |
| 600 | ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 601 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 602 | { |
| 603 | goto error; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | ret = mbedtls_ssl_get_early_data_status( ssl ); |
| 608 | if( ret < 0 ) |
| 609 | goto error; |
| 610 | |
| 611 | if( ret == MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED ) |
| 612 | early_data_written = 0; |
| 613 | |
| 614 | ret = write_data( ssl, data_to_write + early_data_written, |
| 615 | data_to_write_len - early_data_written, &data_written ); |
| 616 | if( ret < 0 ) |
| 617 | goto error; |
| 618 | |
| 619 | data_written += early_data_written; |
| 620 | ``` |
| 621 | |
| 622 | Basically, the same holds for reading early data on the server side without the |
| 623 | complication of possible rejection. An application function to read early data |
| 624 | into a given buffer could plausibly look like: |
| 625 | ``` |
| 626 | int read_early_data( mbedtls_ssl_context *ssl, |
| 627 | unsigned char *buffer, |
| 628 | size_t buffer_size, |
| 629 | size_t *data_len ) |
| 630 | { |
| 631 | *data_len = 0; |
| 632 | |
| 633 | while( *data_len < buffer_size ) |
| 634 | { |
| 635 | ret = mbedtls_ssl_read_early_data( ssl, buffer + *data_len, |
| 636 | buffer_size - *data_len ); |
| 637 | |
| 638 | if( ret < 0 && |
| 639 | ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 640 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 641 | { |
| 642 | return( ret ); |
| 643 | } |
| 644 | |
| 645 | *data_len += ret; |
| 646 | } |
| 647 | |
| 648 | return( 0 ); |
| 649 | } |
| 650 | ``` |
| 651 | with again calls to read_early_data() expected to be done with a fresh SSL |
| 652 | context. |