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