@@ -734,6 +734,51 @@ typedef enum {
734734}
735735mbedtls_ssl_states ;
736736
737+ /*
738+ * Early data status, client side only.
739+ */
740+
741+ #if defined(MBEDTLS_SSL_EARLY_DATA ) && defined(MBEDTLS_SSL_CLI_C )
742+ typedef enum {
743+ /*
744+ * The client has not sent the first ClientHello yet, it is unknown if the
745+ * client will send an early data indication extension or not.
746+ */
747+ MBEDTLS_SSL_EARLY_DATA_STATUS_UNKNOWN ,
748+
749+ /*
750+ * See documentation of mbedtls_ssl_get_early_data_status().
751+ */
752+ MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT ,
753+ MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED ,
754+ MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED ,
755+
756+ /*
757+ * The client has sent an early data indication extension in its first
758+ * ClientHello, it has not received the response (ServerHello or
759+ * HelloRetryRequest) from the server yet. The transform to protect early data
760+ * is not set and early data cannot be sent yet.
761+ */
762+ MBEDTLS_SSL_EARLY_DATA_STATUS_SENT ,
763+
764+ /*
765+ * The client has sent an early data indication extension in its first
766+ * ClientHello, it has not received the response (ServerHello or
767+ * HelloRetryRequest) from the server yet. The transform to protect early data
768+ * has been set and early data can be written now.
769+ */
770+ MBEDTLS_SSL_EARLY_DATA_STATUS_CAN_WRITE ,
771+
772+ /*
773+ * The client has sent an early data indication extension in its first
774+ * ClientHello, the server has accepted them and the client has received the
775+ * server Finished message. It cannot send early data to the server anymore.
776+ */
777+ MBEDTLS_SSL_EARLY_DATA_STATUS_SERVER_FINISHED_RECEIVED ,
778+ } mbedtls_ssl_early_data_status ;
779+
780+ #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_CLI_C */
781+
737782/**
738783 * \brief Callback type: send data on the network.
739784 *
@@ -1673,33 +1718,29 @@ struct mbedtls_ssl_context {
16731718#endif /* MBEDTLS_SSL_RENEGOTIATION */
16741719
16751720 /**
1676- * Maximum TLS version to be negotiated, then negotiated TLS version.
1721+ * Maximum TLS version to be negotiated, then negotiated TLS version.
16771722 *
1678- * It is initialized as the configured maximum TLS version to be
1679- * negotiated by mbedtls_ssl_setup().
1723+ * It is initialized as the configured maximum TLS version to be
1724+ * negotiated by mbedtls_ssl_setup().
16801725 *
1681- * When renegotiating or resuming a session, it is overwritten in the
1682- * ClientHello writing preparation stage with the previously negotiated
1683- * TLS version.
1726+ * When renegotiating or resuming a session, it is overwritten in the
1727+ * ClientHello writing preparation stage with the previously negotiated
1728+ * TLS version.
16841729 *
1685- * On client side, it is updated to the TLS version selected by the server
1686- * for the handshake when the ServerHello is received.
1730+ * On client side, it is updated to the TLS version selected by the server
1731+ * for the handshake when the ServerHello is received.
16871732 *
1688- * On server side, it is updated to the TLS version the server selects for
1689- * the handshake when the ClientHello is received.
1733+ * On server side, it is updated to the TLS version the server selects for
1734+ * the handshake when the ClientHello is received.
16901735 */
16911736 mbedtls_ssl_protocol_version MBEDTLS_PRIVATE (tls_version );
16921737
16931738#if defined(MBEDTLS_SSL_EARLY_DATA ) && defined(MBEDTLS_SSL_CLI_C )
16941739 /**
1695- * Status of the negotiation of the use of early data.
1696- * See the documentation of mbedtls_ssl_get_early_data_status() for more
1697- * information.
1698- *
1699- * Reset to #MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT when the context is
1700- * reset.
1740+ * Status of the negotiation of the use of early data. Reset to
1741+ * MBEDTLS_SSL_EARLY_DATA_STATUS_UNKNOWN when the context is reset.
17011742 */
1702- int MBEDTLS_PRIVATE (early_data_status );
1743+ mbedtls_ssl_early_data_status MBEDTLS_PRIVATE (early_data_status );
17031744#endif
17041745
17051746 unsigned MBEDTLS_PRIVATE (badmac_seen ); /*!< records with a bad MAC received */
@@ -5150,10 +5191,6 @@ int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl);
51505191
51515192#if defined(MBEDTLS_SSL_EARLY_DATA )
51525193
5153- #define MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT 1
5154- #define MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED 2
5155- #define MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED 3
5156-
51575194#if defined(MBEDTLS_SSL_SRV_C )
51585195/**
51595196 * \brief Read at most 'len' bytes of early data
@@ -5206,17 +5243,43 @@ int mbedtls_ssl_read_early_data(mbedtls_ssl_context *ssl,
52065243 * \brief Try to write exactly 'len' application data bytes while
52075244 * performing the handshake (early data).
52085245 *
5246+ * \warning Early data is defined in the TLS 1.3 specification, RFC 8446.
5247+ * IMPORTANT NOTE from section 2.3 of the specification:
5248+ *
5249+ * The security properties for 0-RTT data are weaker than
5250+ * those for other kinds of TLS data. Specifically:
5251+ * - This data is not forward secret, as it is encrypted
5252+ * solely under keys derived using the offered PSK.
5253+ * - There are no guarantees of non-replay between connections.
5254+ * Protection against replay for ordinary TLS 1.3 1-RTT data
5255+ * is provided via the server's Random value, but 0-RTT data
5256+ * does not depend on the ServerHello and therefore has
5257+ * weaker guarantees. This is especially relevant if the
5258+ * data is authenticated either with TLS client
5259+ * authentication or inside the application protocol. The
5260+ * same warnings apply to any use of the
5261+ * early_exporter_master_secret.
5262+ *
52095263 * \note This function behaves mainly as mbedtls_ssl_write(). The
52105264 * specification of mbedtls_ssl_write() relevant to TLS 1.3
52115265 * (thus not the parts specific to (D)TLS1.2) applies to this
5212- * function and the present documentation is restricted to the
5213- * differences with mbedtls_ssl_write().
5266+ * function and the present documentation is mainly restricted
5267+ * to the differences with mbedtls_ssl_write(). One noticeable
5268+ * difference though is that mbedtls_ssl_write() aims to
5269+ * complete the handshake before to write application data
5270+ * while mbedtls_ssl_write_early() aims to drive the handshake
5271+ * just past the point where it is not possible to send early
5272+ * data anymore.
52145273 *
52155274 * \param ssl SSL context
52165275 * \param buf buffer holding the data
52175276 * \param len how many bytes must be written
52185277 *
5219- * \return One additional specific return value:
5278+ * \return The (non-negative) number of bytes actually written if
5279+ * successful (may be less than \p len).
5280+ *
5281+ * \return One additional specific error code compared to
5282+ * mbedtls_ssl_write():
52205283 * #MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA.
52215284 *
52225285 * #MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA is returned when it
@@ -5237,9 +5300,11 @@ int mbedtls_ssl_read_early_data(mbedtls_ssl_context *ssl,
52375300 * already completed.
52385301 *
52395302 * It is not possible to write early data for the SSL context
5240- * \p ssl but this does not preclude for using it with
5303+ * \p ssl and any subsequent call to this API will return this
5304+ * error code. But this does not preclude for using it with
52415305 * mbedtls_ssl_write(), mbedtls_ssl_read() or
5242- * mbedtls_ssl_handshake().
5306+ * mbedtls_ssl_handshake() and the handshake can be
5307+ * completed by calling one of these APIs.
52435308 *
52445309 * \note This function may write early data only if the SSL context
52455310 * has been configured for the handshake with a PSK for which
0 commit comments