Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

mbedTLS: Update to 4.1.2, TF-PSA-Crypto 1.1.2

Fabio Alessandrelli8693d38588a9
diff --git a/thirdparty/README.md b/thirdparty/README.md
index b478e43344d..7ac76ee449a 100644
--- a/thirdparty/README.md
+++ b/thirdparty/README.md
@@ -701,7 +701,7 @@ Patches:
## mbedtls
- Upstream: https://github.com/Mbed-TLS/mbedtls
-- Version: 4.1.1 (0a8fda272a5a0abef3b47c91bed37185d5a726b1, 2026)
+- Version: 4.1.2 (8808d31cdffb1a2c82645a531a96a0f26dd3e8c3, 2026)
- License: Apache 2.0
Update instructions:
diff --git a/thirdparty/mbedtls/include/mbedtls/build_info.h b/thirdparty/mbedtls/include/mbedtls/build_info.h
index e83b7e99e25..fd8d51f24de 100644
--- a/thirdparty/mbedtls/include/mbedtls/build_info.h
+++ b/thirdparty/mbedtls/include/mbedtls/build_info.h
@@ -28,16 +28,16 @@
*/
#define MBEDTLS_VERSION_MAJOR 4
#define MBEDTLS_VERSION_MINOR 1
-#define MBEDTLS_VERSION_PATCH 1
+#define MBEDTLS_VERSION_PATCH 2
/**
* The single version number has the following structure:
* MMNNPP00
* Major version | Minor version | Patch version
*/
-#define MBEDTLS_VERSION_NUMBER 0x04010100
-#define MBEDTLS_VERSION_STRING "4.1.1"
-#define MBEDTLS_VERSION_STRING_FULL "Mbed TLS 4.1.1"
+#define MBEDTLS_VERSION_NUMBER 0x04010200
+#define MBEDTLS_VERSION_STRING "4.1.2"
+#define MBEDTLS_VERSION_STRING_FULL "Mbed TLS 4.1.2"
#if defined(MBEDTLS_CONFIG_FILES_READ)
#error "Something went wrong: MBEDTLS_CONFIG_FILES_READ defined before reading the config files!"
diff --git a/thirdparty/mbedtls/include/mbedtls/oid.h b/thirdparty/mbedtls/include/mbedtls/oid.h
index d769ff2180f..dc70d326742 100644
--- a/thirdparty/mbedtls/include/mbedtls/oid.h
+++ b/thirdparty/mbedtls/include/mbedtls/oid.h
@@ -265,6 +265,10 @@
* ecdsa-with-SHA2(3) 4 } */
#define MBEDTLS_OID_ECDSA_SHA512 MBEDTLS_OID_ANSI_X9_62_SIG_SHA2 "\x04"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#if defined(MBEDTLS_X509_USE_C)
/**
* \brief Translate an ASN.1 OID into its numeric representation
@@ -301,4 +305,8 @@ int mbedtls_oid_get_numeric_string(char *buf, size_t size, const mbedtls_asn1_bu
int mbedtls_oid_from_numeric_string(mbedtls_asn1_buf *oid, const char *oid_str, size_t size);
#endif /* MBEDTLS_X509_CREATE_C */
+#ifdef __cplusplus
+}
+#endif
+
#endif /* oid.h */
diff --git a/thirdparty/mbedtls/include/mbedtls/ssl.h b/thirdparty/mbedtls/include/mbedtls/ssl.h
index 00e28442d68..fc381bfd34d 100644
--- a/thirdparty/mbedtls/include/mbedtls/ssl.h
+++ b/thirdparty/mbedtls/include/mbedtls/ssl.h
@@ -648,6 +648,16 @@
#endif
#endif /* !MBEDTLS_PSK_MAX_LEN */
+#define MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN 48
+
+#define MBEDTLS_SSL_SESSION_SECRET_LEN MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN
+
+#if defined(MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) && \
+ MBEDTLS_SSL_SESSION_SECRET_LEN < \
+ MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN
+#error "MBEDTLS_SSL_SESSION_SECRET_LEN is too small for the TLS 1.3 ticket resumption key"
+#endif
+
/* Dummy type used only for its size */
union mbedtls_ssl_premaster_secret {
unsigned char dummy; /* Make the union non-empty even with SSL disabled */
@@ -1165,7 +1175,10 @@ struct mbedtls_ssl_session {
int MBEDTLS_PRIVATE(ciphersuite); /*!< chosen ciphersuite */
size_t MBEDTLS_PRIVATE(id_len); /*!< session id length */
unsigned char MBEDTLS_PRIVATE(id)[32]; /*!< session identifier */
- unsigned char MBEDTLS_PRIVATE(master)[48]; /*!< the master secret */
+ /*! The session secret:
+ * - TLS 1.2: the master secret (MBEDTLS_SSL_SESSION_SECRET_LEN bytes)
+ * - TLS 1.3: the resumption key (resumption_key_len bytes) */
+ unsigned char MBEDTLS_PRIVATE(secret)[MBEDTLS_SSL_SESSION_SECRET_LEN];
#if defined(MBEDTLS_X509_CRT_PARSE_C)
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
@@ -1210,8 +1223,9 @@ struct mbedtls_ssl_session {
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
uint32_t MBEDTLS_PRIVATE(ticket_age_add); /*!< Randomly generated value used to obscure the age of the ticket */
uint8_t MBEDTLS_PRIVATE(ticket_flags); /*!< Ticket flags */
- uint8_t MBEDTLS_PRIVATE(resumption_key_len); /*!< resumption_key length */
- unsigned char MBEDTLS_PRIVATE(resumption_key)[MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN];
+ uint8_t MBEDTLS_PRIVATE(resumption_key_len); /*!< resumption key length */
+ unsigned char MBEDTLS_PRIVATE(resumption_key_unused)[
+ MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN]; /*!< Unused, kept for ABI compatibility */
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_SSL_CLI_C)
char *MBEDTLS_PRIVATE(hostname); /*!< host name binded with tickets */
@@ -2019,6 +2033,13 @@ void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport);
* the right time(s), which may not be obvious, while REQUIRED always perform
* the verification as soon as possible. For example, REQUIRED was protecting
* against the "triple handshake" attack even before it was found.
+ *
+ * \note This setting is part of the peer
+ * certificate-authentication context. For more information, see
+ * the documentation of \c mbedtls_ssl_set_session()
+ * (client-side) or
+ * \c mbedtls_ssl_conf_session_cache() and
+ * \c mbedtls_ssl_conf_session_tickets_cb() (server-side).
*/
void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode);
@@ -2095,6 +2116,13 @@ void mbedtls_ssl_conf_max_early_data_size(
* \note For per context callbacks and contexts, please use
* mbedtls_ssl_set_verify() instead.
*
+ * \note This callback is part of the peer
+ * certificate-authentication context. For more information, see
+ * the documentation of \c mbedtls_ssl_set_session()
+ * (client-side) or
+ * \c mbedtls_ssl_conf_session_cache() and
+ * \c mbedtls_ssl_conf_session_tickets_cb() (server-side).
+ *
* \param conf The SSL configuration to use.
* \param f_vrfy The verification callback to use during CRT verification.
* \param p_vrfy The opaque context to be passed to the callback.
@@ -2403,6 +2431,12 @@ void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu);
* If mbedtls_ssl_conf_verify() and mbedtls_ssl_set_verify()
* are both used, mbedtls_ssl_set_verify() takes precedence.
*
+ * \note On a server, this callback is part of the client
+ * certificate-authentication context. When session resumption
+ * is enabled, see the documentation of
+ * \c mbedtls_ssl_conf_session_cache() and
+ * \c mbedtls_ssl_conf_session_tickets_cb().
+ *
* \param ssl The SSL context to use.
* \param f_vrfy The verification callback to use during CRT verification.
* \param p_vrfy The opaque context to be passed to the callback.
@@ -2510,6 +2544,15 @@ void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
* If set, the callback is always called for each handshake,
* after `ClientHello` processing has finished.
*
+ * \note On a server, this callback may select or modify the client
+ * certificate-authentication context for the current
+ * handshake, for example by calling
+ * \c mbedtls_ssl_set_hs_ca_chain() or
+ * \c mbedtls_ssl_set_hs_authmode(). When session resumption is
+ * enabled, see the documentation of
+ * \c mbedtls_ssl_conf_session_cache() and
+ * \c mbedtls_ssl_conf_session_tickets_cb().
+ *
* \param conf The SSL configuration to register the callback with.
* \param f_cert_cb The callback for selecting server certificate after
* `ClientHello` processing has finished.
@@ -2585,6 +2628,30 @@ typedef int mbedtls_ssl_ticket_parse_t(void *p_ticket,
*
* \note On client, use \c mbedtls_ssl_conf_session_tickets().
*
+ * \warning If a server uses session tickets with client
+ * authentication, tickets must only be accepted for resumption
+ * under the same client authentication context under which
+ * they were issued. If the client authentication context is
+ * modified, the application must invalidate tickets issued
+ * under the previous context. Otherwise, a session
+ * established under one authentication context could be
+ * resumed under a different one. For the definition of the
+ * client authentication context, see
+ * docs/architecture/authentication-and-resumption.md.
+ *
+ * \note To invalidate previously issued tickets, the ticket
+ * callback context must be reset using the ticket
+ * implementation's free, init and setup functions, so the
+ * ticket parsing callback no longer accepts tickets issued
+ * under the previous client authentication context. For
+ * \c mbedtls_ssl_ticket_context, these functions are
+ * \c mbedtls_ssl_ticket_free(),
+ * \c mbedtls_ssl_ticket_init() and
+ * \c mbedtls_ssl_ticket_setup(). Ticket-key rotation is not
+ * necessarily sufficient for this purpose: normal key
+ * rotation can preserve the ability to decrypt recent
+ * tickets.
+ *
* \param conf SSL configuration context
* \param f_ticket_write Callback for writing a ticket
* \param f_ticket_parse Callback for parsing a ticket
@@ -2595,6 +2662,29 @@ void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
mbedtls_ssl_ticket_parse_t *f_ticket_parse,
void *p_ticket);
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+/**
+ * \brief Set the SSL session ticket context for the current
+ * handshake (server only).
+ *
+ * \note This function should only be called inside the SNI callback,
+ * i.e. the function passed to \c mbedtls_ssl_conf_sni().
+ *
+ * \note If not equal to \c NULL, the ticket context \p p_ticket
+ * takes precedence for the current handshake over the one
+ configured with \c mbedtls_ssl_conf_session_tickets_cb().
+ * The configured ticket callbacks are unchanged.
+ * Passing \c NULL clears the session ticket context for the
+ * current handshake, so the configured ticket context is used.
+ *
+ * \param ssl SSL context passed to the SNI callback.
+ * \param p_ticket Context passed to the session ticket callbacks. It must
+ * remain valid until the current handshake completes.
+ */
+void mbedtls_ssl_set_hs_session_ticket_context(mbedtls_ssl_context *ssl,
+ void *p_ticket);
+#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
+
#if defined(MBEDTLS_HAVE_TIME)
/**
* \brief Get the creation time of a session ticket.
@@ -3101,8 +3191,8 @@ void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf, uint32_t min,
* The get callback is called once during the initial handshake
* to enable session resuming. The get function has the
* following parameters: (void *parameter, mbedtls_ssl_session *session)
- * If a valid entry is found, it should fill the master of
- * the session object with the cached values and return 0,
+ * If a valid entry is found, it should fill the session
+ * object with the cached values and return 0,
* return 1 otherwise. Optionally peer_cert can be set as well
* if it is properly present in cache entry.
*
@@ -3118,6 +3208,26 @@ void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf, uint32_t min,
* an entry is still valid in the future. Return 0 if
* successfully cached, return 1 otherwise.
*
+ * \warning If a server uses a session cache with client authentication,
+ * the cache must only be used to resume sessions under the same
+ * client authentication context under which the sessions were
+ * established. If the client authentication context is modified,
+ * the application must reset the session cache. Otherwise, a
+ * session established under one authentication context could be
+ * resumed under a different one. For the definition of the
+ * client authentication context, see
+ * docs/architecture/authentication-and-resumption.md.
+ *
+ * \note To reset the session cache, the cache callback context must
+ * be reset using the cache implementation's free and init
+ * functions, so the session get callback no longer
+ * returns sessions established under the previous client
+ * authentication context. An
+ * \c mbedtls_ssl_cache_context is reset by calling
+ * \c mbedtls_ssl_cache_free(), followed by
+ * \c mbedtls_ssl_cache_init(), and then reapplying any cache
+ * options such as the timeout and maximum number of entries.
+ *
* \param conf SSL configuration
* \param p_cache parameter (context) for both callbacks
* \param f_get_cache session get callback
@@ -3127,6 +3237,29 @@ void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
void *p_cache,
mbedtls_ssl_cache_get_t *f_get_cache,
mbedtls_ssl_cache_set_t *f_set_cache);
+
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+/**
+ * \brief Set the session cache callback context for the current
+ * handshake (server only).
+ *
+ * \note This function should only be called inside the SNI callback,
+ * i.e. the function passed to \c mbedtls_ssl_conf_sni().
+ *
+ * \note If not equal to \c NULL, the cache context \p p_cache
+ * takes precedence for the current handshake over the one
+ * configured with \c mbedtls_ssl_conf_session_cache().
+ * The configured cache callbacks are unchanged.
+ * Passing \c NULL clears the session cache context for the
+ * current handshake, so the configured cache context is used.
+ *
+ * \param ssl SSL context passed to the SNI callback.
+ * \param p_cache Context passed to the session cache callbacks. It must
+ * remain valid until the current handshake completes.
+ */
+void mbedtls_ssl_set_hs_session_cache_context(mbedtls_ssl_context *ssl,
+ void *p_cache);
+#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
#endif /* MBEDTLS_SSL_SRV_C */
#if defined(MBEDTLS_SSL_CLI_C)
@@ -3154,6 +3287,14 @@ void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
* with a TLS 1.3 session, will not have any effect on the next
* handshake for the SSL context \p ssl.
*
+ * \warning When using session resumption on a client, an application
+ * must only load a session under the same server-authentication
+ * context under which the session was established. If the
+ * server-authentication context changes, the application must
+ * stop using sessions established under the previous context.
+ * For the definition of the server-authentication context, see
+ * docs/architecture/authentication-and-resumption.md.
+ *
* \param ssl The SSL context representing the connection which should
* be attempted to be setup using session resumption. This
* must be initialized via mbedtls_ssl_init() and bound to
@@ -3425,6 +3566,13 @@ int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf, size_t len,
* chain. However, signatures in the handshake are not covered
* by this setting but by \c mbedtls_ssl_conf_sig_algs().
*
+ * \note This profile is part of the peer
+ * certificate-authentication context. For more information, see
+ * the documentation of \c mbedtls_ssl_set_session()
+ * (client-side) or
+ * \c mbedtls_ssl_conf_session_cache() and
+ * \c mbedtls_ssl_conf_session_tickets_cb() (server-side).
+ *
* \param conf SSL configuration
* \param profile Profile to use
*/
@@ -3438,6 +3586,13 @@ void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
* parameters ca_chain (maps to trust_ca for that function)
* and ca_crl.
*
+ * \note The trusted CA chain and CRLs are part of the peer
+ * certificate-authentication context. For more information, see
+ * the documentation of \c mbedtls_ssl_set_session()
+ * (client-side) or
+ * \c mbedtls_ssl_conf_session_cache() and
+ * \c mbedtls_ssl_conf_session_tickets_cb() (server-side).
+ *
* \param conf SSL configuration
* \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
* \param ca_crl trusted CA CRLs
@@ -3509,6 +3664,13 @@ void mbedtls_ssl_conf_dn_hints(mbedtls_ssl_config *conf,
* during X.509 CRT signature verification (but doesn't affect
* other uses).
*
+ * \note This callback is part of the peer
+ * certificate-authentication context. For more information, see
+ * the documentation of \c mbedtls_ssl_set_session()
+ * (client-side) or
+ * \c mbedtls_ssl_conf_session_cache() and
+ * \c mbedtls_ssl_conf_session_tickets_cb() (server-side).
+ *
* \warning This API is incompatible with the use of CRLs. Any call to
* mbedtls_ssl_conf_ca_cb() unsets CRLs configured through
* earlier calls to mbedtls_ssl_conf_ca_chain().
@@ -3581,6 +3743,11 @@ int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
* \note A PSK set by \c mbedtls_ssl_set_hs_psk() in the PSK callback
* takes precedence over a PSK configured by this function.
*
+ * \note On a server, this PSK is part of the PSK-authentication
+ * context. When session resumption is enabled, see the
+ * documentation of \c mbedtls_ssl_conf_session_cache() and
+ * \c mbedtls_ssl_conf_session_tickets_cb().
+ *
* \param conf The SSL configuration to register the PSK with.
* \param psk The pointer to the pre-shared key to use.
* \param psk_len The length of the pre-shared key in bytes.
@@ -3618,6 +3785,11 @@ int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
* the PSK callback takes precedence over an opaque PSK
* configured by this function.
*
+ * \note On a server, this PSK is part of the PSK-authentication
+ * context. When session resumption is enabled, see the
+ * documentation of \c mbedtls_ssl_conf_session_cache() and
+ * \c mbedtls_ssl_conf_session_tickets_cb().
+ *
* \param conf The SSL configuration to register the PSK with.
* \param psk The identifier of the key slot holding the PSK.
* Until \p conf is destroyed or this function is successfully
@@ -3718,6 +3890,11 @@ int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
* \c mbedtls_ssl_conf_psk() or
* \c mbedtls_ssl_conf_psk_opaque()).
*
+ * \note On a server, this callback is part of the PSK-authentication
+ * context. When session resumption is enabled, see the
+ * documentation of \c mbedtls_ssl_conf_session_cache() and
+ * \c mbedtls_ssl_conf_session_tickets_cb().
+ *
* \param conf The SSL configuration to register the callback with.
* \param f_psk The callback for selecting and setting the PSK based
* in the PSK identity chosen by the client.
@@ -4083,6 +4260,25 @@ void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
* then must return 0. If no matching name is found, the
* callback may return non-zero to abort the handshake.
*
+ * \note In a TLS 1.3 handshake involving a HelloRetryRequest, this
+ * callback is invoked for each ClientHello containing the
+ * same ServerName extension, and may therefore be invoked
+ * twice with the same hostname. If the ServerName in the
+ * second ClientHello is inconsistent with the first ClientHello,
+ * the callback is not invoked again and the handshake is aborted.
+ *
+ * \note On a server, this callback may select or modify the client
+ * certificate-authentication context for the current handshake,
+ * for example by calling \c mbedtls_ssl_set_hs_ca_chain() or
+ * \c mbedtls_ssl_set_hs_authmode(). When session resumption is
+ * enabled, the callback may also use
+ * \c mbedtls_ssl_set_hs_session_cache_context() and
+ * \c mbedtls_ssl_set_hs_session_ticket_context() to select the
+ * session cache and session-ticket contexts for the current
+ * handshake. See the documentation of
+ * \c mbedtls_ssl_conf_session_cache() and
+ * \c mbedtls_ssl_conf_session_tickets_cb().
+ *
* \param conf SSL configuration
* \param f_sni verification function
* \param p_sni verification parameter
@@ -4783,6 +4979,12 @@ const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl
* session. If session tickets are disabled, exported sessions
* cannot be used to resume a TLS 1.3 session.
*
+ * \note When using session resumption on a client, the application
+ * must store each exported session together with the
+ * server-authentication context under which it was established,
+ * and only resume the session under the same context. See
+ * docs/architecture/authentication-and-resumption.md.
+ *
* \return \c 0 if successful. In this case, \p session can be used for
* session resumption by passing it to mbedtls_ssl_set_session(),
* and serialized for storage via mbedtls_ssl_session_save().
@@ -4824,6 +5026,9 @@ int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
* enabled on server (see mbedtls_ssl_conf_early_data()
* documentation). You must call mbedtls_ssl_read_early_data()
* to read the early data before resuming the handshake.
+ * \return #MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO if application data was received
+ * while the client was waiting for the ServerHello during renegotiation. Call
+ * mbedtls_ssl_read() to consume the pending data before continuing renegotiation.
* \return Another SSL error code - in this case you must stop using
* the context (see below).
*
@@ -4833,6 +5038,8 @@ int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
* #MBEDTLS_ERR_SSL_WANT_WRITE,
* #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
* #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS or
+ * #MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO
+ * (on the client side only) or
* #MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA,
* you must stop using the SSL context for reading or writing,
* and either free it or call \c mbedtls_ssl_session_reset()
@@ -4913,6 +5120,8 @@ static inline int mbedtls_ssl_is_handshake_over(mbedtls_ssl_context *ssl)
* #MBEDTLS_ERR_SSL_WANT_READ, #MBEDTLS_ERR_SSL_WANT_WRITE,
* #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS,
* #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS or
+ * #MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO
+ * (on the client side only) or
* #MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA, you must stop using
* the SSL context for reading or writing, and either free it
* or call \c mbedtls_ssl_session_reset() on it before
@@ -4938,7 +5147,10 @@ int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl);
* \warning If this function returns something other than \c 0,
* #MBEDTLS_ERR_SSL_WANT_READ, #MBEDTLS_ERR_SSL_WANT_WRITE,
* #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
- * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS, you must stop using
+ * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS or
+ * #MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO
+ * (on the client side only),
+ * you must stop using
* the SSL context for reading or writing, and either free it
* or call \c mbedtls_ssl_session_reset() on it before
* re-using it for a new connection; the current connection
@@ -5070,6 +5282,9 @@ int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len);
* enabled on server (see mbedtls_ssl_conf_early_data()
* documentation). You must call mbedtls_ssl_read_early_data()
* to read the early data before resuming the handshake.
+ * \return #MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO if application data was received
+ * while the client was waiting for the ServerHello during renegotiation. Call
+ * mbedtls_ssl_read() to consume the pending data before continuing renegotiation.
* \return Another SSL error code - in this case you must stop using
* the context (see below).
*
@@ -5079,6 +5294,8 @@ int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len);
* #MBEDTLS_ERR_SSL_WANT_WRITE,
* #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS,
* #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS or
+ * #MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO
+ * (on the client side only) or
* #MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA,
* you must stop using the SSL context for reading or writing,
* and either free it or call \c mbedtls_ssl_session_reset()
diff --git a/thirdparty/mbedtls/include/mbedtls/x509.h b/thirdparty/mbedtls/include/mbedtls/x509.h
index 130c427c4f6..b408d5e0dda 100644
--- a/thirdparty/mbedtls/include/mbedtls/x509.h
+++ b/thirdparty/mbedtls/include/mbedtls/x509.h
@@ -119,7 +119,10 @@
* uniformResourceIdentifier [6] IA5String,
* iPAddress [7] OCTET STRING,
* registeredID [8] OBJECT IDENTIFIER
+ *
+ * Add NONE for the benefit of x509_crt_infer_name_type().
*/
+#define MBEDTLS_X509_SAN_NONE -1
#define MBEDTLS_X509_SAN_OTHER_NAME 0
#define MBEDTLS_X509_SAN_RFC822_NAME 1
#define MBEDTLS_X509_SAN_DNS_NAME 2
@@ -303,15 +306,18 @@ mbedtls_x509_san_list;
/** \} addtogroup x509_module */
/**
- * \brief Store the certificate DN in printable form into buf;
- * no more than size characters will be written.
+ * \brief Store the certificate DN in printable form into buf.
*
- * \param buf Buffer to write to
- * \param size Maximum size of buffer
- * \param dn The X509 name to represent
+ * \param buf Buffer to write to.
+ * On success, it contains the printable form of the name
+ * plus a terminating null byte.
+ * \param size Size of \p buf, in bytes.
+ * It should have enough room for the name plus a
+ * terminating null byte.
+ * \param dn The X509 name to represent.
*
* \return The length of the string written (not including the
- * terminated nul byte), or a negative error code.
+ * terminating null byte), or a negative error code.
*/
int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn);
@@ -500,4 +506,27 @@ size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst);
}
#endif
+/**
+ * \brief Translate hash algorithm OID into md_type
+ *
+ * \param oid OID to use
+ * \param md_alg place to store message digest algorithm
+ *
+ * \return 0 if successful, or MBEDTLS_ERR_X509_UNKNOWN_OID
+ */
+int mbedtls_x509_oid_get_md_alg(const mbedtls_asn1_buf *oid, mbedtls_md_type_t *md_alg);
+
+/**
+ * \brief Translate a signature algorithm OID and parameters into PK sigalg
+ *
+ * \param sig_oid OID of the signature algorithm from the AlgorithmIdentifier type
+ * \param sig_params Signature parameters from the AlgorithmIdentifier type
+ * \param[out] md_alg The hash algorithm component of the signature algorithm (if any)
+ * \param[out] pk_alg The PK algorithm that corresponds to the signature algorithm
+ *
+ * \return 0 if successful, or a negative error code on failure
+ */
+int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
+ mbedtls_md_type_t *md_alg, mbedtls_pk_sigalg_t *pk_alg);
+
#endif /* MBEDTLS_X509_H */
diff --git a/thirdparty/mbedtls/include/mbedtls/x509_crl.h b/thirdparty/mbedtls/include/mbedtls/x509_crl.h
index 095cb5d9a50..da424242d95 100644
--- a/thirdparty/mbedtls/include/mbedtls/x509_crl.h
+++ b/thirdparty/mbedtls/include/mbedtls/x509_crl.h
@@ -94,6 +94,10 @@ mbedtls_x509_crl;
/**
* \brief Parse a DER-encoded CRL and append it to the chained list
*
+ * \note Issuer names in CRL entries must have the same encoding
+ * as in the certificate. See the warning on
+ * mbedtls_x509_crt_is_revoked() for details.
+ *
* \note The PSA crypto subsystem must have been initialized by
* calling psa_crypto_init() before calling this function.
*
@@ -109,6 +113,10 @@ int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
/**
* \brief Parse one or more CRLs and append them to the chained list
*
+ * \note Issuer names in CRL entries must have the same encoding
+ * as in the certificate. See the warning on
+ * mbedtls_x509_crt_is_revoked() for details.
+ *
* \note Multiple CRLs are accepted only if using PEM format
*
* \note The PSA crypto subsystem must have been initialized by
@@ -127,6 +135,10 @@ int mbedtls_x509_crl_parse(mbedtls_x509_crl *chain, const unsigned char *buf, si
/**
* \brief Load one or more CRLs and append them to the chained list
*
+ * \note Issuer names in CRL entries must have the same encoding
+ * as in the certificate. See the warning on
+ * mbedtls_x509_crt_is_revoked() for details.
+ *
* \note Multiple CRLs are accepted only if using PEM format
*
* \note The PSA crypto subsystem must have been initialized by
diff --git a/thirdparty/mbedtls/include/mbedtls/x509_crt.h b/thirdparty/mbedtls/include/mbedtls/x509_crt.h
index 0a7b5324048..037c322a75f 100644
--- a/thirdparty/mbedtls/include/mbedtls/x509_crt.h
+++ b/thirdparty/mbedtls/include/mbedtls/x509_crt.h
@@ -262,7 +262,7 @@ typedef struct {
#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
/**
- * Verification chain as built by \c mbedtls_crt_verify_chain()
+ * Verification chain as built by \c mbedtls_x509_crt_verify_chain()
*/
typedef struct {
mbedtls_x509_crt_verify_chain_item MBEDTLS_PRIVATE(items)[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
@@ -599,7 +599,47 @@ int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
/**
* \brief Verify a chain of certificates.
*
- * The verify callback is a user-supplied callback that
+ * \note The \p cn represents the reference identifier (historically,
+ * the Common Name component of the certificate's Subject). It
+ * should be either a DNS name, a URI or an IP address. (If the
+ * string can be interpreted either as a URI or an IP address,
+ * it will be considered an IP address.)
+ *
+ * DNS names must be provided in the "preferred name syntax", as
+ * specified by Section 3.5 of RFC 1034 and as modified by
+ * Section 2.1 of RFC 1123. Internationalized domain names must
+ * be in ASCII Compatible Encoding (ACE) format as specified in
+ * Section 4 of RFC 3490 (also known as Punycode).
+ *
+ * The provided DNS name will be matched against Subject
+ * Alternative Name (SAN) entries of type dNSName if the SAN
+ * extension is present; otherwise against CN components of the
+ * Subject. (This behaviour was present in RFC 6125, removed in
+ * RFC 9525, but we preserve it for improved compatibility.)
+ *
+ * DNS name matching is case-insensitive. If the certificate
+ * contains a wildcard, it is matched according to RFC 9525:
+ * a single wildcard is allowed and it must be the entire
+ * content of the leftmost label.
+ *
+ * IP addresses must be provided in dotted-quad format for IPv4,
+ * and for IPv6 in the textual representation defined by RFC
+ * 4291 section 2.2. They will be converted to binary and
+ * matched against iPAddress entries in the SAN extension. The
+ * match must be exact (RFC 9525).
+ *
+ * URIs must not be relative and must follow RFC 3986. They will
+ * be matched against uniformResourceIdentifier entries in the
+ * SAN extension. Support for this type is limited: no kind of
+ * parsing or normalization is performed, and only exact
+ * byte-for-byte matches will yield a positive result.
+ *
+ * As a compatibility measure, if the name passed does not look
+ * like a valid IP, URI or DNS, it is treated as a free string
+ * intended to match one of the CN components of the Subject.
+ * The match must then be byte-for-byte.
+ *
+ * \note The \p f_vrfy callback is a user-supplied callback that
* can clear / modify / add flags for a certificate. If set,
* the verification callback is called for each
* certificate in the chain (from the trust-ca down to the
@@ -640,20 +680,19 @@ int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
* \param crt The certificate chain to be verified.
* \param trust_ca The list of trusted CAs.
* \param ca_crl The list of CRLs for trusted CAs.
- * \param cn The expected Common Name. This will be checked to be
- * present in the certificate's subjectAltNames extension or,
- * if this extension is absent, as a CN component in its
- * Subject name. DNS names and IP addresses are fully
- * supported, while the URI subtype is partially supported:
- * only exact matching, without any normalization procedures
- * described in 7.4 of RFC5280, will result in a positive
- * URI verification.
+ * See the warning on mbedtls_x509_crt_is_revoked() regarding
+ * CA name matching in CRL.
+ * \param cn The expected Common Name or reference identifier.
* This may be \c NULL if the CN need not be verified.
+ * Otherwise, must be a DNS hostname, an IP address, or a URI.
+ * See note above for expected formats and matching rules.
+ * If this input is not in the expected format or not one of the
+ * supported types, the behaviour is unspecified.
* \param flags The address at which to store the result of the verification.
* If the verification couldn't be completed, the flag value is
* set to (uint32_t) -1.
- * \param f_vrfy The verification callback to use. See the documentation
- * of mbedtls_x509_crt_verify() for more information.
+ * \param f_vrfy The verification callback to use. See note above
+ * for more information.
* \param p_vrfy The context to be passed to \p f_vrfy.
*
* \return \c 0 if the chain is valid with respect to the
@@ -687,9 +726,16 @@ int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
* \param crt The certificate chain to be verified.
* \param trust_ca The list of trusted CAs.
* \param ca_crl The list of CRLs for trusted CAs.
+ * See the warning on mbedtls_x509_crt_is_revoked() regarding
+ * CA name matching in CRL.
* \param profile The security profile to use for the verification.
- * \param cn The expected Common Name. This may be \c NULL if the
- * CN need not be verified.
+ * \param cn The expected Common Name or reference identifier.
+ * This may be \c NULL if the CN need not be verified.
+ * Otherwise, must be a DNS hostname, an IP address, or a URI.
+ * See note on mbedtls_x509_crt_verify() for expected formats
+ * and matching rules.
+ * If this input is not in the expected format or not one of the
+ * supported types, the behaviour is unspecified.
* \param flags The address at which to store the result of the verification.
* If the verification couldn't be completed, the flag value is
* set to (uint32_t) -1.
@@ -716,18 +762,25 @@ int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
void *p_vrfy);
/**
- * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
+ * \brief Restartable version of \c mbedtls_x509_crt_verify_with_profile()
*
- * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
+ * \note Performs the same job as \c mbedtls_x509_crt_verify_with_profile()
* but can return early and restart according to the limit
* set with \c psa_interruptible_set_max_ops() to reduce blocking.
*
* \param crt The certificate chain to be verified.
* \param trust_ca The list of trusted CAs.
* \param ca_crl The list of CRLs for trusted CAs.
+ * See the warning on mbedtls_x509_crt_is_revoked() regarding
+ * CA name matching in CRL.
* \param profile The security profile to use for the verification.
- * \param cn The expected Common Name. This may be \c NULL if the
- * CN need not be verified.
+ * \param cn The expected Common Name or reference identifier.
+ * This may be \c NULL if the CN need not be verified.
+ * Otherwise, must be a DNS hostname, an IP address, or a URI.
+ * See note on mbedtls_x509_crt_verify() for expected formats
+ * and matching rules.
+ * If this input is not in the expected format or not one of the
+ * supported types, the behaviour is unspecified.
* \param flags The address at which to store the result of the verification.
* If the verification couldn't be completed, the flag value is
* set to (uint32_t) -1.
@@ -737,7 +790,7 @@ int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
* \param rs_ctx The restart context to use. This may be set to \c NULL
* to disable restartable ECC.
*
- * \return See \c mbedtls_crt_verify_with_profile(), or
+ * \return See \c mbedtls_x509_crt_verify_with_profile(), or
* \return #PSA_OPERATION_INCOMPLETE if maximum number of
* operations was reached: see \c psa_interruptible_set_max_ops().
*/
@@ -796,8 +849,13 @@ typedef int (*mbedtls_x509_crt_ca_cb_t)(void *p_ctx,
* ::mbedtls_x509_crt_ca_cb_t for more information.
* \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
* \param profile The security profile for the verification.
- * \param cn The expected Common Name. This may be \c NULL if the
- * CN need not be verified.
+ * \param cn The expected Common Name or reference identifier.
+ * This may be \c NULL if the CN need not be verified.
+ * Otherwise, must be a DNS hostname, an IP address, or a URI.
+ * See note on mbedtls_x509_crt_verify() for expected formats
+ * and matching rules.
+ * If this input is not in the expected format or not one of the
+ * supported types, the behaviour is unspecified.
* \param flags The address at which to store the result of the verification.
* If the verification couldn't be completed, the flag value is
* set to (uint32_t) -1.
@@ -805,7 +863,7 @@ typedef int (*mbedtls_x509_crt_ca_cb_t)(void *p_ctx,
* of mbedtls_x509_crt_verify() for more information.
* \param p_vrfy The context to be passed to \p f_vrfy.
*
- * \return See \c mbedtls_crt_verify_with_profile().
+ * \return See \c mbedtls_x509_crt_verify_with_profile().
*/
int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
mbedtls_x509_crt_ca_cb_t f_ca_cb,
@@ -862,6 +920,20 @@ int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
/**
* \brief Verify the certificate revocation status
*
+ * \warning A certificate is only considerd to be revoked if its
+ * issuer name matches an entry in the CRL. Mbed TLS
+ * does not fully normalize the names before comparing
+ * them. It ignores the ASN.1 type (e.g. a PrintableString
+ * can match an UTF8String), and it compares ASCII characters
+ * case-insensitively, but no other normalization is done.
+ * Thus, for example, the following do not match:
+ * - `AUTORITÉ` vs `autorité`
+ * (case insensitivity is restricted to ASCII letters);
+ * - `"autorite\u0301"` vs `"autorit\u00e9"`
+ * (no Unicode canonicalization);
+ * - `two__words` vs `two_words` where `_` is a space
+ * (no space folding).
+ *
* \param crt a certificate to be verified
* \param crl the CRL to verify against
*
diff --git a/thirdparty/mbedtls/library/error.c b/thirdparty/mbedtls/library/error.c
index e2a35e9e5f7..ca5b63916c4 100644
--- a/thirdparty/mbedtls/library/error.c
+++ b/thirdparty/mbedtls/library/error.c
@@ -253,7 +253,7 @@ static const char *mbedtls_high_level_strerr(int error_code)
return( "X509 - Read/write of file failed" );
case -(MBEDTLS_ERR_X509_FATAL_ERROR):
return( "X509 - A fatal error occurred, eg the chain is too long or the vrfy callback failed" );
-#endif /* MBEDTLS_X509_USE_C ||
+#endif /* MBEDTLS_X509_USE_C ||
MBEDTLS_X509_CREATE_C */
#if defined(MBEDTLS_CIPHER_C)
diff --git a/thirdparty/mbedtls/library/mbedtls_check_config.h b/thirdparty/mbedtls/library/mbedtls_check_config.h
index b482771714e..9f2a290944a 100644
--- a/thirdparty/mbedtls/library/mbedtls_check_config.h
+++ b/thirdparty/mbedtls/library/mbedtls_check_config.h
@@ -93,7 +93,7 @@
#endif
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
- ( !defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) || !defined(PSA_WANT_ALG_RSA_OAEP) )
+ ( !defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || !defined(PSA_WANT_ALG_RSA_PSS) )
#error "MBEDTLS_X509_RSASSA_PSS_SUPPORT defined, but not all prerequisites"
#endif
@@ -114,7 +114,7 @@
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
#if !( (defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)) && \
defined(MBEDTLS_X509_CRT_PARSE_C) && \
- ( defined(PSA_HAVE_ALG_ECDSA_SIGN) || defined(PSA_WANT_ALG_RSA_OAEP) ) )
+ ( defined(PSA_HAVE_ALG_ECDSA_SIGN) || defined(PSA_WANT_ALG_RSA_PSS) ) )
#error "MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED defined, but not all prerequisites"
#endif
#endif
diff --git a/thirdparty/mbedtls/library/ssl_client.c b/thirdparty/mbedtls/library/ssl_client.c
index 354fe9b72b8..976e95fa6a9 100644
--- a/thirdparty/mbedtls/library/ssl_client.c
+++ b/thirdparty/mbedtls/library/ssl_client.c
@@ -255,8 +255,7 @@ static int ssl_write_supported_groups_ext(mbedtls_ssl_context *ssl,
if (flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG) {
#if defined(PSA_WANT_ALG_ECDH)
if (mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list) &&
- mbedtls_ssl_get_psa_curve_info_from_tls_id(
- *group_list, NULL, NULL) == PSA_SUCCESS) {
+ (mbedtls_ssl_is_tls_id_supported(*group_list))) {
propose_group = 1;
}
#endif
@@ -271,8 +270,7 @@ static int ssl_write_supported_groups_ext(mbedtls_ssl_context *ssl,
#if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
if ((flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG) &&
mbedtls_ssl_tls12_named_group_is_ecdhe(*group_list) &&
- (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) !=
- MBEDTLS_ECP_DP_NONE)) {
+ (mbedtls_ssl_is_tls_id_supported(*group_list))) {
propose_group = 1;
}
#endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC */
diff --git a/thirdparty/mbedtls/library/ssl_misc.h b/thirdparty/mbedtls/library/ssl_misc.h
index f0ca823f33b..5fff3271925 100644
--- a/thirdparty/mbedtls/library/ssl_misc.h
+++ b/thirdparty/mbedtls/library/ssl_misc.h
@@ -629,6 +629,35 @@ typedef struct {
unsigned char server_handshake_traffic_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
} mbedtls_ssl_tls13_handshake_secrets;
+typedef enum {
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULING_STAGE_NONE = 0,
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULING_STAGE_TRAFFIC_KEYS,
+} mbedtls_ssl_tls12_key_scheduling_stage;
+
+typedef enum {
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULE_INPUT_NONE = 0,
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULE_INPUT_KEY_EXCHANGE = 1 << 0,
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULE_INPUT_RESUMPTION = 1 << 1,
+} mbedtls_ssl_tls12_key_schedule_input;
+
+typedef enum {
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_NONE = 0,
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_EARLY_SECRET,
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_EARLY_DATA,
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_HANDSHAKE,
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_APPLICATION,
+} mbedtls_ssl_tls13_key_scheduling_stage;
+
+typedef enum {
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_NONE = 0,
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_PSK = 1 << 0,
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_EPHEMERAL_PRIVATE = 1 << 1,
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_EPHEMERAL_PEER = 1 << 2,
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_EPHEMERAL_BOTH =
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_EPHEMERAL_PRIVATE |
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_EPHEMERAL_PEER,
+} mbedtls_ssl_tls13_key_exchange_input;
+
/*
* This structure contains the parameters only needed during handshake.
*/
@@ -742,6 +771,11 @@ struct mbedtls_ssl_handshake_params {
#endif
/** selected_group of key_share extension in HelloRetryRequest message. */
uint16_t hrr_selected_group;
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+ uint8_t ch1_sni_name_present;
+ unsigned char *ch1_sni_name;
+ size_t ch1_sni_name_len;
+#endif
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
uint16_t new_session_tickets_count; /*!< number of session tickets */
#endif
@@ -797,6 +831,13 @@ struct mbedtls_ssl_handshake_params {
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
#endif /* MBEDTLS_X509_CRT_PARSE_C */
+#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+ void *sni_p_cache; /*!< context for cache callbacks */
+#if defined(MBEDTLS_SSL_SESSION_TICKETS)
+ void *sni_p_ticket; /*!< context for ticket callbacks */
+#endif /* MBEDTLS_SSL_SESSION_TICKETS */
+#endif /* MBEDTLS_SSL_SRV_C */
+
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
!defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
mbedtls_pk_context peer_pubkey; /*!< The public key from the peer. */
@@ -946,6 +987,10 @@ struct mbedtls_ssl_handshake_params {
unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
/*!< premaster secret */
size_t pmslen; /*!< premaster length */
+
+ mbedtls_ssl_tls12_key_scheduling_stage
+ tls12_key_scheduling_stage_completed;
+ unsigned char tls12_key_schedule_inputs;
#endif
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
@@ -959,6 +1004,13 @@ struct mbedtls_ssl_handshake_params {
/** TLS 1.3 transform for encrypted handshake messages. */
mbedtls_ssl_transform *transform_handshake;
+ /** TLS 1.3 key scheduling progress.
+ *
+ * The early-data, handshake and application stages are reached after
+ * successfully computing the secret, sub-secrets and keys of the stage.
+ */
+ mbedtls_ssl_tls13_key_scheduling_stage tls13_key_scheduling_stage_completed;
+ unsigned char tls13_key_exchange_inputs;
union {
unsigned char early[MBEDTLS_TLS1_3_MD_MAX_SIZE];
unsigned char handshake[MBEDTLS_TLS1_3_MD_MAX_SIZE];
@@ -989,6 +1041,14 @@ struct mbedtls_ssl_handshake_params {
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
};
+#if defined(MBEDTLS_SSL_SRV_C)
+void *mbedtls_ssl_get_session_cache_context(const mbedtls_ssl_context *ssl);
+
+#if defined(MBEDTLS_SSL_SESSION_TICKETS)
+void *mbedtls_ssl_get_session_ticket_context(const mbedtls_ssl_context *ssl);
+#endif /* MBEDTLS_SSL_SESSION_TICKETS */
+#endif /* MBEDTLS_SSL_SRV_C */
+
typedef struct mbedtls_ssl_hs_buffer mbedtls_ssl_hs_buffer;
/*
@@ -1266,6 +1326,13 @@ void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform);
*/
void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl);
+/**
+ * Reinitialize the handshake negotiation state, including the provisional
+ * session, without resetting the record and message layers.
+ */
+MBEDTLS_CHECK_RETURN_CRITICAL
+int mbedtls_ssl_handshake_init(mbedtls_ssl_context *ssl);
+
/* set inbound transform of ssl context */
void mbedtls_ssl_set_inbound_transform(mbedtls_ssl_context *ssl,
mbedtls_ssl_transform *transform);
@@ -1502,10 +1569,6 @@ int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md);
MBEDTLS_CHECK_RETURN_CRITICAL
int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id);
-#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
-MBEDTLS_CHECK_RETURN_CRITICAL
-int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id);
-#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
/**
* \brief Return PSA EC info for the specified TLS ID.
@@ -1528,22 +1591,31 @@ int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
size_t *bits);
/**
- * \brief Return \c mbedtls_ecp_group_id for the specified TLS ID.
+ * \brief Tell if a TLS group ID is supported or not.
*
- * \param tls_id The TLS ID to look for
- * \return Proper \c mbedtls_ecp_group_id if the TLS ID is supported,
- * or MBEDTLS_ECP_DP_NONE otherwise
+ * \param tls_id The TLS ID to look for.
+ * \return 1 if specified TLS ID is supported, 0 otherwise.
*/
-mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id);
+int mbedtls_ssl_is_tls_id_supported(uint16_t tls_id);
/**
- * \brief Return TLS ID for the specified \c mbedtls_ecp_group_id.
+ * \brief Return TLS ID for the specified curve.
*
- * \param grp_id The \c mbedtls_ecp_group_id ID to look for
- * \return Proper TLS ID if the \c mbedtls_ecp_group_id is supported,
+ * \param family The \c psa_ecc_family_t for the TLS ID to look for.
+ * \param bits The bit size for the TLS ID to look for.
+ * \return Proper TLS ID if the specified EC group is supported,
* or 0 otherwise
*/
-uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id);
+uint16_t mbedtls_ssl_get_tls_id_from_curve_info(psa_ecc_family_t family, size_t bits);
+
+/**
+ * \brief Return the number of supported TLS group IDs + 1.
+ *
+ * \return Number of supported TLS group IDs in \c tls_id_match_table + 1.
+ * In other words the returned value concides with the array length
+ * of \c tls_id_match_table.
+ */
+size_t mbedtls_ssl_get_supported_tls_id_count(void);
#if defined(MBEDTLS_DEBUG_C)
/**
@@ -2249,8 +2321,7 @@ static inline int mbedtls_ssl_named_group_is_supported(uint16_t named_group)
{
#if defined(PSA_WANT_ALG_ECDH)
if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group)) {
- if (mbedtls_ssl_get_ecp_group_id_from_tls_id(named_group) !=
- MBEDTLS_ECP_DP_NONE) {
+ if (mbedtls_ssl_is_tls_id_supported(named_group)) {
return 1;
}
}
@@ -2650,6 +2721,9 @@ MBEDTLS_CHECK_RETURN_CRITICAL
int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
const unsigned char *buf,
const unsigned char *end);
+
+MBEDTLS_CHECK_RETURN_CRITICAL
+int mbedtls_ssl_call_sni_callback(mbedtls_ssl_context *ssl);
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
diff --git a/thirdparty/mbedtls/library/ssl_tls.c b/thirdparty/mbedtls/library/ssl_tls.c
index 01955762132..1a687e42a7d 100644
--- a/thirdparty/mbedtls/library/ssl_tls.c
+++ b/thirdparty/mbedtls/library/ssl_tls.c
@@ -338,13 +338,19 @@ static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
size_t out_buf_new_len)
{
int modified = 0;
- size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0, hdr_in = 0;
- size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
+ size_t msg_offset_in = 0, iv_offset_in = 0, len_offset_in = 0, hdr_offset_in = 0;
+ size_t offt_offset_in = 0;
+ int in_offt_live = 0;
+ size_t msg_offset_out = 0, iv_offset_out = 0, len_offset_out = 0, hdr_offset_out = 0;
if (ssl->in_buf != NULL) {
- written_in = ssl->in_msg - ssl->in_buf;
+ msg_offset_in = ssl->in_msg - ssl->in_buf;
iv_offset_in = ssl->in_iv - ssl->in_buf;
len_offset_in = ssl->in_len - ssl->in_buf;
- hdr_in = ssl->in_hdr - ssl->in_buf;
+ hdr_offset_in = ssl->in_hdr - ssl->in_buf;
+ if (ssl->in_offt != NULL) {
+ offt_offset_in = ssl->in_offt - ssl->in_buf;
+ in_offt_live = 1;
+ }
if (downsizing ?
ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
ssl->in_buf_len < in_buf_new_len) {
@@ -359,9 +365,10 @@ static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
}
if (ssl->out_buf != NULL) {
- written_out = ssl->out_msg - ssl->out_buf;
+ msg_offset_out = ssl->out_msg - ssl->out_buf;
iv_offset_out = ssl->out_iv - ssl->out_buf;
len_offset_out = ssl->out_len - ssl->out_buf;
+ hdr_offset_out = ssl->out_hdr - ssl->out_buf;
if (downsizing ?
ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
ssl->out_buf_len < out_buf_new_len) {
@@ -376,19 +383,26 @@ static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
}
if (modified) {
/* Update pointers here to avoid doing it twice. */
- ssl->in_hdr = ssl->in_buf + hdr_in;
+ ssl->in_hdr = ssl->in_buf + hdr_offset_in;
mbedtls_ssl_update_in_pointers(ssl);
mbedtls_ssl_reset_out_pointers(ssl);
/* Fields below might not be properly updated with record
* splitting or with CID, so they are manually updated here. */
- ssl->out_msg = ssl->out_buf + written_out;
+ ssl->out_msg = ssl->out_buf + msg_offset_out;
ssl->out_len = ssl->out_buf + len_offset_out;
ssl->out_iv = ssl->out_buf + iv_offset_out;
+ ssl->out_hdr = ssl->out_buf + hdr_offset_out;
- ssl->in_msg = ssl->in_buf + written_in;
+ ssl->in_msg = ssl->in_buf + msg_offset_in;
ssl->in_len = ssl->in_buf + len_offset_in;
ssl->in_iv = ssl->in_buf + iv_offset_in;
+ /* mbedtls_ssl_update_in_pointers() doesn't change ssl->in_offt this implies
+ * 1. Updating ssl->in_offt might be needed
+ * 2. The condition below correctly tests the pre-resize value */
+ if (in_offt_live) {
+ ssl->in_offt = ssl->in_buf + offt_offset_in;
+ }
}
}
#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
@@ -413,7 +427,8 @@ typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
MBEDTLS_CHECK_RETURN_CRITICAL
static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
int ciphersuite,
- const unsigned char master[48],
+ const unsigned char master[
+ MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN],
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
int encrypt_then_mac,
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
@@ -966,7 +981,7 @@ void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
}
MBEDTLS_CHECK_RETURN_CRITICAL
-static int ssl_handshake_init(mbedtls_ssl_context *ssl)
+int mbedtls_ssl_handshake_init(mbedtls_ssl_context *ssl)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
@@ -1222,7 +1237,7 @@ int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
#endif
- if ((ret = ssl_handshake_init(ssl)) != 0) {
+ if ((ret = mbedtls_ssl_handshake_init(ssl)) != 0) {
goto error;
}
@@ -1402,7 +1417,7 @@ int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
}
#endif
- if ((ret = ssl_handshake_init(ssl)) != 0) {
+ if ((ret = mbedtls_ssl_handshake_init(ssl)) != 0) {
return ret;
}
@@ -1529,6 +1544,25 @@ void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
conf->f_get_cache = f_get_cache;
conf->f_set_cache = f_set_cache;
}
+
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+void mbedtls_ssl_set_hs_session_cache_context(mbedtls_ssl_context *ssl,
+ void *p_cache)
+{
+ ssl->handshake->sni_p_cache = p_cache;
+}
+#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
+
+void *mbedtls_ssl_get_session_cache_context(const mbedtls_ssl_context *ssl)
+{
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+ if (ssl->handshake != NULL && ssl->handshake->sni_p_cache != NULL) {
+ return ssl->handshake->sni_p_cache;
+ }
+#endif
+
+ return ssl->conf->p_cache;
+}
#endif /* MBEDTLS_SSL_SRV_C */
#if defined(MBEDTLS_SSL_CLI_C)
@@ -2710,6 +2744,25 @@ void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
conf->f_ticket_parse = f_ticket_parse;
conf->p_ticket = p_ticket;
}
+
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+void mbedtls_ssl_set_hs_session_ticket_context(mbedtls_ssl_context *ssl,
+ void *p_ticket)
+{
+ ssl->handshake->sni_p_ticket = p_ticket;
+}
+#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
+
+void *mbedtls_ssl_get_session_ticket_context(const mbedtls_ssl_context *ssl)
+{
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+ if (ssl->handshake != NULL && ssl->handshake->sni_p_ticket != NULL) {
+ return ssl->handshake->sni_p_ticket;
+ }
+#endif
+
+ return ssl->conf->p_ticket;
+}
#endif
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
@@ -3100,7 +3153,7 @@ static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
*/
used += 1 /* id_len */
+ sizeof(session->id)
- + sizeof(session->master)
+ + MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN
+ 4; /* verify_result */
if (used <= buf_len) {
@@ -3108,8 +3161,8 @@ static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
memcpy(p, session->id, 32);
p += 32;
- memcpy(p, session->master, 48);
- p += 48;
+ memcpy(p, session->secret, MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN);
+ p += MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN;
MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
p += 4;
@@ -3249,16 +3302,19 @@ static int ssl_tls12_session_load(mbedtls_ssl_session *session,
/*
* Basic mandatory fields
*/
- if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
+ if (1 + 32 + MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN + 4 > (size_t) (end - p)) {
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
}
session->id_len = *p++;
+ if (session->id_len > sizeof(session->id)) {
+ return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
+ }
memcpy(session->id, p, 32);
p += 32;
- memcpy(session->master, p, 48);
- p += 48;
+ memcpy(session->secret, p, MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN);
+ p += MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN;
session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
p += 4;
@@ -3510,7 +3566,7 @@ static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
/* save resumption_key */
p[5] = session->resumption_key_len;
p += 6;
- memcpy(p, session->resumption_key, session->resumption_key_len);
+ memcpy(p, session->secret, session->resumption_key_len);
p += session->resumption_key_len;
#if defined(MBEDTLS_SSL_EARLY_DATA)
@@ -3595,10 +3651,11 @@ static int ssl_tls13_session_load(mbedtls_ssl_session *session,
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
}
- if (sizeof(session->resumption_key) < session->resumption_key_len) {
+ if (MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN <
+ session->resumption_key_len) {
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
}
- memcpy(session->resumption_key, p, session->resumption_key_len);
+ memcpy(session->secret, p, session->resumption_key_len);
p += session->resumption_key_len;
#if defined(MBEDTLS_SSL_EARLY_DATA)
@@ -4353,7 +4410,7 @@ int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
- if ((ret = ssl_handshake_init(ssl)) != 0) {
+ if ((ret = mbedtls_ssl_handshake_init(ssl)) != 0) {
return ret;
}
@@ -4507,6 +4564,14 @@ void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
}
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
+#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SRV_C) && \
+ defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+ mbedtls_free(handshake->ch1_sni_name);
+ handshake->ch1_sni_name = NULL;
+ handshake->ch1_sni_name_len = 0;
+ handshake->ch1_sni_name_present = 0;
+#endif
+
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
/*
@@ -4949,7 +5014,7 @@ static int ssl_context_load(mbedtls_ssl_context *ssl,
session_len = MBEDTLS_GET_UINT32_BE(p, 0);
p += 4;
- /* This has been allocated by ssl_handshake_init(), called by
+ /* This has been allocated by mbedtls_ssl_handshake_init(), called by
* by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
ssl->session = ssl->session_negotiate;
ssl->session_in = ssl->session;
@@ -4972,7 +5037,7 @@ static int ssl_context_load(mbedtls_ssl_context *ssl,
* Transform
*/
- /* This has been allocated by ssl_handshake_init(), called by
+ /* This has been allocated by mbedtls_ssl_handshake_init(), called by
* by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
ssl->transform = ssl->transform_negotiate;
@@ -4994,7 +5059,7 @@ static int ssl_context_load(mbedtls_ssl_context *ssl,
ret = ssl_tls12_populate_transform(ssl->transform,
ssl->session->ciphersuite,
- ssl->session->master,
+ ssl->session->secret,
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
ssl->session->encrypt_then_mac,
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
@@ -5098,6 +5163,10 @@ static int ssl_context_load(mbedtls_ssl_context *ssl,
alpn_len = *p++;
+ if ((size_t) (end - p) < alpn_len) {
+ return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
+ }
+
if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
/* alpn_chosen should point to an item in the configured list */
for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
@@ -5793,57 +5862,40 @@ int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_
return -1;
}
-#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
-/*
- * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
- */
-int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
-{
- uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
-
- if (tls_id == 0) {
- return -1;
- }
-
- return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
-}
-#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
-
static const struct {
uint16_t tls_id;
- mbedtls_ecp_group_id ecp_group_id;
psa_ecc_family_t psa_family;
uint16_t bits;
} tls_id_match_table[] =
{
#if defined(PSA_WANT_ECC_SECP_R1_521)
- { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
+ { 25, PSA_ECC_FAMILY_SECP_R1, 521 },
#endif
#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
- { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
+ { 28, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
#endif
#if defined(PSA_WANT_ECC_SECP_R1_384)
- { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
+ { 24, PSA_ECC_FAMILY_SECP_R1, 384 },
#endif
#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
- { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
+ { 27, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
#endif
#if defined(PSA_WANT_ECC_SECP_R1_256)
- { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
+ { 23, PSA_ECC_FAMILY_SECP_R1, 256 },
#endif
#if defined(PSA_WANT_ECC_SECP_K1_256)
- { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
+ { 22, PSA_ECC_FAMILY_SECP_K1, 256 },
#endif
#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
- { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
+ { 26, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
#endif
#if defined(PSA_WANT_ECC_MONTGOMERY_255)
- { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
+ { 29, PSA_ECC_FAMILY_MONTGOMERY, 255 },
#endif
#if defined(PSA_WANT_ECC_MONTGOMERY_448)
- { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
+ { 30, PSA_ECC_FAMILY_MONTGOMERY, 448 },
#endif
- { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
+ { 0, 0, 0 },
};
int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
@@ -5865,22 +5917,22 @@ int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
return PSA_ERROR_NOT_SUPPORTED;
}
-mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
+int mbedtls_ssl_is_tls_id_supported(uint16_t tls_id)
{
for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
if (tls_id_match_table[i].tls_id == tls_id) {
- return tls_id_match_table[i].ecp_group_id;
+ return 1;
}
}
- return MBEDTLS_ECP_DP_NONE;
+ return 0;
}
-uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
+uint16_t mbedtls_ssl_get_tls_id_from_curve_info(psa_ecc_family_t family, size_t bits)
{
- for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
- i++) {
- if (tls_id_match_table[i].ecp_group_id == grp_id) {
+ for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
+ if ((tls_id_match_table[i].psa_family == family) &&
+ (tls_id_match_table[i].bits == bits)) {
return tls_id_match_table[i].tls_id;
}
}
@@ -5888,6 +5940,11 @@ uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
return 0;
}
+size_t mbedtls_ssl_get_supported_tls_id_count(void)
+{
+ return ARRAY_LENGTH(tls_id_match_table);
+}
+
#if defined(MBEDTLS_DEBUG_C)
mbedtls_ssl_iana_tls_group_info_t mbedtls_ssl_iana_tls_group_info[] =
MBEDTLS_SSL_IANA_TLS_GROUPS_INFO;
@@ -6286,7 +6343,7 @@ static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
/* cf. RFC 5246, Section 8.1:
* "The master secret is always exactly 48 bytes in length." */
- size_t const master_secret_len = 48;
+ size_t const master_secret_len = MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN;
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
unsigned char session_hash[48];
@@ -6466,6 +6523,11 @@ static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
}
}
#endif
+ if (handshake->pmslen == 0) {
+ MBEDTLS_SSL_DEBUG_MSG(1, ("premaster secret not available"));
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
+
ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
lbl, seed, seed_len,
master,
@@ -6489,11 +6551,26 @@ static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
+ unsigned char required_input;
const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
ssl->handshake->ciphersuite_info;
MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
+ if (ssl->handshake->tls12_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULING_STAGE_NONE) {
+ MBEDTLS_SSL_DEBUG_MSG(1, ("invalid TLS 1.2 key schedule transition"));
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
+
+ required_input = ssl->handshake->resume != 0 ?
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULE_INPUT_RESUMPTION :
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULE_INPUT_KEY_EXCHANGE;
+ if (ssl->handshake->tls12_key_schedule_inputs != required_input) {
+ MBEDTLS_SSL_DEBUG_MSG(1, ("invalid TLS 1.2 key schedule input"));
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
+
/* Set PRF, calc_verify and calc_finished function pointers */
ret = ssl_set_handshake_prfs(ssl->handshake,
(mbedtls_md_type_t) ciphersuite_info->mac);
@@ -6504,7 +6581,7 @@ int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
/* Compute master secret if needed */
ret = ssl_compute_master(ssl->handshake,
- ssl->session_negotiate->master,
+ ssl->session_negotiate->secret,
ssl);
if (ret != 0) {
MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
@@ -6525,7 +6602,7 @@ int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
/* Populate transform structure */
ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
ssl->session_negotiate->ciphersuite,
- ssl->session_negotiate->master,
+ ssl->session_negotiate->secret,
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
ssl->session_negotiate->encrypt_then_mac,
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
@@ -6539,6 +6616,9 @@ int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
return ret;
}
+ ssl->handshake->tls12_key_scheduling_stage_completed =
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULING_STAGE_TRAFFIC_KEYS;
+
/* We no longer need Server/ClientHello.random values */
mbedtls_platform_zeroize(ssl->handshake->randbytes,
sizeof(ssl->handshake->randbytes));
@@ -7281,7 +7361,8 @@ static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
* hash = PRF( master, finished_label,
* Hash( handshake ) )[0.11]
*/
- ssl->handshake->tls_prf(session->master, 48, sender,
+ ssl->handshake->tls_prf(session->secret,
+ MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN, sender,
padbuf, hlen, buf, len);
MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
@@ -7346,8 +7427,6 @@ void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
{
- int resume = ssl->handshake->resume;
-
MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
#if defined(MBEDTLS_SSL_RENEGOTIATION)
@@ -7376,16 +7455,16 @@ void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
/*
* Add cache entry
*/
+#if defined(MBEDTLS_SSL_SRV_C)
if (ssl->conf->f_set_cache != NULL &&
ssl->session->id_len != 0 &&
- resume == 0) {
- if (ssl->conf->f_set_cache(ssl->conf->p_cache,
- ssl->session->id,
- ssl->session->id_len,
- ssl->session) != 0) {
- MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
- }
+ ssl->handshake->resume == 0 &&
+ ssl->conf->f_set_cache(mbedtls_ssl_get_session_cache_context(ssl),
+ ssl->session->id, ssl->session->id_len,
+ ssl->session) != 0) {
+ MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
}
+#endif /* MBEDTLS_SSL_SRV_C */
#if defined(MBEDTLS_SSL_PROTO_DTLS)
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
@@ -7673,7 +7752,8 @@ static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
MBEDTLS_CHECK_RETURN_CRITICAL
static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
int ciphersuite,
- const unsigned char master[48],
+ const unsigned char master[
+ MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN],
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
int encrypt_then_mac,
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
@@ -7782,7 +7862,8 @@ static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
/*
* Compute key block using the PRF
*/
- ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
+ ret = tls_prf(master, MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN, "key expansion",
+ randbytes, 64, keyblk, 256);
if (ret != 0) {
MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
return ret;
@@ -7790,7 +7871,8 @@ static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
- MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
+ MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master,
+ MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN);
MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
@@ -7929,7 +8011,7 @@ static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
if (ssl->f_export_keys != NULL) {
ssl->f_export_keys(ssl->p_export_keys,
MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
- master, 48,
+ master, MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN,
randbytes + 32,
randbytes,
tls_prf_get_type(tls_prf));
@@ -8032,6 +8114,9 @@ int mbedtls_psa_ecjpake_read_round(
step <= PSA_PAKE_STEP_ZK_PROOF;
++step) {
/* Length is stored at the first byte */
+ if (input_offset + 1 > len) {
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
+ }
size_t length = buf[input_offset];
input_offset += 1;
@@ -8079,6 +8164,10 @@ int mbedtls_psa_ecjpake_write_round(
* For each step, prepend 1 byte with the length of the data as
* given by psa_pake_output().
*/
+ if (output_offset + 1 > len) {
+ return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
+ }
+
status = psa_pake_output(pake_ctx, step,
buf + output_offset + 1,
len - output_offset - 1,
@@ -8383,7 +8472,6 @@ int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
const unsigned char *buf,
const unsigned char *end)
{
- int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
const unsigned char *p = buf;
size_t server_name_list_len, hostname_len;
const unsigned char *server_name_list_end;
@@ -8403,6 +8491,29 @@ int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
hostname_len + 3);
if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
+ if (hostname_len == 0) {
+ MBEDTLS_SSL_DEBUG_MSG(
+ 1, ("Invalid zero-length ServerName hostname"));
+ MBEDTLS_SSL_PEND_FATAL_ALERT(
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
+ MBEDTLS_ERR_SSL_DECODE_ERROR);
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
+ }
+ /*
+ * RFC 6066 requires host_name to contain an actual DNS hostname in
+ * ASCII form, without a trailing dot. The limit below therefore does
+ * not exclude any valid host_name, although the TLS vector encoding
+ * itself permits a larger value.
+ */
+ if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
+ MBEDTLS_SSL_DEBUG_MSG(
+ 1, ("Invalid ServerName hostname length"));
+ MBEDTLS_SSL_PEND_FATAL_ALERT(
+ MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
+ MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
+ }
+
/* sni_name is intended to be used only during the parsing of the
* ClientHello message (it is reset to NULL before the end of
* the message parsing). Thus it is ok to just point to the
@@ -8410,17 +8521,6 @@ int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
*/
ssl->handshake->sni_name = p + 3;
ssl->handshake->sni_name_len = hostname_len;
- if (ssl->conf->f_sni == NULL) {
- return 0;
- }
- ret = ssl->conf->f_sni(ssl->conf->p_sni,
- ssl, p + 3, hostname_len);
- if (ret != 0) {
- MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
- MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
- MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
- return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
- }
return 0;
}
@@ -8429,6 +8529,28 @@ int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
return 0;
}
+
+MBEDTLS_CHECK_RETURN_CRITICAL
+int mbedtls_ssl_call_sni_callback(mbedtls_ssl_context *ssl)
+{
+ int ret;
+
+ if (ssl->handshake->sni_name == NULL || ssl->conf->f_sni == NULL) {
+ return 0;
+ }
+
+ ret = ssl->conf->f_sni(ssl->conf->p_sni, ssl,
+ ssl->handshake->sni_name,
+ ssl->handshake->sni_name_len);
+ if (ret != 0) {
+ MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
+ MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
+ MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
+ return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
+ }
+
+ return 0;
+}
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
#if defined(MBEDTLS_SSL_ALPN)
@@ -8827,8 +8949,13 @@ int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
- PSA_KEY_TYPE_IS_ECC(mbedtls_pk_get_type(&chain->pk))) {
- if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
+ PSA_KEY_TYPE_IS_ECC(mbedtls_pk_get_key_type(&chain->pk))) {
+ psa_key_type_t key_type = mbedtls_pk_get_key_type(&chain->pk);
+ psa_ecc_family_t ec_family = PSA_KEY_TYPE_ECC_GET_FAMILY(key_type);
+ size_t bits = mbedtls_pk_get_bitlen(&chain->pk);
+ int tls_id = mbedtls_ssl_get_tls_id_from_curve_info(ec_family, bits);
+
+ if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
if (ret == 0) {
@@ -8962,7 +9089,8 @@ static int mbedtls_ssl_tls12_export_keying_material(const mbedtls_ssl_context *s
MBEDTLS_PUT_UINT16_BE(context_len, prf_input, randbytes_len);
memcpy(prf_input + randbytes_len + sizeof(uint16_t), context, context_len);
}
- ret = tls_prf_generic(hash_alg, ssl->session->master, sizeof(ssl->session->master),
+ ret = tls_prf_generic(hash_alg, ssl->session->secret,
+ MBEDTLS_SSL_TLS1_2_MASTER_SECRET_LEN,
label, label_len,
prf_input, prf_input_len,
out, key_len);
diff --git a/thirdparty/mbedtls/library/ssl_tls12_client.c b/thirdparty/mbedtls/library/ssl_tls12_client.c
index 8799287a094..fc7eb36d032 100644
--- a/thirdparty/mbedtls/library/ssl_tls12_client.c
+++ b/thirdparty/mbedtls/library/ssl_tls12_client.c
@@ -1093,7 +1093,6 @@ static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
-
return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
}
@@ -1140,6 +1139,7 @@ static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
int ret, i;
size_t n;
size_t ext_len;
+ mbedtls_ssl_protocol_version server_version;
unsigned char *buf, *ext;
unsigned char comp;
#if defined(MBEDTLS_SSL_RENEGOTIATION)
@@ -1223,26 +1223,31 @@ static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
buf += mbedtls_ssl_hs_hdr_len(ssl);
MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
- ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf,
- ssl->conf->transport);
- ssl->session_negotiate->tls_version = ssl->tls_version;
- ssl->session_negotiate->endpoint = ssl->conf->endpoint;
-
- if (ssl->tls_version < ssl->conf->min_tls_version ||
- ssl->tls_version > ssl->conf->max_tls_version) {
- MBEDTLS_SSL_DEBUG_MSG(1,
- (
- "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
- (unsigned) ssl->conf->min_tls_version,
- (unsigned) ssl->tls_version,
- (unsigned) ssl->conf->max_tls_version));
-
+ server_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(
+ buf, ssl->conf->transport);
+
+ if (server_version != MBEDTLS_SSL_VERSION_TLS1_2) {
+ MBEDTLS_SSL_DEBUG_MSG(
+ 1,
+ ("ServerHello legacy_version is not TLS 1.2 - legacy_version: [0x%x]",
+ (unsigned) server_version));
+
+ /* Temporarily use the server's version to encode an alert
+ * that the server can parse. */
+ if (server_version < MBEDTLS_SSL_VERSION_TLS1_2) {
+ ssl->tls_version = server_version;
+ }
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
+ /* Restore the TLS version under negotiation. */
+ ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
}
+ ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
+ ssl->session_negotiate->endpoint = MBEDTLS_SSL_IS_CLIENT;
+
MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
((unsigned long) buf[2] << 24) |
((unsigned long) buf[3] << 16) |
@@ -1337,6 +1342,8 @@ static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
ssl->session_negotiate->id_len = n;
memcpy(ssl->session_negotiate->id, buf + 35, n);
} else {
+ ssl->handshake->tls12_key_schedule_inputs |=
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULE_INPUT_RESUMPTION;
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC);
}
@@ -1934,8 +1941,7 @@ start_processing:
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 3);
uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
- uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
- MBEDTLS_ECP_DP_SECP256R1);
+ uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_curve_info(PSA_ECC_FAMILY_SECP_R1, 256);
if (exp_tls_id == 0) {
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
@@ -2218,7 +2224,7 @@ static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
buf = ssl->in_msg;
/* certificate_types */
- if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
+ if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 1) {
MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
@@ -2228,16 +2234,10 @@ static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
n = cert_type_len;
/*
- * In the subsequent code there are two paths that read from buf:
- * * the length of the signature algorithms field (if minor version of
- * SSL is 3),
- * * distinguished name length otherwise.
- * Both reach at most the index:
- * ...hdr_len + 2 + n,
- * therefore the buffer length at this point must be greater than that
- * regardless of the actual code path.
+ * The two-byte supported_signature_algorithms length is read next,
+ * at offset ...hdr_len + 1 + n.
*/
- if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
+ if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 1 + n + 2) {
MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
@@ -2248,18 +2248,13 @@ static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
/*
- * The furthest access in buf is in the loop few lines below:
- * sig_alg[i + 1],
- * where:
- * sig_alg = buf + ...hdr_len + 3 + n,
- * max(i) = sig_alg_len - 1.
- * Therefore the furthest access is:
- * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
- * which reduces to:
- * buf[...hdr_len + 3 + n + sig_alg_len],
- * which is one less than we need the buf to be.
+ * The list is a sequence of two-byte values, so its length must be
+ * even. The message must have room for the list itself, plus the
+ * two-byte certificate_authorities length that follows it and is
+ * read below.
*/
- if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
+ if (sig_alg_len % 2 != 0 ||
+ ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 1 + n + 2 + sig_alg_len + 2) {
MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
mbedtls_ssl_send_alert_message(
ssl,
@@ -2646,6 +2641,9 @@ static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
}
+ ssl->handshake->tls12_key_schedule_inputs |=
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULE_INPUT_KEY_EXCHANGE;
+
ssl->out_msglen = header_len + content_len;
ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
@@ -3012,6 +3010,10 @@ int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
break;
case MBEDTLS_SSL_CLIENT_FINISHED:
+ if (ssl->handshake->tls12_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULING_STAGE_TRAFFIC_KEYS) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
ret = mbedtls_ssl_write_finished(ssl);
break;
@@ -3031,6 +3033,10 @@ int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
break;
case MBEDTLS_SSL_SERVER_FINISHED:
+ if (ssl->handshake->tls12_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULING_STAGE_TRAFFIC_KEYS) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
ret = mbedtls_ssl_parse_finished(ssl);
break;
@@ -3040,6 +3046,10 @@ int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
break;
case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
+ if (ssl->handshake->tls12_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULING_STAGE_TRAFFIC_KEYS) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
mbedtls_ssl_handshake_wrapup(ssl);
break;
diff --git a/thirdparty/mbedtls/library/ssl_tls12_server.c b/thirdparty/mbedtls/library/ssl_tls12_server.c
index 2a825e07b15..40117a33a2d 100644
--- a/thirdparty/mbedtls/library/ssl_tls12_server.c
+++ b/thirdparty/mbedtls/library/ssl_tls12_server.c
@@ -13,6 +13,7 @@
#include "mbedtls/ssl.h"
#include "debug_internal.h"
+#include "ssl_debug_helpers.h"
#include "mbedtls/error.h"
#include "mbedtls/platform_util.h"
#include "mbedtls/constant_time.h"
@@ -202,8 +203,8 @@ static int ssl_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
/* Don't allow our peer to make us allocate too much memory,
* and leave room for a final 0 */
our_size = list_size / 2 + 1;
- if (our_size > MBEDTLS_ECP_DP_MAX) {
- our_size = MBEDTLS_ECP_DP_MAX;
+ if (our_size > mbedtls_ssl_get_supported_tls_id_count()) {
+ our_size = mbedtls_ssl_get_supported_tls_id_count();
}
if ((curves_tls_id = mbedtls_calloc(our_size,
@@ -219,8 +220,7 @@ static int ssl_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
while (list_size > 0 && our_size > 1) {
uint16_t curr_tls_id = MBEDTLS_GET_UINT16_BE(p, 0);
- if (mbedtls_ssl_get_ecp_group_id_from_tls_id(curr_tls_id) !=
- MBEDTLS_ECP_DP_NONE) {
+ if (mbedtls_ssl_is_tls_id_supported(curr_tls_id)) {
*curves_tls_id++ = curr_tls_id;
our_size--;
}
@@ -466,10 +466,9 @@ static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
/*
* Failures are ok: just ignore the ticket and proceed.
*/
- if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket, &session,
- buf, len)) != 0) {
- mbedtls_ssl_session_free(&session);
-
+ ret = ssl->conf->f_ticket_parse(mbedtls_ssl_get_session_ticket_context(ssl),
+ &session, buf, len);
+ if (ret != 0) {
if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
} else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
@@ -478,7 +477,12 @@ static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_parse", ret);
}
- return 0;
+ goto cleanup;
+ }
+
+ if (session.tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
+ MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.2."));
+ goto cleanup;
}
/*
@@ -496,12 +500,18 @@ static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from ticket"));
+ ssl->handshake->tls12_key_schedule_inputs |=
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULE_INPUT_RESUMPTION;
ssl->handshake->resume = 1;
/* Don't send a new ticket after all, this one is OK */
ssl->handshake->new_session_ticket = 0;
return 0;
+
+cleanup:
+ mbedtls_ssl_session_free(&session);
+ return 0;
}
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
@@ -630,12 +640,24 @@ static int ssl_check_key_curve(mbedtls_pk_context *pk,
uint16_t *curves_tls_id)
{
uint16_t *curr_tls_id = curves_tls_id;
- mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(pk);
- mbedtls_ecp_group_id curr_grp_id;
+ psa_key_type_t key_type = mbedtls_pk_get_key_type(pk);
+ size_t key_bits = mbedtls_pk_get_bitlen(pk);
+ psa_key_type_t curr_key_type;
+ size_t curr_key_bits;
+ psa_status_t status;
while (*curr_tls_id != 0) {
- curr_grp_id = mbedtls_ssl_get_ecp_group_id_from_tls_id(*curr_tls_id);
- if (curr_grp_id == grp_id) {
+ status = mbedtls_ssl_get_psa_curve_info_from_tls_id(*curr_tls_id,
+ &curr_key_type, &curr_key_bits);
+ if (status != PSA_SUCCESS) {
+ /* This TLS group ID is not supported. Move to the next one. */
+ curr_tls_id++;
+ continue;
+ }
+ /* Check if EC family type and key bits for the current TLS group ID are the
+ * same as the ones in the provided PK context. */
+ if ((PSA_KEY_TYPE_ECC_GET_FAMILY(key_type) == PSA_KEY_TYPE_ECC_GET_FAMILY(curr_key_type)) &&
+ (key_bits == curr_key_bits)) {
return 0;
}
curr_tls_id++;
@@ -846,6 +868,7 @@ MBEDTLS_CHECK_RETURN_CRITICAL
static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
{
int ret, got_common_suite;
+ mbedtls_ssl_protocol_version client_version;
size_t i, j;
size_t ciph_offset, comp_offset, ext_offset;
size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
@@ -980,18 +1003,31 @@ static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
*/
MBEDTLS_SSL_DEBUG_BUF(3, "client hello, version", buf, 2);
- ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf,
- ssl->conf->transport);
- ssl->session_negotiate->tls_version = ssl->tls_version;
- ssl->session_negotiate->endpoint = ssl->conf->endpoint;
+ client_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(
+ buf, ssl->conf->transport);
- if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
- MBEDTLS_SSL_DEBUG_MSG(1, ("server only supports TLS 1.2"));
+ if (client_version != MBEDTLS_SSL_VERSION_TLS1_2) {
+ MBEDTLS_SSL_DEBUG_MSG(
+ 1,
+ ("ClientHello legacy_version is not TLS 1.2 - legacy_version: [0x%x]",
+ (unsigned) client_version));
+
+ /* Temporarily use the client's version to encode an alert
+ * that the client can parse. */
+ if (client_version < MBEDTLS_SSL_VERSION_TLS1_2) {
+ ssl->tls_version = client_version;
+ }
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
+
+ /* Restore the TLS version under negotiation. */
+ ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
}
+ ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
+ ssl->session_negotiate->endpoint = MBEDTLS_SSL_IS_SERVER;
+
/*
* Save client random (inc. Unix time)
*/
@@ -1005,7 +1041,7 @@ static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
sess_len = buf[34];
if (sess_len > sizeof(ssl->session_negotiate->id) ||
- sess_len + 34 + 2 > msg_len) { /* 2 for cipherlist length field */
+ sess_len + 35 > msg_len) {
MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
@@ -1026,9 +1062,16 @@ static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
#if defined(MBEDTLS_SSL_PROTO_DTLS)
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
cookie_offset = 35 + sess_len;
+
+ if (cookie_offset + 1 > msg_len) {
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
+ }
cookie_len = buf[cookie_offset];
- if (cookie_offset + 1 + cookie_len + 2 > msg_len) {
+ if (cookie_offset + 1 + cookie_len > msg_len) {
MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
@@ -1074,6 +1117,16 @@ static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
#endif /* MBEDTLS_SSL_PROTO_DTLS */
ciph_offset = 35 + sess_len;
+ /* The two-byte ciphersuite list length field must be within the
+ * message: the checks above only guarantee that the fields before it
+ * (session id, and for DTLS the cookie) are. */
+ if (ciph_offset + 2 > msg_len) {
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
+ }
+
ciph_len = MBEDTLS_GET_UINT16_BE(buf, ciph_offset);
if (ciph_len < 2 ||
@@ -1136,6 +1189,10 @@ static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
ext = buf + ext_offset + 2;
MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", ext, ext_len);
+#if defined(MBEDTLS_SSL_SESSION_TICKETS)
+ unsigned char *session_ticket_ext = NULL;
+ unsigned int session_ticket_ext_size = 0;
+#endif
while (ext_len != 0) {
unsigned int ext_id;
@@ -1164,6 +1221,10 @@ static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
if (ret != 0) {
return ret;
}
+ ret = mbedtls_ssl_call_sni_callback(ssl);
+ if (ret != 0) {
+ return ret;
+ }
break;
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
@@ -1276,14 +1337,13 @@ static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
case MBEDTLS_TLS_EXT_SESSION_TICKET:
MBEDTLS_SSL_DEBUG_MSG(3, ("found session ticket extension"));
/*
- * If the Mbed TLS ssl_ticket.c implementation is used, the
- * ticket is decrypted in place. This modifies the ClientHello
- * message in the input buffer.
+ * Postpone parsing of the session ticket extension. The
+ * ticket context used for parsing the ticket may need to be
+ * selected based on the target host. When required, this context
+ * is selected while parsing the Server Name extension.
*/
- ret = ssl_parse_session_ticket_ext(ssl, ext + 4, ext_size);
- if (ret != 0) {
- return ret;
- }
+ session_ticket_ext = ext + 4;
+ session_ticket_ext_size = ext_size;
break;
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
@@ -1319,6 +1379,21 @@ static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
ext += 4 + ext_size;
}
+#if defined(MBEDTLS_SSL_SESSION_TICKETS)
+ /*
+ * If the Mbed TLS ssl_ticket.c implementation is used, the
+ * ticket is decrypted in place. This modifies the ClientHello
+ * message in the input buffer.
+ */
+ if (session_ticket_ext != NULL) {
+ ret = ssl_parse_session_ticket_ext(ssl, session_ticket_ext,
+ session_ticket_ext_size);
+ if (ret != 0) {
+ return ret;
+ }
+ }
+#endif
+
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
/*
@@ -1928,7 +2003,7 @@ static void ssl_handle_id_based_session_resumption(mbedtls_ssl_context *ssl)
mbedtls_ssl_session session_tmp;
mbedtls_ssl_session * const session = ssl->session_negotiate;
- /* Resume is 0 by default, see ssl_handshake_init().
+ /* Resume is 0 by default, see mbedtls_ssl_handshake_init().
* It may be already set to 1 by ssl_parse_session_ticket_ext(). */
if (ssl->handshake->resume == 1) {
return;
@@ -1947,10 +2022,8 @@ static void ssl_handle_id_based_session_resumption(mbedtls_ssl_context *ssl)
mbedtls_ssl_session_init(&session_tmp);
- ret = ssl->conf->f_get_cache(ssl->conf->p_cache,
- session->id,
- session->id_len,
- &session_tmp);
+ ret = ssl->conf->f_get_cache(mbedtls_ssl_get_session_cache_context(ssl),
+ session->id, session->id_len, &session_tmp);
if (ret != 0) {
goto exit;
}
@@ -1966,6 +2039,8 @@ static void ssl_handle_id_based_session_resumption(mbedtls_ssl_context *ssl)
memset(&session_tmp, 0, sizeof(session_tmp));
MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from cache"));
+ ssl->handshake->tls12_key_schedule_inputs |=
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULE_INPUT_RESUMPTION;
ssl->handshake->resume = 1;
exit:
@@ -2486,8 +2561,7 @@ static int ssl_prepare_server_key_exchange(mbedtls_ssl_context *ssl,
* However since we only support secp256r1 for now, we hardcode its
* TLS ID here
*/
- uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
- MBEDTLS_ECP_DP_SECP256R1);
+ uint16_t tls_id = mbedtls_ssl_get_tls_id_from_curve_info(PSA_ECC_FAMILY_SECP_R1, 256);
if (tls_id == 0) {
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
}
@@ -2995,25 +3069,23 @@ static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
- size_t data_len = (size_t) (*p++);
- size_t buf_len = (size_t) (end - p);
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
MBEDTLS_SSL_DEBUG_MSG(3, ("Read the peer's public key."));
- /*
- * We must have at least two bytes (1 for length, at least 1 for data)
- */
- if (buf_len < 2) {
+ if (end - p < 1) {
MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid buffer length: %" MBEDTLS_PRINTF_SIZET,
- buf_len));
+ (size_t) (end - p)));
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
}
- if (data_len < 1 || data_len > buf_len) {
+ size_t data_len = (size_t) (*p++);
+ size_t buf_len = (size_t) (end - p);
+
+ if (data_len < 1 || data_len != buf_len) {
MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid data length: %" MBEDTLS_PRINTF_SIZET
- " > %" MBEDTLS_PRINTF_SIZET,
+ " != %" MBEDTLS_PRINTF_SIZET,
data_len, buf_len));
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
}
@@ -3175,6 +3247,9 @@ static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
}
+ ssl->handshake->tls12_key_schedule_inputs |=
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULE_INPUT_KEY_EXCHANGE;
+
if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
return ret;
@@ -3283,16 +3358,52 @@ static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
}
/*
- * Hash
+ * Check if SignatureAndHashAlgorithm is one of the requested algorithms
*/
- md_alg = mbedtls_ssl_md_alg_from_hash(ssl->in_msg[i]);
+ const uint16_t sig_alg = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i);
+ i += 2;
- if (md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md(ssl, ssl->in_msg[i])) {
+ const uint16_t *req_sig_alg = mbedtls_ssl_get_sig_algs(ssl);
+ if (req_sig_alg == NULL) {
+ // Should never happen
+ MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_ssl_get_sig_algs(ssl) returned NULL"));
+ return MBEDTLS_ERR_SSL_BAD_CONFIG;
+ }
+
+ for (; *req_sig_alg != MBEDTLS_TLS_SIG_NONE; req_sig_alg++) {
+ MBEDTLS_SSL_DEBUG_MSG(3, ("requested sig_alg [%x] %s",
+ *req_sig_alg,
+ mbedtls_ssl_sig_alg_to_str(*req_sig_alg)));
+ if (*req_sig_alg == sig_alg) {
+ break;
+ }
+ }
+
+ MBEDTLS_SSL_DEBUG_MSG(3, ("received sig_alg [%x] %s",
+ sig_alg,
+ mbedtls_ssl_sig_alg_to_str(sig_alg)));
+
+ if (*req_sig_alg == MBEDTLS_TLS_SIG_NONE) {
MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
" for verify message"));
+ MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
+ MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
}
+ /*
+ * Hash
+ */
+ ret = mbedtls_ssl_set_calc_verify_md(ssl, MBEDTLS_BYTE_1(sig_alg));
+ if (ret != 0) {
+ MBEDTLS_SSL_DEBUG_MSG(1, ("peer using unsupported hash algorithm"
+ " for verify message"));
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
+ }
+
+ // Since mbedtls_ssl_set_calc_verify_md() succeeded, this will never be MBEDTLS_MD_NONE.
+ md_alg = mbedtls_ssl_md_alg_from_hash(MBEDTLS_BYTE_1(sig_alg));
+
#if !defined(MBEDTLS_MD_SHA1)
if (MBEDTLS_MD_SHA1 == md_alg) {
hash_start += 16;
@@ -3302,14 +3413,12 @@ static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
/* Info from md_alg will be used instead */
hashlen = 0;
- i++;
-
/*
* Signature
*/
- if ((pk_alg = mbedtls_ssl_pk_sig_alg_from_sig(ssl->in_msg[i]))
+ if ((pk_alg = mbedtls_ssl_pk_sig_alg_from_sig(MBEDTLS_BYTE_0(sig_alg)))
== MBEDTLS_PK_SIGALG_NONE) {
- MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
+ MBEDTLS_SSL_DEBUG_MSG(1, ("peer using unsupported public key algorithm"
" for verify message"));
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
}
@@ -3323,8 +3432,6 @@ static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
}
- i++;
-
if (i + 2 > ssl->in_hslen) {
MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
return MBEDTLS_ERR_SSL_DECODE_ERROR;
@@ -3394,11 +3501,11 @@ static int ssl_write_new_session_ticket(mbedtls_ssl_context *ssl)
#if defined(MBEDTLS_HAVE_TIME)
ssl->session_negotiate->ticket_creation_time = mbedtls_ms_time();
#endif
- if ((ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
- ssl->session_negotiate,
- ssl->out_msg + 10,
- ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
- &tlen, &lifetime)) != 0) {
+ ret = ssl->conf->f_ticket_write(mbedtls_ssl_get_session_ticket_context(ssl),
+ ssl->session_negotiate, ssl->out_msg + 10,
+ ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
+ &tlen, &lifetime);
+ if (ret != 0) {
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_write", ret);
}
@@ -3500,6 +3607,10 @@ int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl)
break;
case MBEDTLS_SSL_CLIENT_FINISHED:
+ if (ssl->handshake->tls12_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULING_STAGE_TRAFFIC_KEYS) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
ret = mbedtls_ssl_parse_finished(ssl);
break;
@@ -3518,6 +3629,10 @@ int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl)
break;
case MBEDTLS_SSL_SERVER_FINISHED:
+ if (ssl->handshake->tls12_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULING_STAGE_TRAFFIC_KEYS) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
ret = mbedtls_ssl_write_finished(ssl);
break;
@@ -3527,6 +3642,10 @@ int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl)
break;
case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
+ if (ssl->handshake->tls12_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_2_KEY_SCHEDULING_STAGE_TRAFFIC_KEYS) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
mbedtls_ssl_handshake_wrapup(ssl);
break;
diff --git a/thirdparty/mbedtls/library/ssl_tls13_client.c b/thirdparty/mbedtls/library/ssl_tls13_client.c
index e8b6063f32f..76599f96243 100644
--- a/thirdparty/mbedtls/library/ssl_tls13_client.c
+++ b/thirdparty/mbedtls/library/ssl_tls13_client.c
@@ -194,6 +194,8 @@ static int ssl_tls13_reset_key_share(mbedtls_ssl_context *ssl)
}
ssl->handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
+ ssl->handshake->tls13_key_exchange_inputs &=
+ ~MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_EPHEMERAL_BOTH;
return 0;
} else
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
@@ -736,7 +738,7 @@ static int ssl_tls13_ticket_get_psk(mbedtls_ssl_context *ssl,
}
*hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
- *psk = session->resumption_key;
+ *psk = session->secret;
*psk_len = session->resumption_key_len;
return 0;
@@ -1276,17 +1278,13 @@ int mbedtls_ssl_tls13_finalize_client_hello(mbedtls_ssl_context *ssl)
ssl->session_negotiate->ciphersuite);
ssl->handshake->ciphersuite_info = ciphersuite_info;
- /* Enable psk and psk_ephemeral to make stage early happy */
- ssl->handshake->key_exchange_mode =
- MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
-
- /* Start the TLS 1.3 key schedule:
- * Set the PSK and derive early secret.
- */
- ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
+ /* Provisionally derive the early secret for early-data protection.
+ * The selected handshake's early secret is derived after ServerHello
+ * because the server may ignore this PSK. */
+ ret = mbedtls_ssl_tls13_compute_early_secret_for_early_data(ssl);
if (ret != 0) {
MBEDTLS_SSL_DEBUG_RET(
- 1, "mbedtls_ssl_tls13_key_schedule_stage_early", ret);
+ 1, "mbedtls_ssl_tls13_compute_early_secret_for_early_data", ret);
return ret;
}
@@ -1928,10 +1926,21 @@ static int ssl_tls13_postprocess_server_hello(mbedtls_ssl_context *ssl)
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL)
#endif
{
- ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
+#if defined(MBEDTLS_SSL_EARLY_DATA)
+ if (ssl->early_data_state !=
+ MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT) {
+ if (handshake->tls13_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_EARLY_DATA) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
+ handshake->tls13_key_scheduling_stage_completed =
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_NONE;
+ }
+#endif
+ ret = mbedtls_ssl_tls13_compute_early_secret(ssl);
if (ret != 0) {
MBEDTLS_SSL_DEBUG_RET(
- 1, "mbedtls_ssl_tls13_key_schedule_stage_early", ret);
+ 1, "mbedtls_ssl_tls13_compute_early_secret", ret);
goto cleanup;
}
}
@@ -2978,7 +2987,7 @@ static int ssl_tls13_postprocess_new_session_ticket(mbedtls_ssl_context *ssl,
psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
hash_length = PSA_HASH_LENGTH(psa_hash_alg);
if (hash_length == -1 ||
- (size_t) hash_length > sizeof(session->resumption_key)) {
+ (size_t) hash_length > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
}
@@ -2999,7 +3008,7 @@ static int ssl_tls13_postprocess_new_session_ticket(mbedtls_ssl_context *ssl,
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
ticket_nonce,
ticket_nonce_len,
- session->resumption_key,
+ session->secret,
hash_length);
if (ret != 0) {
@@ -3012,7 +3021,7 @@ static int ssl_tls13_postprocess_new_session_ticket(mbedtls_ssl_context *ssl,
session->resumption_key_len = hash_length;
MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
- session->resumption_key,
+ session->secret,
session->resumption_key_len);
/* Set ticket_flags depends on the selected key exchange modes */
@@ -3100,6 +3109,10 @@ int mbedtls_ssl_tls13_handshake_client_step(mbedtls_ssl_context *ssl)
break;
case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
+ if (ssl->handshake->tls13_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_HANDSHAKE) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
ret = ssl_tls13_process_encrypted_extensions(ssl);
break;
@@ -3118,6 +3131,10 @@ int mbedtls_ssl_tls13_handshake_client_step(mbedtls_ssl_context *ssl)
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
case MBEDTLS_SSL_SERVER_FINISHED:
+ if (ssl->handshake->tls13_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_HANDSHAKE) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
ret = ssl_tls13_process_server_finished(ssl);
break;
@@ -3146,6 +3163,10 @@ int mbedtls_ssl_tls13_handshake_client_step(mbedtls_ssl_context *ssl)
break;
case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
+ if (ssl->handshake->tls13_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_APPLICATION) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
ret = ssl_tls13_handshake_wrapup(ssl);
break;
diff --git a/thirdparty/mbedtls/library/ssl_tls13_generic.c b/thirdparty/mbedtls/library/ssl_tls13_generic.c
index abb8b4f3e41..f56d544cc86 100644
--- a/thirdparty/mbedtls/library/ssl_tls13_generic.c
+++ b/thirdparty/mbedtls/library/ssl_tls13_generic.c
@@ -341,11 +341,12 @@ static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
- if ((ret = mbedtls_pk_verify_ext((mbedtls_pk_sigalg_t) sig_alg,
- &ssl->session_negotiate->peer_cert->pk,
- md_alg, verify_hash, verify_hash_len,
- p, signature_len)) == 0) {
- return 0;
+ ret = mbedtls_pk_verify_ext((mbedtls_pk_sigalg_t) sig_alg,
+ &ssl->session_negotiate->peer_cert->pk,
+ md_alg, verify_hash, verify_hash_len,
+ p, signature_len);
+ if (ret == 0) {
+ return ret;
}
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
@@ -1506,6 +1507,8 @@ int mbedtls_ssl_tls13_read_public_xxdhe_share(mbedtls_ssl_context *ssl,
}
memcpy(handshake->xxdh_psa_peerkey, p, peerkey_len);
handshake->xxdh_psa_peerkey_len = peerkey_len;
+ handshake->tls13_key_exchange_inputs |=
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_EPHEMERAL_PEER;
return 0;
}
@@ -1622,6 +1625,8 @@ int mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
return ret;
}
+ handshake->tls13_key_exchange_inputs |=
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_EPHEMERAL_PRIVATE;
*out_len = own_pubkey_len;
return 0;
diff --git a/thirdparty/mbedtls/library/ssl_tls13_invasive.h b/thirdparty/mbedtls/library/ssl_tls13_invasive.h
index 73e0e304f96..e68fd18e80e 100644
--- a/thirdparty/mbedtls/library/ssl_tls13_invasive.h
+++ b/thirdparty/mbedtls/library/ssl_tls13_invasive.h
@@ -18,6 +18,15 @@ int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
const unsigned char *end);
#endif /* MBEDTLS_TEST_HOOKS */
+#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_SSL_SRV_C)
+int mbedtls_ssl_tls13_reset_handshake_state_for_hrr(mbedtls_ssl_context *ssl);
+#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
+int mbedtls_ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
+ const unsigned char *buf,
+ const unsigned char *end);
+#endif
+#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_SSL_SRV_C */
+
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
#endif /* MBEDTLS_SSL_TLS13_INVASIVE_H */
diff --git a/thirdparty/mbedtls/library/ssl_tls13_keys.c b/thirdparty/mbedtls/library/ssl_tls13_keys.c
index 865e02c2dc8..ec15788c4cf 100644
--- a/thirdparty/mbedtls/library/ssl_tls13_keys.c
+++ b/thirdparty/mbedtls/library/ssl_tls13_keys.c
@@ -1034,6 +1034,8 @@ int mbedtls_ssl_tls13_populate_transform(
&transform->psa_key_dec)) != PSA_SUCCESS) {
MBEDTLS_SSL_DEBUG_RET(
1, "psa_import_key", PSA_TO_MBEDTLS_ERR(status));
+ psa_destroy_key(transform->psa_key_enc);
+ transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
return PSA_TO_MBEDTLS_ERR(status);
}
}
@@ -1102,6 +1104,8 @@ static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
+ memset(traffic_keys, 0, sizeof(*traffic_keys));
+
ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
if (ret != 0) {
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
@@ -1188,6 +1192,14 @@ int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
mbedtls_ssl_transform *transform_earlydata = NULL;
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
+ if (handshake->tls13_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_EARLY_SECRET ||
+ (handshake->tls13_key_exchange_inputs &
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_PSK) !=
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_PSK) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
+
/* Next evolution in key schedule: Establish early_data secret and
* key material. */
ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
@@ -1214,6 +1226,8 @@ int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
goto cleanup;
}
handshake->transform_earlydata = transform_earlydata;
+ handshake->tls13_key_scheduling_stage_completed =
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_EARLY_DATA;
cleanup:
mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
@@ -1223,9 +1237,36 @@ cleanup:
return ret;
}
+
#endif /* MBEDTLS_SSL_EARLY_DATA */
-int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
+static int ssl_tls13_check_handshake_inputs(
+ const mbedtls_ssl_handshake_params *handshake)
+{
+ unsigned char required_inputs;
+
+ switch (handshake->key_exchange_mode) {
+ case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK:
+ required_inputs = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_PSK;
+ break;
+ case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL:
+ required_inputs = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_EPHEMERAL_BOTH;
+ break;
+ case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL:
+ required_inputs = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_PSK |
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_EPHEMERAL_BOTH;
+ break;
+ default:
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
+
+ return (handshake->tls13_key_exchange_inputs & required_inputs) ==
+ required_inputs ?
+ 0 : MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+}
+
+static int ssl_tls13_compute_early_secret(mbedtls_ssl_context *ssl,
+ int with_psk)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
psa_algorithm_t hash_alg;
@@ -1233,6 +1274,13 @@ int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
unsigned char *psk = NULL;
size_t psk_len = 0;
+ (void) with_psk;
+
+ if (handshake->tls13_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_NONE) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
+
if (handshake->ciphersuite_info == NULL) {
MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
@@ -1240,7 +1288,7 @@ int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) handshake->ciphersuite_info->mac);
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
- if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
+ if (with_psk) {
ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
if (ret != 0) {
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
@@ -1260,12 +1308,36 @@ int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
return ret;
}
- MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
+ if (with_psk) {
+ handshake->tls13_key_exchange_inputs |=
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_PSK;
+ } else {
+ handshake->tls13_key_exchange_inputs &=
+ ~MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_INPUT_PSK;
+ }
+
+ MBEDTLS_SSL_DEBUG_BUF(4, "ssl_tls13_compute_early_secret",
handshake->tls13_master_secrets.early,
PSA_HASH_LENGTH(hash_alg));
+ handshake->tls13_key_scheduling_stage_completed =
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_EARLY_SECRET;
return 0;
}
+#if defined(MBEDTLS_SSL_EARLY_DATA)
+int mbedtls_ssl_tls13_compute_early_secret_for_early_data(
+ mbedtls_ssl_context *ssl)
+{
+ return ssl_tls13_compute_early_secret(ssl, 1);
+}
+#endif /* MBEDTLS_SSL_EARLY_DATA */
+
+int mbedtls_ssl_tls13_compute_early_secret(mbedtls_ssl_context *ssl)
+{
+ return ssl_tls13_compute_early_secret(
+ ssl, mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl));
+}
+
/**
* \brief Compute TLS 1.3 handshake traffic keys.
*
@@ -1652,6 +1724,21 @@ int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
mbedtls_ssl_transform *transform_handshake = NULL;
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
+ if (handshake->tls13_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_EARLY_SECRET
+#if defined(MBEDTLS_SSL_EARLY_DATA)
+ && handshake->tls13_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_EARLY_DATA
+#endif
+ ) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
+
+ ret = ssl_tls13_check_handshake_inputs(handshake);
+ if (ret != 0) {
+ return ret;
+ }
+
/* Compute handshake secret */
ret = ssl_tls13_key_schedule_stage_handshake(ssl);
if (ret != 0) {
@@ -1685,6 +1772,8 @@ int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
goto cleanup;
}
handshake->transform_handshake = transform_handshake;
+ handshake->tls13_key_scheduling_stage_completed =
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_HANDSHAKE;
cleanup:
mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
@@ -1744,6 +1833,11 @@ int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
mbedtls_ssl_key_set traffic_keys;
mbedtls_ssl_transform *transform_application = NULL;
+ if (ssl->handshake->tls13_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_HANDSHAKE) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
+
ret = ssl_tls13_key_schedule_stage_application(ssl);
if (ret != 0) {
MBEDTLS_SSL_DEBUG_RET(1,
@@ -1777,6 +1871,8 @@ int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
}
ssl->transform_application = transform_application;
+ ssl->handshake->tls13_key_scheduling_stage_completed =
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_APPLICATION;
cleanup:
diff --git a/thirdparty/mbedtls/library/ssl_tls13_keys.h b/thirdparty/mbedtls/library/ssl_tls13_keys.h
index 1509e9a4d49..21ac078ede6 100644
--- a/thirdparty/mbedtls/library/ssl_tls13_keys.h
+++ b/thirdparty/mbedtls/library/ssl_tls13_keys.h
@@ -539,7 +539,7 @@ int mbedtls_ssl_tls13_populate_transform(mbedtls_ssl_transform *transform,
* \returns A negative error code on failure.
*/
MBEDTLS_CHECK_RETURN_CRITICAL
-int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl);
+int mbedtls_ssl_tls13_compute_early_secret(mbedtls_ssl_context *ssl);
/**
* \brief Compute TLS 1.3 resumption master secret.
@@ -584,6 +584,17 @@ int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
int which);
#if defined(MBEDTLS_SSL_EARLY_DATA)
+/**
+ * \brief Derive the early secret for 0-RTT data protection
+ *
+ * The selected handshake's early secret is derived by
+ * mbedtls_ssl_tls13_compute_early_secret() after ServerHello has selected the
+ * key exchange if not already done by this function.
+ */
+MBEDTLS_CHECK_RETURN_CRITICAL
+int mbedtls_ssl_tls13_compute_early_secret_for_early_data(
+ mbedtls_ssl_context *ssl);
+
/**
* \brief Compute TLS 1.3 early transform
*
@@ -593,8 +604,8 @@ int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
* \returns A negative error code on failure.
*
* \warning The function does not compute the early master secret. Call
- * mbedtls_ssl_tls13_key_schedule_stage_early() before to
- * call this function to generate the early master secret.
+ * mbedtls_ssl_tls13_compute_early_secret_for_early_data() before
+ * this function to generate it.
* \note For a client/server endpoint, the function computes only the
* encryption/decryption part of the transform as the decryption/
* encryption part is not defined by the specification (no early
diff --git a/thirdparty/mbedtls/library/ssl_tls13_server.c b/thirdparty/mbedtls/library/ssl_tls13_server.c
index 38f2fe93708..4551a1d31cc 100644
--- a/thirdparty/mbedtls/library/ssl_tls13_server.c
+++ b/thirdparty/mbedtls/library/ssl_tls13_server.c
@@ -17,8 +17,18 @@
#include "mbedtls/psa_util.h"
#include "ssl_tls13_keys.h"
+#include "ssl_tls13_invasive.h"
#include "ssl_debug_helpers.h"
+/* Define a local translating function to save code size by not using too many
+ * arguments in each translating place. */
+static int local_err_translation(psa_status_t status)
+{
+ return psa_status_to_mbedtls(status, psa_to_ssl_errors,
+ ARRAY_LENGTH(psa_to_ssl_errors),
+ psa_generic_status_to_mbedtls);
+}
+#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
mbedtls_ssl_context *ssl,
@@ -215,7 +225,7 @@ static int ssl_tls13_offered_psks_check_identity_match_ticket(
}
memcpy(ticket_buffer, identity, identity_len);
- ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
+ ret = ssl->conf->f_ticket_parse(mbedtls_ssl_get_session_ticket_context(ssl),
session,
ticket_buffer, identity_len);
switch (ret) {
@@ -325,6 +335,10 @@ exit:
}
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
+/* On return with SSL_TLS1_3_PSK_IDENTITY_MATCH, the PSK corresponding to the
+ * identity has been installed in the handshake through
+ * mbedtls_ssl_set_hs_psk(), either directly or by the configured PSK callback.
+ */
MBEDTLS_CHECK_RETURN_CRITICAL
static int ssl_tls13_offered_psks_check_identity_match(
mbedtls_ssl_context *ssl,
@@ -348,7 +362,7 @@ static int ssl_tls13_offered_psks_check_identity_match(
if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
*psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
ret = mbedtls_ssl_set_hs_psk(ssl,
- session->resumption_key,
+ session->secret,
session->resumption_key_len);
if (ret != 0) {
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
@@ -356,7 +370,7 @@ static int ssl_tls13_offered_psks_check_identity_match(
}
MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
- session->resumption_key,
+ session->secret,
session->resumption_key_len);
MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
(unsigned) obfuscated_ticket_age));
@@ -392,14 +406,6 @@ static int ssl_tls13_offered_psks_check_identity_match(
return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
}
-/*
- * Non-error return values of ssl_tls13_offered_psks_check_binder_match().
- * They are positive to not collide with error codes that are negative. Zero
- * (SSL_TLS1_3_BINDER_MATCH) in case of success as it may be propagated up
- * by the callers of this function as a generic success condition.
- */
-#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
-#define SSL_TLS1_3_BINDER_MATCH 0
MBEDTLS_CHECK_RETURN_CRITICAL
static int ssl_tls13_offered_psks_check_binder_match(
mbedtls_ssl_context *ssl,
@@ -415,7 +421,7 @@ static int ssl_tls13_offered_psks_check_binder_match(
unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
if (binder_len != PSA_HASH_LENGTH(psk_hash_alg)) {
- return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
}
/* Get current state of handshake transcript. */
@@ -448,12 +454,12 @@ static int ssl_tls13_offered_psks_check_binder_match(
if (mbedtls_ct_memcmp(server_computed_binder,
binder,
PSA_HASH_LENGTH(psk_hash_alg)) == 0) {
- return SSL_TLS1_3_BINDER_MATCH;
+ return 0;
}
mbedtls_platform_zeroize(server_computed_binder,
sizeof(server_computed_binder));
- return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
}
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
@@ -467,7 +473,7 @@ static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
if (src->resumption_key_len == 0) {
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
}
- memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
+ memcpy(dst->secret, src->secret, src->resumption_key_len);
#if defined(MBEDTLS_SSL_EARLY_DATA)
dst->max_early_data_size = src->max_early_data_size;
@@ -491,6 +497,135 @@ struct psk_attributes {
};
#define PSK_ATTRIBUTES_INIT { 0, 0, NULL }
+/* Process one PSK identity and its corresponding binder.
+ *
+ * return 0 if processing completed. In this case, psk_is_set is set to 1
+ * if the identity yielded a usable PSK, and to 0 otherwise. psk is
+ * valid only if psk_is_set is set to 1.
+ * return A negative error code on fatal error.
+ */
+MBEDTLS_CHECK_RETURN_CRITICAL
+static int ssl_tls13_process_psk_identity(
+ mbedtls_ssl_context *ssl,
+ const unsigned char *identity,
+ size_t identity_len,
+ uint32_t obfuscated_ticket_age,
+ const unsigned char *binder,
+ size_t binder_len,
+ const unsigned char *ciphersuites,
+ const unsigned char *ciphersuites_end,
+ struct psk_attributes *psk,
+ int *psk_is_set)
+{
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
+ int psk_ciphersuite_id = 0;
+ psa_algorithm_t psk_hash_alg = PSA_ALG_NONE;
+ int allowed_key_exchange_modes =
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
+ mbedtls_ssl_session session;
+
+ *psk = (struct psk_attributes) PSK_ATTRIBUTES_INIT;
+ *psk_is_set = 0;
+
+ mbedtls_ssl_session_init(&session);
+
+ ret = ssl_tls13_offered_psks_check_identity_match(
+ ssl, identity, identity_len, obfuscated_ticket_age,
+ &psk->type, &session);
+ if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
+ if (ret > 0) {
+ ret = 0;
+ }
+ goto cleanup;
+ }
+
+ MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
+
+ switch (psk->type) {
+ case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
+ psk_hash_alg = PSA_ALG_SHA_256;
+ break;
+#if defined(MBEDTLS_SSL_SESSION_TICKETS)
+ case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
+ psk_ciphersuite_id = session.ciphersuite;
+ allowed_key_exchange_modes =
+ session.ticket_flags &
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
+ break;
+#endif
+ default:
+ ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ goto cleanup;
+ }
+
+ psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
+
+ if ((allowed_key_exchange_modes &
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
+ ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
+ psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
+ } else if ((allowed_key_exchange_modes &
+ MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
+ ssl_tls13_key_exchange_is_psk_available(ssl)) {
+ psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
+ }
+
+ if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
+ MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
+ ret = 0;
+ goto cleanup;
+ }
+
+ ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
+ psk_ciphersuite_id, psk_hash_alg,
+ &psk->ciphersuite_info);
+
+ if (psk->ciphersuite_info == NULL) {
+ /*
+ * We consider finding a ciphersuite suitable for the PSK as part
+ * of the validation of its binder. Thus if we do not find one, we
+ * abort the handshake with a decrypt_error alert.
+ */
+ MBEDTLS_SSL_PEND_FATAL_ALERT(
+ MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
+ MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
+ ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
+ goto cleanup;
+ }
+
+ ret = ssl_tls13_offered_psks_check_binder_match(
+ ssl, binder, binder_len, psk->type,
+ mbedtls_md_psa_alg_from_type(
+ (mbedtls_md_type_t) psk->ciphersuite_info->mac));
+ if (ret != 0) {
+ /* For security reasons, the handshake should be aborted when we
+ * fail to validate a binder value. See RFC 8446 section 4.2.11.2
+ * and appendix E.6. */
+ MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
+ MBEDTLS_SSL_DEBUG_RET(
+ 1, "ssl_tls13_offered_psks_check_binder_match", ret);
+ MBEDTLS_SSL_PEND_FATAL_ALERT(
+ MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
+ MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
+ goto cleanup;
+ }
+
+#if defined(MBEDTLS_SSL_SESSION_TICKETS)
+ if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
+ ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate, &session);
+ if (ret != 0) {
+ goto cleanup;
+ }
+ }
+#endif /* MBEDTLS_SSL_SESSION_TICKETS */
+
+ *psk_is_set = 1;
+
+cleanup:
+ mbedtls_ssl_session_free(&session);
+ return ret;
+}
+
/* Parser for pre_shared_key extension in client hello
* struct {
* opaque identity<1..2^16-1>;
@@ -539,8 +674,11 @@ static int ssl_tls13_parse_pre_shared_key_ext(
/* identities_len 2 bytes
* identities_data >= 7 bytes
*/
- MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
+ MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 2);
identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
+ if (identities_len < 7) {
+ goto decode_error;
+ }
p_identity_len = identities + 2;
MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
identities_len);
@@ -550,8 +688,11 @@ static int ssl_tls13_parse_pre_shared_key_ext(
* binders >= 33 bytes
*/
binders = identities_end;
- MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
+ MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 2);
binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
+ if (binders_len < 33) {
+ goto decode_error;
+ }
p_binder_len = binders + 2;
MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
binders_end = p_binder_len + binders_len;
@@ -569,22 +710,23 @@ static int ssl_tls13_parse_pre_shared_key_ext(
uint32_t obfuscated_ticket_age;
const unsigned char *binder;
size_t binder_len;
- int psk_ciphersuite_id;
- psa_algorithm_t psk_hash_alg;
- int allowed_key_exchange_modes;
-
- mbedtls_ssl_session session;
- mbedtls_ssl_session_init(&session);
+ int psk_is_set;
MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
+ if (identity_len == 0) {
+ goto decode_error;
+ }
identity = p_identity_len + 2;
MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
p_identity_len += identity_len + 6;
- MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
+ MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1);
binder_len = *p_binder_len;
+ if (binder_len < 32) {
+ goto decode_error;
+ }
binder = p_binder_len + 1;
MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
p_binder_len += binder_len + 1;
@@ -594,110 +736,21 @@ static int ssl_tls13_parse_pre_shared_key_ext(
continue;
}
- ret = ssl_tls13_offered_psks_check_identity_match(
+ ret = ssl_tls13_process_psk_identity(
ssl, identity, identity_len, obfuscated_ticket_age,
- &psk->type, &session);
- if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
- continue;
- }
-
- MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
-
- switch (psk->type) {
- case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
- psk_ciphersuite_id = 0;
- psk_hash_alg = PSA_ALG_SHA_256;
- allowed_key_exchange_modes =
- MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
- break;
-#if defined(MBEDTLS_SSL_SESSION_TICKETS)
- case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
- psk_ciphersuite_id = session.ciphersuite;
- psk_hash_alg = PSA_ALG_NONE;
- ssl->session_negotiate->ticket_flags = session.ticket_flags;
- allowed_key_exchange_modes =
- session.ticket_flags &
- MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
- break;
-#endif
- default:
- return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
- }
-
- psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
-
- if ((allowed_key_exchange_modes &
- MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
- ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
- psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
- } else if ((allowed_key_exchange_modes &
- MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
- ssl_tls13_key_exchange_is_psk_available(ssl)) {
- psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
- }
-
- if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
- MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
- continue;
- }
-
- ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
- psk_ciphersuite_id, psk_hash_alg,
- &psk->ciphersuite_info);
-
- if (psk->ciphersuite_info == NULL) {
-#if defined(MBEDTLS_SSL_SESSION_TICKETS)
- mbedtls_ssl_session_free(&session);
-#endif
- /*
- * We consider finding a ciphersuite suitable for the PSK as part
- * of the validation of its binder. Thus if we do not find one, we
- * abort the handshake with a decrypt_error alert.
- */
- MBEDTLS_SSL_PEND_FATAL_ALERT(
- MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
- MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
- return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
- }
-
- ret = ssl_tls13_offered_psks_check_binder_match(
- ssl, binder, binder_len, psk->type,
- mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
- if (ret != SSL_TLS1_3_BINDER_MATCH) {
- /* For security reasons, the handshake should be aborted when we
- * fail to validate a binder value. See RFC 8446 section 4.2.11.2
- * and appendix E.6. */
-#if defined(MBEDTLS_SSL_SESSION_TICKETS)
- mbedtls_ssl_session_free(&session);
-#endif
- MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
- MBEDTLS_SSL_DEBUG_RET(
- 1, "ssl_tls13_offered_psks_check_binder_match", ret);
- MBEDTLS_SSL_PEND_FATAL_ALERT(
- MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
- MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
+ binder, binder_len, ciphersuites, ciphersuites_end,
+ psk, &psk_is_set);
+ if (ret != 0) {
return ret;
}
- matched_identity = identity_id;
-
-#if defined(MBEDTLS_SSL_SESSION_TICKETS)
- if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
- ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
- &session);
- mbedtls_ssl_session_free(&session);
- if (ret != 0) {
- return ret;
- }
+ if (psk_is_set) {
+ matched_identity = identity_id;
}
-#endif /* MBEDTLS_SSL_SESSION_TICKETS */
}
if (p_identity_len != identities_end || p_binder_len != binders_end) {
- MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
- MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
- MBEDTLS_ERR_SSL_DECODE_ERROR);
- return MBEDTLS_ERR_SSL_DECODE_ERROR;
+ goto decode_error;
}
/* Update the handshake transcript with the binder list. */
@@ -716,6 +769,12 @@ static int ssl_tls13_parse_pre_shared_key_ext(
MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
return 0;
+
+decode_error:
+ MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
+ MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
+ MBEDTLS_ERR_SSL_DECODE_ERROR);
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
}
/*
@@ -843,7 +902,6 @@ static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
p += 2;
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
named_group_list_end = p + named_group_list_len;
- ssl->handshake->hrr_selected_group = 0;
while (p < named_group_list_end) {
uint16_t named_group;
@@ -856,7 +914,13 @@ static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
mbedtls_ssl_named_group_to_str(named_group),
named_group));
- if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
+ /* No need to select a group for an HRR if one has already been sent.
+ * This also preserves the group selected for the HRR, allowing us to
+ * check that the key_share extension in the second ClientHello is
+ * consistent with it.
+ */
+ if (ssl->handshake->hello_retry_request_flag ||
+ !mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
!mbedtls_ssl_named_group_is_supported(named_group) ||
ssl->handshake->hrr_selected_group != 0) {
continue;
@@ -891,14 +955,16 @@ static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
* - A negative value for fatal errors.
*/
MBEDTLS_CHECK_RETURN_CRITICAL
-static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
- const unsigned char *buf,
- const unsigned char *end)
+MBEDTLS_STATIC_TESTABLE
+int mbedtls_ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
+ const unsigned char *buf,
+ const unsigned char *end)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
unsigned char const *p = buf;
unsigned char const *client_shares_end;
size_t client_shares_len;
+ size_t key_share_count = 0;
/* From RFC 8446:
*
@@ -940,6 +1006,7 @@ static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
key_exchange = p;
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
p += key_exchange_len;
+ key_share_count++;
/* Continue parsing even if we have already found a match,
* for input validation purposes.
@@ -973,6 +1040,20 @@ static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
ssl->handshake->offered_group_id = group;
}
+ /*
+ * When the HRR requests a key share for a selected group, the second
+ * ClientHello must contain exactly one KeyShareEntry (RFC 8446,
+ * Section 4.1.2).
+ */
+ if (ssl->handshake->hello_retry_request_flag &&
+ ssl->handshake->hrr_selected_group != 0 && key_share_count != 1) {
+ MBEDTLS_SSL_DEBUG_MSG(
+ 1, ("Non-compliant second ClientHello: "
+ "key_share must contain exactly one KeyShareEntry"));
+ MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
+ MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
+ }
if (ssl->handshake->offered_group_id == 0) {
MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
@@ -1248,6 +1329,8 @@ static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
const unsigned char *supported_versions_data;
const unsigned char *supported_versions_data_end;
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
+ uint16_t hrr_ciphersuite_id = handshake->hello_retry_request_flag ?
+ handshake->ciphersuite_info->id : 0;
int hrr_required = 0;
int no_usable_share_for_key_agreement = 0;
@@ -1555,7 +1638,7 @@ static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
* contains the endpoint's cryptographic parameters for
* ECDHE/DHE key establishment methods.
*/
- ret = ssl_tls13_parse_key_shares_ext(
+ ret = mbedtls_ssl_tls13_parse_key_shares_ext(
ssl, p, extension_data_end);
if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
@@ -1663,7 +1746,46 @@ static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
handshake->received_extensions);
+ if (handshake->hello_retry_request_flag) {
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+ /* RFC 8446, Section 4.1.2 requires CH2 to retain CH1's server_name.
+ * Check this before invoking the SNI callback for CH2. */
+ int sni_name_presence_changed =
+ handshake->ch1_sni_name_present != (handshake->sni_name != NULL);
+ int sni_name_changed = handshake->sni_name != NULL &&
+ (handshake->ch1_sni_name_len !=
+ handshake->sni_name_len ||
+ memcmp(handshake->ch1_sni_name,
+ handshake->sni_name,
+ handshake->sni_name_len) != 0);
+
+ if (sni_name_presence_changed || sni_name_changed) {
+ MBEDTLS_SSL_DEBUG_MSG(
+ 1, ("Non-compliant second ClientHello: SNI changed after HRR"));
+ MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
+ MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
+ }
+#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
+#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
+ if (handshake->hrr_selected_group != 0 &&
+ handshake->offered_group_id != handshake->hrr_selected_group) {
+ MBEDTLS_SSL_DEBUG_MSG(
+ 1, ("Non-compliant second ClientHello: "
+ "key share group does not match HRR"));
+ MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
+ MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
+ }
+#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
+ }
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+ ret = mbedtls_ssl_call_sni_callback(ssl);
+ if (ret != 0) {
+ return ret;
+ }
+#endif
ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
MBEDTLS_SSL_HS_CLIENT_HELLO,
p - buf);
@@ -1776,6 +1898,18 @@ static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
}
#endif
+ /* RFC 8446, Section 4.1.4 requires the server to negotiate the same
+ * ciphersuite after a HelloRetryRequest. */
+ if (handshake->hello_retry_request_flag &&
+ handshake->ciphersuite_info->id != hrr_ciphersuite_id) {
+ MBEDTLS_SSL_DEBUG_MSG(1,
+ ("Non-compliant second ClientHello: "
+ "the selected ciphersuite has changed."));
+ MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
+ MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
+ }
+
mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
@@ -1869,6 +2003,30 @@ static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
/* Update the handshake state machine */
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+MBEDTLS_CHECK_RETURN_CRITICAL
+static int ssl_tls13_preserve_ch1_sni_name(mbedtls_ssl_handshake_params *handshake)
+{
+ unsigned char *name = NULL;
+
+ if (handshake->sni_name != NULL) {
+ name = mbedtls_calloc(1, handshake->sni_name_len);
+ if (name == NULL) {
+ return MBEDTLS_ERR_SSL_ALLOC_FAILED;
+ }
+
+ memcpy(name, handshake->sni_name, handshake->sni_name_len);
+ }
+
+ mbedtls_free(handshake->ch1_sni_name);
+ handshake->ch1_sni_name = name;
+ handshake->ch1_sni_name_len = handshake->sni_name_len;
+ handshake->ch1_sni_name_present = handshake->sni_name != NULL;
+
+ return 0;
+}
+#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
+
MBEDTLS_CHECK_RETURN_CRITICAL
static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
int hrr_required)
@@ -1883,15 +2041,25 @@ static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
return ret;
}
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+ if (hrr_required) {
+ ret = ssl_tls13_preserve_ch1_sni_name(ssl->handshake);
+ if (ret != 0) {
+ return ret;
+ }
+ }
+
ssl->handshake->sni_name = NULL;
ssl->handshake->sni_name_len = 0;
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
- ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
- if (ret != 0) {
- MBEDTLS_SSL_DEBUG_RET(1,
- "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
- return ret;
+ /* An HRR causes this function to process another ClientHello. Compute the
+ * early secret only once, after the final ClientHello has been accepted. */
+ if (!hrr_required) {
+ ret = mbedtls_ssl_tls13_compute_early_secret(ssl);
+ if (ret != 0) {
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_compute_early_secret", ret);
+ return ret;
+ }
}
#if defined(MBEDTLS_SSL_EARLY_DATA)
@@ -2474,6 +2642,127 @@ static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
return 0;
}
+MBEDTLS_CHECK_RETURN_CRITICAL
+MBEDTLS_STATIC_TESTABLE
+int mbedtls_ssl_tls13_reset_handshake_state_for_hrr(mbedtls_ssl_context *ssl)
+{
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
+ mbedtls_ssl_handshake_params *handshake = ssl->handshake;
+ psa_status_t status;
+
+ /* Preserve the handshake and provisional session state needed to process
+ * CH2 after sending an HRR: the selected ciphersuite and group,
+ * the transcript and the early-data limit. */
+ const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
+ handshake->ciphersuite_info;
+ uint16_t selected_group = handshake->hrr_selected_group;
+ psa_hash_operation_t saved_transcript = PSA_HASH_OPERATION_INIT;
+ psa_hash_operation_t *transcript = NULL;
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+ uint8_t ch1_sni_name_present = handshake->ch1_sni_name_present;
+ unsigned char *ch1_sni_name = NULL;
+ size_t ch1_sni_name_len = handshake->ch1_sni_name_len;
+#endif
+#if defined(MBEDTLS_SSL_EARLY_DATA)
+ uint32_t max_early_data_size = ssl->session_negotiate->max_early_data_size;
+ /* These fields belong to the record layer and must span CH1, HRR and CH2
+ * so that early-data records continue to be discarded and accounted for.
+ * However, mbedtls_ssl_handshake_init() resets these record-layer
+ * fields as an initialization side effect. Preserve them explicitly around
+ * that reset. */
+ uint8_t discard_early_data_record = ssl->discard_early_data_record;
+ uint32_t total_early_data_size = ssl->total_early_data_size;
+#endif
+
+#if defined(PSA_WANT_ALG_SHA_384)
+ if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
+ transcript = &handshake->fin_sha384_psa;
+ }
+#endif
+#if defined(PSA_WANT_ALG_SHA_256)
+ if (ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
+ transcript = &handshake->fin_sha256_psa;
+ }
+#endif
+ if (transcript == NULL) {
+ ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ goto cleanup;
+ }
+
+ status = psa_hash_clone(transcript, &saved_transcript);
+ if (status != PSA_SUCCESS) {
+ ret = PSA_TO_MBEDTLS_ERR(status);
+ goto cleanup;
+ }
+
+ /* Discard all other state derived while processing CH1. */
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+ /* Move ownership out of the handshake state while it is reset. */
+ ch1_sni_name = handshake->ch1_sni_name;
+ handshake->ch1_sni_name = NULL;
+#endif
+ MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_handshake_init(ssl));
+
+#if defined(MBEDTLS_SSL_ALPN)
+ /* ALPN is selected while processing a ClientHello but is stored outside
+ * the handshake context. Let CH2 select it again. */
+ ssl->alpn_chosen = NULL;
+#endif
+
+ /* Restore what we saved before resetting the handshake context. */
+ handshake = ssl->handshake;
+ handshake->ciphersuite_info = ciphersuite_info;
+ handshake->hrr_selected_group = selected_group;
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+ handshake->ch1_sni_name_present = ch1_sni_name_present;
+ handshake->ch1_sni_name = ch1_sni_name;
+ handshake->ch1_sni_name_len = ch1_sni_name_len;
+ ch1_sni_name = NULL;
+#endif
+#if defined(MBEDTLS_SSL_EARLY_DATA)
+ ssl->session_negotiate->max_early_data_size = max_early_data_size;
+ ssl->discard_early_data_record = discard_early_data_record;
+ ssl->total_early_data_size = total_early_data_size;
+#endif
+
+ transcript = NULL;
+#if defined(PSA_WANT_ALG_SHA_384)
+ if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
+ transcript = &handshake->fin_sha384_psa;
+ }
+#endif
+#if defined(PSA_WANT_ALG_SHA_256)
+ if (ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
+ transcript = &handshake->fin_sha256_psa;
+ }
+#endif
+ if (transcript == NULL) {
+ ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ goto cleanup;
+ }
+
+ status = psa_hash_abort(transcript);
+ if (status != PSA_SUCCESS) {
+ ret = PSA_TO_MBEDTLS_ERR(status);
+ goto cleanup;
+ }
+
+ status = psa_hash_clone(&saved_transcript, transcript);
+ if (status != PSA_SUCCESS) {
+ ret = PSA_TO_MBEDTLS_ERR(status);
+ goto cleanup;
+ }
+
+ mbedtls_ssl_optimize_checksum(ssl, ciphersuite_info);
+
+cleanup:
+#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
+ mbedtls_free(ch1_sni_name);
+#endif
+ psa_hash_abort(&saved_transcript);
+ return ret;
+}
+
MBEDTLS_CHECK_RETURN_CRITICAL
static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
{
@@ -2500,6 +2789,11 @@ static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
msg_len));
+ /* Process CH2 with fresh handshake and provisional session state. Keep
+ * only the state that is intrinsic to the HRR exchange itself. */
+ MBEDTLS_SSL_PROC_CHK(
+ mbedtls_ssl_tls13_reset_handshake_state_for_hrr(ssl));
+
ssl->handshake->hello_retry_request_flag = 1;
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
@@ -3207,7 +3501,7 @@ static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
hash_length = PSA_HASH_LENGTH(psa_hash_alg);
if (hash_length == -1 ||
- (size_t) hash_length > sizeof(session->resumption_key)) {
+ (size_t) hash_length > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
}
@@ -3227,7 +3521,7 @@ static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
ticket_nonce,
ticket_nonce_size,
- session->resumption_key,
+ session->secret,
hash_length);
if (ret != 0) {
@@ -3237,7 +3531,7 @@ static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
return ret;
}
MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
- session->resumption_key,
+ session->secret,
session->resumption_key_len);
MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
@@ -3302,7 +3596,7 @@ static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
#if defined(MBEDTLS_HAVE_TIME)
session->ticket_creation_time = mbedtls_ms_time();
#endif
- ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
+ ret = ssl->conf->f_ticket_write(mbedtls_ssl_get_session_ticket_context(ssl),
session,
p + 9 + ticket_nonce_size + 2,
end,
@@ -3482,6 +3776,10 @@ int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
break;
case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
+ if (ssl->handshake->tls13_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_HANDSHAKE) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
ret = ssl_tls13_write_encrypted_extensions(ssl);
if (ret != 0) {
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
@@ -3524,6 +3822,10 @@ int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
case MBEDTLS_SSL_SERVER_FINISHED:
+ if (ssl->handshake->tls13_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_HANDSHAKE) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
ret = ssl_tls13_write_server_finished(ssl);
break;
@@ -3538,6 +3840,10 @@ int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
break;
case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
+ if (ssl->handshake->tls13_key_scheduling_stage_completed !=
+ MBEDTLS_SSL_TLS1_3_KEY_SCHEDULING_STAGE_APPLICATION) {
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
+ }
ret = ssl_tls13_handshake_wrapup(ssl);
break;
diff --git a/thirdparty/mbedtls/library/x509.c b/thirdparty/mbedtls/library/x509.c
index d7bc5d2dfb9..e5bd1d1f58c 100644
--- a/thirdparty/mbedtls/library/x509.c
+++ b/thirdparty/mbedtls/library/x509.c
@@ -908,6 +908,11 @@ int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
p = buf;
n = size;
+ if (size == 0) {
+ return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
+ }
+ buf[0] = 0;
+
while (name != NULL) {
if (!name->oid.p) {
name = name->next;
diff --git a/thirdparty/mbedtls/library/x509_create.c b/thirdparty/mbedtls/library/x509_create.c
index e424cbb47cf..92d14c851f6 100644
--- a/thirdparty/mbedtls/library/x509_create.c
+++ b/thirdparty/mbedtls/library/x509_create.c
@@ -287,7 +287,7 @@ static int oid_parse_number(unsigned int *num, const char **p, const char *bound
while (*p < bound && **p >= '0' && **p <= '9') {
ret = 0;
- if (*num > (UINT_MAX / 10)) {
+ if (*num > (UINT_MAX - (unsigned int) (**p - '0')) / 10) {
return MBEDTLS_ERR_ASN1_INVALID_DATA;
}
*num *= 10;
diff --git a/thirdparty/mbedtls/library/x509_crl.c b/thirdparty/mbedtls/library/x509_crl.c
index 0b98ba46644..ab4908e811b 100644
--- a/thirdparty/mbedtls/library/x509_crl.c
+++ b/thirdparty/mbedtls/library/x509_crl.c
@@ -310,7 +310,6 @@ int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
crl->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crl));
if (crl->next == NULL) {
- mbedtls_x509_crl_free(crl);
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}
diff --git a/thirdparty/mbedtls/library/x509_crt.c b/thirdparty/mbedtls/library/x509_crt.c
index a6fcd1b4e6b..e660365774e 100644
--- a/thirdparty/mbedtls/library/x509_crt.c
+++ b/thirdparty/mbedtls/library/x509_crt.c
@@ -265,17 +265,22 @@ static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
/*
* Return 0 if name matches wildcard, -1 otherwise
+ *
+ * Follow RFC 9525: the wildcard can only be the entirety of the leftmost label.
+ * (Note: RFC 6125 was more lenient, but too complex for our own good.)
*/
-static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
+static int x509_check_dns_wildcard(const char *cn, size_t cn_len,
+ const mbedtls_x509_buf *name)
{
size_t i;
- size_t cn_idx = 0, cn_len = strlen(cn);
+ size_t cn_idx = 0;
/* We can't have a match if there is no wildcard to match */
if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
return -1;
}
+ /* Find the first dot. This ensures the wildcard only matches one label. */
for (i = 0; i < cn_len; ++i) {
if (cn[i] == '.') {
cn_idx = i;
@@ -518,17 +523,19 @@ static int x509_get_basic_constraints(unsigned char **p,
}
if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
- /* If the SEQUENCE starts with an INTEGER and not a BOOLEAN,
- * according to RFC 5280, it's syntactically valid, but it's
- * something that a CA MUST NOT produce. So we reject it.
+ /* cA is DEFAULT FALSE. If it is absent, the first element must
+ * be pathLenConstraint. Keep cA false and let the INTEGER parser
+ * below validate the element.
*
* Note: historically, from XySSL 0.9 to Mbed TLS 3.6.6/4.1.0,
* `SEQUENCE { INTEGER n }` was interpreted as cA=FALSE if n == 0
* and cA=TRUE if n != 0. This was dangerous since
* `SEQUENCE { INTEGER n }` should be parsed as cA=FALSE
- * according to RFC 5280, so we stopped doing it.
+ * according to RFC 5280.
*/
- return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
+ if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
+ }
}
/* `SEQUENCE { BOOLEAN FALSE }` is not DER since default-value fields
* must be omitted in DER. But it seems harmless, and Mbed TLS has
@@ -638,6 +645,11 @@ static int x509_get_authority_key_id(unsigned char **p,
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
}
+ /* All fields are OPTIONAL, so an empty SEQUENCE is valid. */
+ if (*p == end) {
+ return 0;
+ }
+
ret = mbedtls_asn1_get_tag(p, end, &len,
MBEDTLS_ASN1_CONTEXT_SPECIFIC);
@@ -2861,100 +2873,176 @@ size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
}
/*
- * Check for CN match
+ * Check for DNS hostname match
*/
-static int x509_crt_check_cn(const mbedtls_x509_buf *name,
- const char *cn, size_t cn_len)
+static int x509_crt_check_dns_name(const mbedtls_x509_buf *name,
+ const char *cn, size_t cn_len)
{
- /* try exact match */
+ /* try exact match, case-insensitive (RFC 6125 or 9525) */
if (name->len == cn_len &&
x509_memcasecmp(cn, name->p, cn_len) == 0) {
return 0;
}
/* try wildcard match */
- if (x509_check_wildcard(cn, name) == 0) {
+ if (x509_check_dns_wildcard(cn, cn_len, name) == 0) {
return 0;
}
return -1;
}
-static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
- const char *cn, size_t cn_len)
-{
- uint32_t ip[4];
- cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
- if (cn_len == 0) {
- return -1;
- }
-
- for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
- const unsigned char san_type = (unsigned char) cur->buf.tag &
- MBEDTLS_ASN1_TAG_VALUE_MASK;
- if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
- cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
- return 0;
- }
- }
-
- return -1;
-}
-
-static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
- const char *cn, size_t cn_len)
-{
- for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
- const unsigned char san_type = (unsigned char) cur->buf.tag &
- MBEDTLS_ASN1_TAG_VALUE_MASK;
- if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
- cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
- return 0;
- }
- }
-
- return -1;
-}
-
/*
* Check for SAN match, see RFC 5280 Section 4.2.1.6
+ *
+ * If cn_type is IP, then cn must contain the binary representation of the
+ * address, and cn_len the number of bytes (4 for IPv4, 16 for IPv6).
+ * Otherwise, cn is what was passed by the user.
*/
static int x509_crt_check_san(const mbedtls_x509_sequence *san,
- const char *cn, size_t cn_len)
+ const char *cn, size_t cn_len, int cn_type)
{
- int san_ip = 0;
- int san_uri = 0;
- /* Prioritize DNS name over other subtypes due to popularity */
for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
- switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
+ /* Only consider SAN entries of the expected type */
+ int san_type = cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK;
+ if (san_type != cn_type) {
+ continue;
+ }
+
+ /* Check according to type */
+ switch (cn_type) {
case MBEDTLS_X509_SAN_DNS_NAME:
- if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
+ /* DNS: special rules */
+ if (x509_crt_check_dns_name(&cur->buf, cn, cn_len) == 0) {
return 0;
}
break;
case MBEDTLS_X509_SAN_IP_ADDRESS:
- san_ip = 1;
- break;
case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
- san_uri = 1;
+ /* IP: exact match according to RFC 9525 */
+ /* URL: exact match for now according to our documentation */
+ if (cur->buf.len == cn_len &&
+ memcmp(cur->buf.p, cn, cn_len) == 0) {
+ return 0;
+ }
break;
/* (We may handle other types here later.) */
default: /* Unrecognized type */
break;
}
}
- if (san_ip) {
- if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
+
+ return -1;
+}
+
+/* Return 1 if the cn superficially looks like a URI, 0 if it doesn't.
+ *
+ * RFC 5280 4.2.1.6 URIs are as specified by RFC 3986, whose App. A says:
+ * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
+ * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
+ *
+ * For now, look for the presence of a colon, and that the scheme is non-empty
+ * and consists only of allowed characters.
+ */
+static int x509_cn_looks_like_uri(const char *cn)
+{
+ const char *end = strchr(cn, ':');
+ if (end == NULL || end == cn) {
+ return 0;
+ }
+
+ for (const char *p = cn; p < end; p++) {
+ if (!((*p >= 'a' && *p <= 'z') ||
+ (*p >= 'A' && *p <= 'Z') ||
+ (*p >= '0' && *p <= '9') ||
+ *p == '+' || *p == '-' || *p == '.')) {
return 0;
}
}
- if (san_uri) {
- if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
- return 0;
+
+ return 1;
+}
+
+/* Return 1 if the cn superficially looks like a DNS, 0 if it doesn't.
+ *
+ * RFC 5280 4.2.1.6 says DNS names are as specified by Section 3.5 of
+ * RFC1034 and as modified by Section 2.1 of RFC1123, in particular they
+ * consist entirely of ASCII letters, digits, hyphen, and dot.
+ *
+ * In compatibility mode (only used in CN matching), also allow:
+ * - underscore as it might be used in private host names;
+ * - non-ASCII as people might not be using Punycode as they should;
+ * and both have always "worked" historically.
+ *
+ * For now, accept any non-empty string that only consists of the allowed
+ * characters.
+ */
+static int x509_cn_looks_like_dns(const char *cn, int compat_mode)
+{
+ if (*cn == '\0') {
+ return 0;
+ }
+
+ for (const char *p = cn; *p != '\0'; p++) {
+ /* ASCII letters, digits, hyphen and dot always acceptable */
+ if ((*p >= 'a' && *p <= 'z') ||
+ (*p >= 'A' && *p <= 'Z') ||
+ (*p >= '0' && *p <= '9') ||
+ *p == '-' || *p == '.') {
+ continue;
+ }
+
+ /* In compat mode, also accept underscore and non-ASCII */
+ if (compat_mode == 1 && (*p == '_' || (unsigned char) *p >= 0x80)) {
+ continue;
}
+
+ return 0;
}
- return -1;
+ return 1;
+}
+
+/*
+ * Infer the type of CN (as MBEDTLS_X509_SAN_xxx macro) based on its value.
+ * Currently supported: IP (v4 or v6), URI, DNS (default).
+ *
+ * This takes advantage of the fact that the sets of permissible values for
+ * these types are almost disjoint (except IPv6 and URI intersect slightly,
+ * and there we choose to give IPv6 precedence). (Also, a dotted-quad IPv4 looks
+ * like a DNS name for the level of sanity checking we do, but can't be a valid
+ * FQDN as there is no purely-numeric TLD.)
+ *
+ * If the type is found to be IP, also return the IP as bytes.
+ * Otherwise, the values of ip and ip_len upon return are unspecified.
+ *
+ * Parameters:
+ * cn[in] must be a NUL-terminated string.
+ * ip[out] must point to a 4-byte-aligned 16-byte buffer
+ * iplen[out] must point to a size_t
+ *
+ * Return the type as a MBEDTLS_X509_SAN_XXX value if CN is one of the supported
+ * types, otherwise MBEDTLS_X509_SAN_NONE.
+ */
+static int x509_crt_infer_name_type(const char *cn, void *ip, size_t *ip_len)
+{
+ /* Check IP first, this avoids mistaking an IPv4 address for a DNS name,
+ * and is how we chose to resolve possible IPv6 vs URI ambiguities.
+ * It is also the only format for which we have an actual parser */
+ *ip_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
+ if (*ip_len != 0) {
+ return MBEDTLS_X509_SAN_IP_ADDRESS;
+ }
+
+ if (x509_cn_looks_like_uri(cn)) {
+ return MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
+ }
+
+ if (x509_cn_looks_like_dns(cn, 0)) {
+ return MBEDTLS_X509_SAN_DNS_NAME;
+ }
+
+ return MBEDTLS_X509_SAN_NONE;
}
/*
@@ -2964,21 +3052,44 @@ static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
const char *cn,
uint32_t *flags)
{
- const mbedtls_x509_name *name;
+ /* 16 bytes is enough for both IPv6 and IPv4. We also want the array
+ * 4-byte-aligned as the system's inet_pton() may require it. */
+ uint32_t ip[4];
+ size_t ip_len;
+
+ int name_type = x509_crt_infer_name_type(cn, ip, &ip_len);
size_t cn_len = strlen(cn);
if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
- if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
+ if (name_type == MBEDTLS_X509_SAN_IP_ADDRESS) {
+ if (x509_crt_check_san(&crt->subject_alt_names,
+ (const char *) ip, ip_len, name_type) == 0) {
+ return;
+ }
+ } else if (x509_crt_check_san(&crt->subject_alt_names,
+ cn, cn_len, name_type) == 0) {
return;
}
- } else {
+ } else if (name_type == MBEDTLS_X509_SAN_DNS_NAME ||
+ name_type == MBEDTLS_X509_SAN_NONE) {
+ const mbedtls_x509_name *name;
for (name = &crt->subject; name != NULL; name = name->next) {
- if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
- x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
+ if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) != 0) {
+ continue;
+ }
+
+ /* Legacy path: be more lax in what we match using DNS rules */
+ if (x509_cn_looks_like_dns(cn, 1) &&
+ x509_crt_check_dns_name(&name->val, cn, cn_len) == 0) {
return;
}
- }
+ /* Final compat fallback: exact byte match */
+ if (name->val.len == cn_len &&
+ memcmp(name->val.p, cn, cn_len) == 0) {
+ return;
+ }
+ }
}
*flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
diff --git a/thirdparty/mbedtls/library/x509_internal.h b/thirdparty/mbedtls/library/x509_internal.h
index f68b6593bd4..7b65331736c 100644
--- a/thirdparty/mbedtls/library/x509_internal.h
+++ b/thirdparty/mbedtls/library/x509_internal.h
@@ -32,8 +32,6 @@ int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
int *salt_len);
#endif
int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig);
-int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
- mbedtls_md_type_t *md_alg, mbedtls_pk_sigalg_t *pk_alg);
int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
mbedtls_x509_time *t);
int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
diff --git a/thirdparty/mbedtls/library/x509_oid.c b/thirdparty/mbedtls/library/x509_oid.c
index 8c67cdfa1c8..5c50e3cb328 100644
--- a/thirdparty/mbedtls/library/x509_oid.c
+++ b/thirdparty/mbedtls/library/x509_oid.c
@@ -509,7 +509,7 @@ FN_OID_GET_OID_BY_ATTR2(mbedtls_x509_oid_get_oid_by_sig_alg,
#endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CRT_WRITE_C || MBEDTLS_X509_CSR_WRITE_C */
-#if defined(MBEDTLS_X509_OID_HAVE_GET_MD_ALG)
+#if defined(MBEDTLS_X509_CRT_PARSE_C)
/*
* For digestAlgorithm
*/
@@ -598,6 +598,6 @@ static const oid_md_alg_t oid_md_alg[] =
FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg)
FN_OID_GET_ATTR1(mbedtls_x509_oid_get_md_alg, oid_md_alg_t, md_alg, mbedtls_md_type_t, md_alg)
-#endif /* MBEDTLS_X509_OID_HAVE_GET_MD_ALG */
+#endif /* MBEDTLS_X509_CRT_PARSE_C */
#endif /* some X.509 is enabled */
diff --git a/thirdparty/mbedtls/library/x509_oid.h b/thirdparty/mbedtls/library/x509_oid.h
index 5721d8586ce..617fb132bf7 100644
--- a/thirdparty/mbedtls/library/x509_oid.h
+++ b/thirdparty/mbedtls/library/x509_oid.h
@@ -107,20 +107,6 @@ int mbedtls_x509_oid_get_oid_by_sig_alg(mbedtls_pk_sigalg_t pk_alg, mbedtls_md_t
const char **oid, size_t *olen);
#endif /* MBEDTLS_X509_CRT_WRITE_C || MBEDTLS_X509_CSR_WRITE_C */
-#if (defined(MBEDTLS_X509_USE_C) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)) || \
- defined(MBEDTLS_PKCS7_C)
-#define MBEDTLS_X509_OID_HAVE_GET_MD_ALG
-/**
- * \brief Translate hash algorithm OID into md_type
- *
- * \param oid OID to use
- * \param md_alg place to store message digest algorithm
- *
- * \return 0 if successful, or MBEDTLS_ERR_X509_UNKNOWN_OID
- */
-int mbedtls_x509_oid_get_md_alg(const mbedtls_asn1_buf *oid, mbedtls_md_type_t *md_alg);
-#endif /* MBEDTLS_X509_OID_HAVE_GET_MD_ALG */
-
#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
/**
* \brief Translate Extended Key Usage OID into description
diff --git a/thirdparty/mbedtls/tf-psa-crypto/core/alignment.h b/thirdparty/mbedtls/tf-psa-crypto/core/alignment.h
index ec1edc27aab..8bb30527f3e 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/core/alignment.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/core/alignment.h
@@ -293,7 +293,7 @@ static inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x)
/*
* Detect GCC built-in byteswap routines
*/
-#if defined(__GNUC__)
+#if defined(MBEDTLS_COMPILER_IS_GCC)
#if MBEDTLS_GCC_VERSION >= 40800
#define MBEDTLS_BSWAP16 __builtin_bswap16
#endif
@@ -301,7 +301,7 @@ static inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x)
#define MBEDTLS_BSWAP32 __builtin_bswap32
#define MBEDTLS_BSWAP64 __builtin_bswap64
#endif
-#endif /* defined(__GNUC__) */
+#endif /* defined(MBEDTLS_COMPILER_IS_GCC) */
/*
* Detect Clang built-in byteswap routines
diff --git a/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto.c b/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto.c
index c6f165d29ef..cedba758bcc 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto.c
@@ -7998,25 +7998,28 @@ psa_status_t psa_key_agreement(mbedtls_svc_key_id_t private_key,
mbedtls_svc_key_id_t *key)
{
psa_status_t status;
- uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
+ uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
size_t shared_secret_len;
*key = MBEDTLS_SVC_KEY_ID_INIT;
status = validate_key_agreement_params(attributes, alg);
if (status != PSA_SUCCESS) {
- return status;
+ goto exit;
}
status = psa_raw_key_agreement(alg, private_key, peer_key, peer_key_length, shared_secret,
sizeof(shared_secret), &shared_secret_len);
if (status != PSA_SUCCESS) {
- return status;
+ goto exit;
}
status = psa_import_key(attributes, shared_secret, shared_secret_len, key);
+exit:
+ mbedtls_platform_zeroize(shared_secret, sizeof(shared_secret));
+
return status;
}
diff --git a/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers.h b/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers.h
index 9891d2ced62..99d4f17a0bf 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers.h
@@ -20,10 +20,6 @@
#include "psa_crypto_pake.h"
#include "psa_crypto_rsa.h"
-#if defined(TF_PSA_CRYPTO_PQCP_MLDSA_ENABLED)
-#include "psa_crypto_mldsa.h"
-#endif
-
#include "mbedtls/platform.h"
#include "mbedtls/constant_time.h"
/* END-common headers */
diff --git a/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers_no_static.c b/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers_no_static.c
index a8bd7eb3a96..f8eff253f52 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers_no_static.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers_no_static.c
@@ -19,10 +19,6 @@
#include "psa_crypto_pake.h"
#include "psa_crypto_rsa.h"
-#if defined(TF_PSA_CRYPTO_PQCP_MLDSA_ENABLED)
-#include "psa_crypto_mldsa.h"
-#endif
-
#include "mbedtls/platform.h"
/* END-common headers */
diff --git a/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto_its.h b/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto_its.h
index ed294fd36ae..10ce9f83228 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto_its.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/core/psa_crypto_its.h
@@ -59,8 +59,9 @@ struct psa_storage_info_t {
* \retval #PSA_ERROR_NOT_SUPPORTED The operation failed because one or more of the flags provided in `create_flags` is not supported or is not valid
* \retval #PSA_ERROR_INSUFFICIENT_STORAGE The operation failed because there was insufficient space on the storage medium
* \retval #PSA_ERROR_STORAGE_FAILURE The operation failed because the physical storage has failed (Fatal error)
- * \retval #PSA_ERROR_INVALID_ARGUMENT The operation failed because one of the provided pointers(`p_data`)
- * is invalid, for example is `NULL` or references memory the caller cannot access
+ * \retval #PSA_ERROR_INVALID_ARGUMENT The operation failed because \p uid is 0,
+ * or one of the provided pointers (`p_data`) is invalid,
+ * for example is `NULL` or references memory the caller cannot access.
*/
psa_status_t psa_its_set(psa_storage_uid_t uid,
uint32_t data_length,
@@ -83,8 +84,9 @@ psa_status_t psa_its_set(psa_storage_uid_t uid,
* \retval #PSA_ERROR_DOES_NOT_EXIST The operation failed because the provided `uid` value was not found in the storage
* \retval #PSA_ERROR_STORAGE_FAILURE The operation failed because the physical storage has failed (Fatal error)
* \retval #PSA_ERROR_DATA_CORRUPT The operation failed because stored data has been corrupted
- * \retval #PSA_ERROR_INVALID_ARGUMENT The operation failed because one of the provided pointers(`p_data`, `p_data_length`)
- * is invalid. For example is `NULL` or references memory the caller cannot access.
+ * \retval #PSA_ERROR_INVALID_ARGUMENT The operation failed because \p uid is 0,
+ * or one of the provided pointers (`p_data`) is invalid,
+ * for example is `NULL` or references memory the caller cannot access.
* In addition, this can also happen if an invalid offset was provided.
*/
psa_status_t psa_its_get(psa_storage_uid_t uid,
@@ -104,8 +106,9 @@ psa_status_t psa_its_get(psa_storage_uid_t uid,
* \retval #PSA_SUCCESS The operation completed successfully
* \retval #PSA_ERROR_DOES_NOT_EXIST The operation failed because the provided uid value was not found in the storage
* \retval #PSA_ERROR_DATA_CORRUPT The operation failed because stored data has been corrupted
- * \retval #PSA_ERROR_INVALID_ARGUMENT The operation failed because one of the provided pointers(`p_info`)
- * is invalid, for example is `NULL` or references memory the caller cannot access
+ * \retval #PSA_ERROR_INVALID_ARGUMENT The operation failed because \p uid is 0,
+ * or the provided pointer (`p_info`) is invalid,
+ * for example is `NULL` or references memory the caller cannot access.
*/
psa_status_t psa_its_get_info(psa_storage_uid_t uid,
struct psa_storage_info_t *p_info);
@@ -121,6 +124,7 @@ psa_status_t psa_its_get_info(psa_storage_uid_t uid,
* \retval #PSA_ERROR_DOES_NOT_EXIST The operation failed because the provided key value was not found in the storage
* \retval #PSA_ERROR_NOT_PERMITTED The operation failed because the provided key value was created with PSA_STORAGE_FLAG_WRITE_ONCE
* \retval #PSA_ERROR_STORAGE_FAILURE The operation failed because the physical storage has failed (Fatal error)
+ * \retval #PSA_ERROR_INVALID_ARGUMENT The operation failed because \p uid is 0.
*/
psa_status_t psa_its_remove(psa_storage_uid_t uid);
diff --git a/thirdparty/mbedtls/tf-psa-crypto/core/psa_its_file.c b/thirdparty/mbedtls/tf-psa-crypto/core/psa_its_file.c
index 79290b29ebf..1421c6e6a94 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/core/psa_its_file.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/core/psa_its_file.c
@@ -79,6 +79,10 @@ static psa_status_t psa_its_read_file(psa_storage_uid_t uid,
psa_its_file_header_t header;
size_t n;
+ if (uid == 0) {
+ return PSA_ERROR_INVALID_ARGUMENT;
+ }
+
*p_stream = NULL;
psa_its_fill_filename(uid, filename);
*p_stream = fopen(filename, "rb");
@@ -178,7 +182,7 @@ psa_status_t psa_its_set(psa_storage_uid_t uid,
psa_storage_create_flags_t create_flags)
{
if (uid == 0) {
- return PSA_ERROR_INVALID_HANDLE;
+ return PSA_ERROR_INVALID_ARGUMENT;
}
psa_status_t status = PSA_ERROR_STORAGE_FAILURE;
@@ -239,6 +243,11 @@ psa_status_t psa_its_remove(psa_storage_uid_t uid)
{
char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
FILE *stream;
+
+ if (uid == 0) {
+ return PSA_ERROR_INVALID_ARGUMENT;
+ }
+
psa_its_fill_filename(uid, filename);
stream = fopen(filename, "rb");
if (stream == NULL) {
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/bignum.h b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/bignum.h
index 8e8290e6238..7d5bb065541 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/bignum.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/bignum.h
@@ -1025,7 +1025,6 @@ int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
*
* \param X The MPI to check for primality.
* This must point to an initialized MPI.
- * The value must be larger than 997.
* \param rounds The number of bases to perform the Miller-Rabin primality
* test for. The probability of returning 0 on a composite is
* at most 2<sup>-2*\p rounds </sup>.
@@ -1037,7 +1036,6 @@ int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
* \return \c 0 if successful, i.e. \p X is probably prime.
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
* \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime.
- * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p X is too small.
* \return Another negative error code on other kinds of failure.
*/
int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
@@ -1060,7 +1058,7 @@ typedef enum {
* \param X The destination MPI to store the generated prime in.
* This must point to an initialized MPi.
* \param nbits The required size of the destination MPI in bits.
- * This must be between \c 12 and #MBEDTLS_MPI_MAX_BITS.
+ * This must be between \c 3 and #MBEDTLS_MPI_MAX_BITS.
* \param flags A mask of flags of type #mbedtls_mpi_gen_prime_flag_t.
* \param f_rng The RNG function to use. This must not be \c NULL.
* \param p_rng The RNG parameter to be passed to \p f_rng.
@@ -1071,7 +1069,7 @@ typedef enum {
* probably prime number.
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if `nbits` is not between
- * \c 12 and #MBEDTLS_MPI_MAX_BITS.
+ * \c 3 and #MBEDTLS_MPI_MAX_BITS.
*/
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
int (*f_rng)(void *, unsigned char *, size_t),
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/pkcs5.h b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/pkcs5.h
index 94c56f1cbbb..dc982eae2b2 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/pkcs5.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/pkcs5.h
@@ -38,6 +38,15 @@
extern "C" {
#endif
+/* Add a default value for MBEDTLS_PK_DECRYPT_MAX_ITERATIONS in case
+ * it's not defined in the user configuration file.
+ * This value is chosen to be valid for almost any real use case and at the
+ * same time be computationally acceptable in terms of execution time.
+ */
+#if !defined(MBEDTLS_PK_DECRYPT_MAX_ITERATIONS)
+#define MBEDTLS_PK_DECRYPT_MAX_ITERATIONS 10000000
+#endif
+
#if defined(MBEDTLS_ASN1_PARSE_C) && defined(MBEDTLS_CIPHER_C)
#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/aes.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/aes.c
index 0bb4ff48960..977bac48dd9 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/aes.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/aes.c
@@ -36,7 +36,7 @@
#if defined(MBEDTLS_AESCE_C)
#include "aesce.h"
#endif
-
+#include "constant_time_internal.h"
#include "mbedtls/platform.h"
#include "ctr.h"
@@ -48,6 +48,63 @@
#define MBEDTLS_AES_NEED_REVERSE_TABLES
#endif
+/* 1 if hardware support is present at runtime, 0 otherwise.
+ * Undefined macro if no hardware support code is enabled.
+ *
+ * This macro is only used for re-checking, so it can directly
+ * reference a variable, it doesn't need to call the checking code.
+ */
+#if defined(MBEDTLS_AESNI_HAVE_CODE)
+#define MBEDTLS_AES_HAVE_HARDWARE \
+ mbedtls_aesni_has_support(MBEDTLS_AESNI_AES)
+#endif
+#if defined(MBEDTLS_AESCE_HAVE_CODE)
+#define MBEDTLS_AES_HAVE_HARDWARE MBEDTLS_AESCE_HAS_SUPPORT_RECHECK
+#endif
+
+#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
+/* Call this function before executing non-constant-time AES code
+ * (key schedule or block encryption/decryption).
+ *
+ * On platforms with speculative execution, the CPU may start
+ * executing the non-constant-time software fallback speculatively.
+ * This would leak part of the key or plaintext to a powerful local
+ * attacker who can observe precise memory access patterns.
+ *
+ * Calling this function sets `*pkey` to `NULL` if hardware AES
+ * is available.
+ */
+static void mbedtls_aes_key_block_speculation_of_no_hardware(
+ const unsigned char **pkey)
+{
+#if defined(MBEDTLS_SPECTRE_V1_COUNTERMEASURES) && \
+ defined(MBEDTLS_AES_HAVE_HARDWARE)
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(*pkey, MBEDTLS_AES_HAVE_HARDWARE, 0);
+#else
+ (void) pkey;
+#endif
+}
+
+/* Same as mbedtls_aes_key_block_speculation_of_no_hardware(),
+ * but acts on a pointer to a context, not to a key.
+ *
+ * (The two functions are extremeley likely to have exactly the same code,
+ * but they act on pointers to different types, which are not compatible
+ * according to C rules, and lying about it could confuse the compiler's
+ * aliasing detection.)
+ */
+static void mbedtls_aes_ctx_block_speculation_of_no_hardware(
+ mbedtls_aes_context **pctx)
+{
+#if defined(MBEDTLS_SPECTRE_V1_COUNTERMEASURES) && \
+ defined(MBEDTLS_AES_HAVE_HARDWARE)
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(*pctx, MBEDTLS_AES_HAVE_HARDWARE, 0);
+#else
+ (void) pctx;
+#endif
+}
+#endif /* !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
+
#if defined(MBEDTLS_AES_ROM_TABLES)
/*
* Forward S-box
@@ -600,6 +657,8 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
#endif
#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
+ mbedtls_aes_key_block_speculation_of_no_hardware(&key);
+
for (unsigned int i = 0; i < (keybits >> 5); i++) {
RK[i] = MBEDTLS_GET_UINT32_LE(key, i << 2);
}
@@ -676,14 +735,10 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
unsigned int keybits)
{
-#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
- uint32_t *SK;
-#endif
int ret;
mbedtls_aes_context cty;
uint32_t *RK;
-
mbedtls_aes_init(&cty);
ctx->rk_offset = mbedtls_aes_rk_offset(ctx->buf);
@@ -715,7 +770,10 @@ int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
#endif
#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
- SK = cty.buf + cty.rk_offset + cty.nr * 4;
+ const unsigned char *SK_address =
+ (const unsigned char *) (cty.buf + cty.rk_offset + cty.nr * 4);
+ mbedtls_aes_key_block_speculation_of_no_hardware(&SK_address);
+ const uint32_t *SK = (const uint32_t *) SK_address;
*RK++ = *SK++;
*RK++ = *SK++;
@@ -1039,6 +1097,7 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
#endif
#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
+ mbedtls_aes_ctx_block_speculation_of_no_hardware(&ctx);
#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
if (mode == MBEDTLS_AES_DECRYPT) {
return mbedtls_internal_aes_decrypt(ctx, input, output);
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/aesce.h b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/aesce.h
index fcc4acdff64..e5467d0e6bf 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/aesce.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/aesce.h
@@ -46,6 +46,9 @@ int mbedtls_aesce_has_support_impl(void);
#define MBEDTLS_AESCE_HAS_SUPPORT() (mbedtls_aesce_has_support_result == -1 ? \
mbedtls_aesce_has_support_impl() : \
mbedtls_aesce_has_support_result)
+/* Access the variable directly. This means fewer branches and
+ * therefore fewer concerns about wrong speculation here. */
+#define MBEDTLS_AESCE_HAS_SUPPORT_RECHECK mbedtls_aesce_has_support_result
#else /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
@@ -56,6 +59,10 @@ int mbedtls_aesce_has_support_impl(void);
#endif /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
+#if !defined(MBEDTLS_AESCE_HAS_SUPPORT_RECHECK)
+#define MBEDTLS_AESCE_HAS_SUPPORT_RECHECK MBEDTLS_AESCE_HAS_SUPPORT()
+#endif
+
/**
* \brief Internal AES-ECB block encryption and decryption
*
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum.c
index b1607d15b2d..385fec823cf 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum.c
@@ -2059,44 +2059,30 @@ int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi
#if defined(MBEDTLS_GENPRIME)
-/* Largest "small prime" tested. If this is ever changed, must update:
- * - the product below obviously,
- * - the documentation and tests of mbedtls_mpi_is_prime_ext() about this limit,
- * - documentation tests and code of mbedtls_mpi_gen_prime() about the minimum
- * value of nbits.
- */
-static const mbedtls_mpi_sint small_primes_limit = 997;
-/* Product of small primes up to small_primes_limit included */
-static const mbedtls_mpi_uint small_primes_product_limbs[] = {
- MBEDTLS_BYTES_TO_T_UINT_8(0x4b, 0x13, 0x6a, 0x97, 0xbb, 0xd0, 0xdf, 0x95),
- MBEDTLS_BYTES_TO_T_UINT_8(0xa7, 0x2c, 0x10, 0xa4, 0x20, 0xa4, 0x9f, 0x7b),
- MBEDTLS_BYTES_TO_T_UINT_8(0x9d, 0x18, 0xd6, 0xdf, 0xc0, 0xf5, 0x61, 0x65),
- MBEDTLS_BYTES_TO_T_UINT_8(0xfc, 0x35, 0x79, 0xfb, 0x30, 0xa8, 0xd5, 0xbf),
- MBEDTLS_BYTES_TO_T_UINT_8(0xdb, 0x37, 0xba, 0x2c, 0xfb, 0xbb, 0x89, 0xfb),
- MBEDTLS_BYTES_TO_T_UINT_8(0xad, 0xc2, 0x8c, 0x1d, 0x99, 0x18, 0xe8, 0xe5),
- MBEDTLS_BYTES_TO_T_UINT_8(0x8d, 0x77, 0xc9, 0x5d, 0x96, 0x8a, 0x61, 0x9d),
- MBEDTLS_BYTES_TO_T_UINT_8(0x39, 0x41, 0x3e, 0xf4, 0x34, 0x07, 0x57, 0xe0),
- MBEDTLS_BYTES_TO_T_UINT_8(0x4a, 0xf1, 0x54, 0x3a, 0x43, 0x67, 0x46, 0xa2),
- MBEDTLS_BYTES_TO_T_UINT_8(0x83, 0x0c, 0xe5, 0x31, 0xa2, 0xfc, 0x05, 0x45),
- MBEDTLS_BYTES_TO_T_UINT_8(0xf0, 0x1d, 0x66, 0xfc, 0x7a, 0x85, 0x37, 0xe1),
- MBEDTLS_BYTES_TO_T_UINT_8(0x17, 0xe0, 0x80, 0x62, 0x0d, 0xa2, 0xbc, 0x32),
- MBEDTLS_BYTES_TO_T_UINT_8(0x6d, 0xce, 0x84, 0x68, 0x00, 0xb5, 0xe3, 0x35),
- MBEDTLS_BYTES_TO_T_UINT_8(0x14, 0x19, 0x0d, 0xe4, 0x92, 0xd5, 0xd8, 0xdf),
- MBEDTLS_BYTES_TO_T_UINT_8(0x20, 0x1c, 0x7d, 0x38, 0x3b, 0xe8, 0xd9, 0xa8),
- MBEDTLS_BYTES_TO_T_UINT_8(0xf6, 0xac, 0x11, 0xe6, 0xb4, 0x03, 0xf7, 0x6c),
- MBEDTLS_BYTES_TO_T_UINT_8(0x78, 0x3d, 0xf2, 0x3a, 0x8f, 0xf9, 0x2f, 0x6a),
- MBEDTLS_BYTES_TO_T_UINT_8(0x7b, 0x72, 0x66, 0xa9, 0x48, 0xe4, 0x6d, 0x02),
- MBEDTLS_BYTES_TO_T_UINT_8(0x69, 0xc7, 0x32, 0xcb, 0xf2, 0xf7, 0xa9, 0x0b),
- MBEDTLS_BYTES_TO_T_UINT_8(0xd3, 0x52, 0x72, 0x9d, 0xbf, 0x54, 0xea, 0xc7),
- MBEDTLS_BYTES_TO_T_UINT_8(0xf5, 0xf3, 0x8a, 0xc5, 0xf0, 0xe1, 0x21, 0x81),
- MBEDTLS_BYTES_TO_T_UINT_8(0x8e, 0x61, 0x78, 0xf0, 0x05, 0x00, 0x00, 0x00),
-};
-/* Could make ECP_MPI_INIT_ARRAY() available outside ecp, but not doing it now
- * as it would lead to conflicts with other in-flight PRs. */
-static const mbedtls_mpi small_primes_product = {
- .p = (mbedtls_mpi_uint *) small_primes_product_limbs,
- .s = 1,
- .n = sizeof(small_primes_product_limbs) / sizeof(mbedtls_mpi_uint),
+/* Gaps between primes, starting at 3. https://oeis.org/A001223 */
+static const unsigned char small_prime_gaps[] = {
+ 2, 2, 4, 2, 4, 2, 4, 6,
+ 2, 6, 4, 2, 4, 6, 6, 2,
+ 6, 4, 2, 6, 4, 6, 8, 4,
+ 2, 4, 2, 4, 14, 4, 6, 2,
+ 10, 2, 6, 6, 4, 6, 6, 2,
+ 10, 2, 4, 2, 12, 12, 4, 2,
+ 4, 6, 2, 10, 6, 6, 6, 2,
+ 6, 4, 2, 10, 14, 4, 2, 4,
+ 14, 6, 10, 2, 4, 6, 8, 6,
+ 6, 4, 6, 8, 4, 8, 10, 2,
+ 10, 2, 6, 4, 6, 8, 4, 2,
+ 4, 12, 8, 4, 8, 4, 6, 12,
+ 2, 18, 6, 10, 6, 6, 2, 6,
+ 10, 6, 6, 2, 6, 6, 4, 2,
+ 12, 10, 2, 4, 6, 6, 2, 12,
+ 4, 6, 8, 10, 8, 10, 8, 6,
+ 6, 4, 8, 6, 4, 8, 4, 14,
+ 10, 12, 2, 10, 2, 4, 2, 10,
+ 14, 4, 2, 4, 14, 4, 2, 4,
+ 20, 4, 8, 10, 8, 4, 6, 6,
+ 14, 4, 6, 6, 8, 6, /*reaches 997*/
+ 0 /* the last entry is effectively unused */
};
/*
@@ -2104,41 +2090,33 @@ static const mbedtls_mpi small_primes_product = {
*
* Return values:
* 0: no small factor (possible prime, more tests needed)
+ * 1: certain prime
* MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
- * MBEDTLS_ERR_MPI_BAD_INPUT_DATA: input too small
* other negative: error
*/
static int mpi_check_small_factors(const mbedtls_mpi *X)
{
int ret = 0;
- mbedtls_mpi g;
-
- mbedtls_mpi_init(&g);
-
- /* The GCD test below only works if X > small_primes_limit. */
- if (mbedtls_mpi_cmp_int(X, small_primes_limit) <= 0) {
- return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
- }
+ size_t i;
+ mbedtls_mpi_uint r;
+ unsigned p = 3; /* The first odd prime */
if ((X->p[0] & 1) == 0) {
return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
}
- /* We can't directly use mbedtls_mpi_gcd_modinv_odd() because we don't know
- * if X is larger than prod or not (prod is 1380 bits). So, use this generic
- * wrapper - it does a bit more than what we need (handles even inputs as
- * well, while we know our inputs are both odd), but that's OK. */
- MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&g, &small_primes_product, X));
-
- if (mbedtls_mpi_cmp_int(&g, 1) == 0) {
- /* X is not divisible by a small prime */
- ret = 0;
- } else {
- ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
+ for (i = 0; i < sizeof(small_prime_gaps); p += small_prime_gaps[i], i++) {
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, p));
+ if (r == 0) {
+ if (mbedtls_mpi_cmp_int(X, p) == 0) {
+ return 1;
+ } else {
+ return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
+ }
+ }
}
cleanup:
- mbedtls_mpi_free(&g);
return ret;
}
@@ -2239,12 +2217,30 @@ int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
void *p_rng)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
+ mbedtls_mpi XX;
+
+ XX.s = 1;
+ XX.n = X->n;
+ XX.p = X->p;
+
+ if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
+ mbedtls_mpi_cmp_int(&XX, 1) == 0) {
+ return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
+ }
+
+ if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
+ return 0;
+ }
+
+ if ((ret = mpi_check_small_factors(&XX)) != 0) {
+ if (ret == 1) {
+ return 0;
+ }
- if ((ret = mpi_check_small_factors(X)) != 0) {
return ret;
}
- return mpi_miller_rabin(X, rounds, f_rng, p_rng);
+ return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
}
/*
@@ -2271,11 +2267,7 @@ int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
mbedtls_mpi_uint r;
mbedtls_mpi Y;
- /* The minimum value must be such that 2^(nbits - 2) > small_primes_limit,
- * so we can use mbedtls_mpi_is_prime_ext(). The -2 comes from:
- * - first -1 because X will be generated with 2^(nbits-1) <= X < 2^nbits;
- * - another -1 because for safe primes we test (X-1) / 2. */
- if (nbits < 12 || nbits > MBEDTLS_MPI_MAX_BITS) {
+ if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.h b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.h
index 3c41f921dbe..dcad2eb3d61 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.h
@@ -536,6 +536,8 @@ void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
* the square of the Montgomery constant:
* 2^{2*n*biL} mod N.
* \param[in] N Little-endian presentation of the modulus, which must be odd.
+ * It must not have leading zero limbs, i.e. `N->n`
+ * must be the number of significant limbs of the modulus.
*
* \return 0 if successful.
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod.h b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod.h
index 6d2db592572..e016f462c9e 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod.h
@@ -186,6 +186,7 @@ void mbedtls_mpi_mod_modulus_init(mbedtls_mpi_mod_modulus *N);
*
* \param[out] N The address of the modulus structure to populate.
* \param[in] p The address of the limb array storing the value of \p N.
+ * The most significant limb must be nonzero.
* The memory pointed to by \p p will be used by \p N and must
* not be modified in any way until after
* mbedtls_mpi_mod_modulus_free() is called.
@@ -201,6 +202,7 @@ int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N,
*
* \param[out] N The address of the modulus structure to populate.
* \param[in] p The address of the limb array storing the value of \p N.
+ * The most significant limb must be nonzero.
* The memory pointed to by \p p will be used by \p N and must
* not be modified in any way until after
* mbedtls_mpi_mod_modulus_free() is called.
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher.c
index dd44872729d..f7e02845167 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher.c
@@ -250,7 +250,11 @@ int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
}
#if defined(MBEDTLS_CHACHA20_C)
- if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
+ if (MBEDTLS_CT_SPEC_RISKY_BOOL(ctx->cipher_info->type ==
+ MBEDTLS_CIPHER_CHACHA20)) {
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(ctx,
+ ctx->cipher_info->type,
+ MBEDTLS_CIPHER_CHACHA20);
/* Even though the actual_iv_size is overwritten with a correct value
* of 12 from the cipher info, return an error to indicate that
* the input iv_len is wrong. */
@@ -273,7 +277,11 @@ int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
#endif
#if defined(MBEDTLS_GCM_C)
- if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
+ if (MBEDTLS_CT_SPEC_RISKY_BOOL(MBEDTLS_MODE_GCM ==
+ ctx->cipher_info->mode)) {
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(ctx,
+ MBEDTLS_MODE_GCM,
+ ctx->cipher_info->mode);
return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
ctx->operation,
iv, iv_len);
@@ -281,7 +289,11 @@ int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
#endif
#if defined(MBEDTLS_CCM_C)
- if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
+ if (MBEDTLS_CT_SPEC_RISKY_BOOL(MBEDTLS_MODE_CCM_STAR_NO_TAG ==
+ ctx->cipher_info->mode)) {
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(ctx,
+ MBEDTLS_MODE_CCM_STAR_NO_TAG,
+ ctx->cipher_info->mode);
int set_lengths_result;
int ccm_star_mode;
@@ -334,14 +346,22 @@ int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
}
#if defined(MBEDTLS_GCM_C)
- if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
+ if (MBEDTLS_CT_SPEC_RISKY_BOOL(MBEDTLS_MODE_GCM ==
+ ctx->cipher_info->mode)) {
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(ctx,
+ MBEDTLS_MODE_GCM,
+ ctx->cipher_info->mode);
return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
ad, ad_len);
}
#endif
#if defined(MBEDTLS_CHACHAPOLY_C)
- if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
+ if (MBEDTLS_CT_SPEC_RISKY_BOOL(MBEDTLS_CIPHER_CHACHA20_POLY1305 ==
+ ctx->cipher_info->type)) {
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(ctx,
+ MBEDTLS_CIPHER_CHACHA20_POLY1305,
+ ctx->cipher_info->type);
int result;
mbedtls_chachapoly_mode_t mode;
@@ -398,7 +418,11 @@ int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *in
}
#if defined(MBEDTLS_GCM_C)
- if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
+ if (MBEDTLS_CT_SPEC_RISKY_BOOL(MBEDTLS_MODE_GCM ==
+ ctx->cipher_info->mode)) {
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(ctx,
+ MBEDTLS_MODE_GCM,
+ ctx->cipher_info->mode);
return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
input, ilen,
output, ilen, olen);
@@ -406,7 +430,11 @@ int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *in
#endif
#if defined(MBEDTLS_CCM_C)
- if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
+ if (MBEDTLS_CT_SPEC_RISKY_BOOL(MBEDTLS_MODE_CCM_STAR_NO_TAG ==
+ ctx->cipher_info->mode)) {
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(ctx,
+ MBEDTLS_MODE_CCM_STAR_NO_TAG,
+ ctx->cipher_info->mode);
return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
input, ilen,
output, ilen, olen);
@@ -414,11 +442,18 @@ int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *in
#endif
#if defined(MBEDTLS_CHACHAPOLY_C)
- if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
+ if (MBEDTLS_CT_SPEC_RISKY_BOOL(MBEDTLS_CIPHER_CHACHA20_POLY1305 ==
+ ctx->cipher_info->type)) {
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(ctx,
+ MBEDTLS_CIPHER_CHACHA20_POLY1305,
+ ctx->cipher_info->type);
*olen = ilen;
return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
ilen, input, output);
}
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_NE(ctx,
+ MBEDTLS_CIPHER_CHACHA20_POLY1305,
+ ctx->cipher_info->type);
#endif
if (input == output &&
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ecdsa.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ecdsa.c
index 90a5fe17cf9..5dca906958c 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ecdsa.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ecdsa.c
@@ -18,6 +18,8 @@
#include "mbedtls/private/ecdsa.h"
#include "mbedtls/asn1write.h"
#include "bignum_internal.h"
+#include "bignum_core.h"
+#include "ecp_invasive.h"
#include <string.h>
@@ -232,6 +234,112 @@ int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid)
}
}
+/* Copy an MPI into a zero-padded, fixed-width limb array. */
+static void ecdsa_copy_limbs(mbedtls_mpi_uint *x, size_t limbs,
+ const mbedtls_mpi *a)
+{
+ if (a->n == 0) {
+ /* This shouldn't happen, but to be on the safe side,
+ * avoid undefined behavior if a is 0 with a->p==NULL. */
+ return;
+ }
+ size_t copy_limbs = a->n < limbs ? a->n : limbs;
+ memcpy(x, a->p, copy_limbs * sizeof(*x));
+}
+
+MBEDTLS_STATIC_TESTABLE int mbedtls_ecdsa_compute_s(const mbedtls_mpi *N,
+ const mbedtls_mpi *d,
+ const mbedtls_mpi *k,
+ mbedtls_mpi *e,
+ const mbedtls_mpi *r,
+ mbedtls_mpi *s)
+{
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
+ const size_t limbs = N->n;
+
+ mbedtls_mpi_uint *scratch = NULL;
+ mbedtls_mpi mont_r2;
+ mbedtls_mpi_init(&mont_r2);
+
+ /* The Montgomery radix is based on the significant limb count. */
+ MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&mont_r2, N));
+ mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
+
+ /* Beyond this point, we may have allocated memory, so cleanup
+ * is needed on error. */
+
+ /* Make sure s has room for the number of limbs we're working with.
+ * Zero it out, because later we'll focus on `limbs` limbs and
+ * we won't touch the other limbs if there are more. */
+ MBEDTLS_MPI_CHK(mbedtls_mpi_grow(s, limbs));
+ mbedtls_mpi_lset(s, 0);
+
+ /* e may be smaller than N when signing a smaller hash. Grow it
+ * to the size we use here. */
+ MBEDTLS_MPI_CHK(mbedtls_mpi_grow(e, limbs));
+
+ /* Get room for 3 temporaries (r_mont, d_mont, inv_k_mont),
+ * plus the workspace for modular inversion (5 * limbs). */
+ size_t scratch_limbs = (3 + 5) * limbs;
+ scratch = mbedtls_calloc(scratch_limbs, sizeof(*scratch));
+ if (scratch == NULL) {
+ ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
+ goto cleanup;
+ }
+
+ mbedtls_mpi_uint *r_mont = scratch; /* 0 */
+ mbedtls_mpi_uint *d_mont = r_mont + limbs; /* 1 */
+ mbedtls_mpi_uint *inv_k_mont = d_mont + limbs; /* 2 */
+ mbedtls_mpi_uint *working = inv_k_mont + limbs; /* 3-7 */
+
+ /* Invert the nonce at the fixed width of the group N.
+ * We already know that k is in the range [1, N-1] and is represented
+ * with as many limbs as N.
+ * Convert the result to Montgomery representation.
+ */
+ mbedtls_mpi_core_gcd_modinv_odd(s->p, inv_k_mont, k->p, limbs, N->p, limbs, working);
+ mbedtls_mpi_core_to_mont_rep(inv_k_mont, inv_k_mont, N->p, limbs, mm,
+ mont_r2.p, working);
+
+ /* Make a copy of r in Montgomery representation. */
+ ecdsa_copy_limbs(r_mont, limbs, r);
+ mbedtls_mpi_core_to_mont_rep(r_mont, r_mont, N->p, limbs, mm,
+ mont_r2.p, working);
+
+ /* Make a copy of d in Montgomery representation. */
+ ecdsa_copy_limbs(d_mont, limbs, d);
+ mbedtls_mpi_core_to_mont_rep(d_mont, d_mont, N->p, limbs, mm,
+ mont_r2.p, working);
+
+ /* Convert e to Montgomery representation in place. */
+ mbedtls_mpi_core_to_mont_rep(e->p, e->p, N->p, limbs, mm,
+ mont_r2.p, working);
+
+ /* s := r * d mod N */
+ mbedtls_mpi_core_montmul(s->p, r_mont, d_mont, limbs, N->p, limbs,
+ mm, working);
+
+ /* s := r * d + e mod N */
+ mbedtls_mpi_uint carry = mbedtls_mpi_core_add(s->p, s->p, e->p, limbs);
+ mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(e->p, s->p, N->p, limbs);
+ mbedtls_mpi_core_cond_assign(s->p, e->p, limbs,
+ mbedtls_ct_bool(carry | (borrow ^ 1)));
+
+ /* s := (r * d + e) * k^-1 mod N */
+ mbedtls_mpi_core_montmul(s->p, s->p, inv_k_mont, limbs, N->p, limbs,
+ mm, working);
+
+ /* Convert the output from Montgomery to standard representation. */
+ mbedtls_mpi_core_from_mont_rep(s->p, s->p, N->p, limbs, mm, working);
+
+cleanup:
+ if (scratch != NULL) {
+ mbedtls_zeroize_and_free(scratch, scratch_limbs * sizeof(*scratch));
+ }
+ mbedtls_mpi_free(&mont_r2);
+ return ret;
+}
+
/*
* Compute ECDSA signature of a hashed message (SEC1 4.1.3)
* Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
@@ -338,11 +446,8 @@ modn:
/*
* Step 6: compute s = (e + r * d) / k
*/
- MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, pr, d));
- MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&e, &e, s));
- MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(NULL, s, pk, &grp->N));
- MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, s, &e));
- MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(s, s, &grp->N));
+ MBEDTLS_MPI_CHK(mbedtls_ecdsa_compute_s(&grp->N, d, pk,
+ &e, pr, s));
} while (mbedtls_mpi_cmp_int(s, 0) == 0);
#if defined(MBEDTLS_ECP_RESTARTABLE)
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_curves.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_curves.c
index e0393e470b5..ecf64b98fc3 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_curves.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_curves.c
@@ -3928,14 +3928,14 @@ int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id)
#if defined(MBEDTLS_HAVE_INT32) /* 32 bit */
-#define MAX32 X_limbs
+#define MAX32 (X_limbs)
#define A(j) X[j]
#define STORE32 X[i] = (mbedtls_mpi_uint) cur;
#define STORE0 X[i] = 0;
#else /* 64 bit */
-#define MAX32 X_limbs * 2
+#define MAX32 (X_limbs * 2)
#define A(j) \
(j) % 2 ? \
(uint32_t) (X[(j) / 2] >> 32) : \
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_invasive.h b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_invasive.h
index 85acf814e31..155976ca0c4 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_invasive.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_invasive.h
@@ -217,4 +217,39 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_LIGHT */
+#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_TEST_HOOKS)
+/** Compute s = (e + r * d) / k mod N in ECDSA.
+ *
+ * \param[in] N Group order.
+ * It must not have a leading zero limb.
+ * It must be odd.
+ * \param[in] d Private key.
+ * It must be between 1 and N-1.
+ * \param[in] k The nonce k.
+ * It must be between 1 and N-1.
+ * `k->n` must be at least as large as `N->n`.
+ * \param[in,out] e On input, the normalized message hash.
+ * It must be between 0 and N-1.
+ * Clobbered on output.
+ * \param[in] r The r value of the signature.
+ * It must be between 1 and N-1.
+ * \param[out] s On output, the s value of the signature.
+ * On input, it must be initialized, but the
+ * value is not otherwise used.
+ *
+ * \return \c 0 on success, or a negative error code.
+ *
+ * \note This is an internal function. If the assumptions on MPI sizes and
+ * values are violated, no memory corruption should happen on typical
+ * platforms, but the function may return 0 with nonsensical output
+ * that may leak secret data.
+ */
+int mbedtls_ecdsa_compute_s(const mbedtls_mpi *N,
+ const mbedtls_mpi *d,
+ const mbedtls_mpi *k,
+ mbedtls_mpi *e,
+ const mbedtls_mpi *r,
+ mbedtls_mpi *s);
+#endif /* MBEDTLS_ECDSA_C && MBEDTLS_TEST_HOOKS */
+
#endif /* TF_PSA_CRYPTO_ECP_INVASIVE_H */
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/gcm.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/gcm.c
index b9412a7b6dc..aafcbc099b6 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/gcm.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/gcm.c
@@ -34,10 +34,10 @@
#if defined(MBEDTLS_AESNI_C)
#include "aesni.h"
#endif
-
#if defined(MBEDTLS_AESCE_C)
#include "aesce.h"
#endif
+#include "constant_time_internal.h"
/* Used to select the acceleration mechanism */
#define MBEDTLS_GCM_ACC_SMALLTABLE 0
@@ -45,6 +45,34 @@
#define MBEDTLS_GCM_ACC_AESNI 2
#define MBEDTLS_GCM_ACC_AESCE 3
+/* 1 if hardware support is present at runtime, 0 otherwise.
+ * Undefined macro if no hardware support code is enabled.
+ *
+ * This macro is only used for re-checking, so it can directly
+ * reference a variable, it doesn't need to call the checking code.
+ */
+#if defined(MBEDTLS_AESNI_HAVE_CODE)
+#define MBEDTLS_GCM_HAVE_HARDWARE \
+ mbedtls_aesni_has_support(MBEDTLS_AESNI_CLMUL)
+#endif
+#if defined(MBEDTLS_AESCE_HAVE_CODE)
+#define MBEDTLS_GCM_HAVE_HARDWARE MBEDTLS_AESCE_HAS_SUPPORT_RECHECK
+#endif
+
+/* Similar to mbedtls_aes_block_speculation_of_no_hardware() in aes.c.
+ * See the documentation of that function. */
+static void mbedtls_gcm_block_speculation_of_no_hardware(
+ mbedtls_gcm_context **pctx)
+{
+#if defined(MBEDTLS_SPECTRE_V1_COUNTERMEASURES) && \
+ defined(MBEDTLS_GCM_HAVE_HARDWARE)
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(*pctx, MBEDTLS_GCM_HAVE_HARDWARE, 0);
+#else
+ (void) pctx;
+#endif
+
+}
+
/*
* Initialize a context
*/
@@ -128,6 +156,8 @@ static int gcm_gen_table(mbedtls_gcm_context *ctx)
#endif
default:
+ mbedtls_gcm_block_speculation_of_no_hardware(&ctx);
+
/* 0 corresponds to 0 in GF(2^128) */
ctx->H[0][0] = 0;
ctx->H[0][1] = 0;
@@ -360,10 +390,12 @@ static void gcm_mult(mbedtls_gcm_context *ctx, const unsigned char x[16],
#if defined(MBEDTLS_GCM_LARGE_TABLE)
case MBEDTLS_GCM_ACC_LARGETABLE:
+ mbedtls_gcm_block_speculation_of_no_hardware(&ctx);
gcm_mult_largetable(output, x, ctx->H);
break;
#else
case MBEDTLS_GCM_ACC_SMALLTABLE:
+ mbedtls_gcm_block_speculation_of_no_hardware(&ctx);
gcm_mult_smalltable(output, x, ctx->H);
break;
#endif
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/md5.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/md5.c
index 01397b70d2b..ae7f6b8f625 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/md5.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/md5.c
@@ -217,6 +217,10 @@ int mbedtls_md5_update(mbedtls_md5_context *ctx,
ctx->total[1]++;
}
+#if SIZE_MAX > 0xFFFFFFFF
+ ctx->total[1] += (uint32_t) (ilen >> 32);
+#endif
+
if (left && ilen >= fill) {
memcpy((void *) (ctx->buffer + left), input, fill);
if ((ret = mbedtls_internal_md5_process(ctx, ctx->buffer)) != 0) {
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c
index 767dde04d96..f34b6e1ce82 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c
@@ -15,6 +15,7 @@
#include "psa_crypto_ecp.h"
#include "psa_crypto_random_impl.h"
#include "psa_util_internal.h"
+#include "constant_time_internal.h"
#include <stdlib.h>
#include <string.h>
@@ -101,6 +102,14 @@ psa_status_t mbedtls_psa_ecp_load_representation(
size_t curve_bytes = data_length;
int explicit_bits = (curve_bits != 0);
+ /* Avoid potentially leaking information through the timing of the
+ * parsing of the key material if this function was called
+ * speculatively and the key is actually of a different type. */
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(data,
+ type & ~(PSA_KEY_TYPE_ECC_CURVE_MASK |
+ PSA_KEY_TYPE_CATEGORY_FLAG_PAIR),
+ PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE);
+
if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
PSA_KEY_TYPE_ECC_GET_FAMILY(type) != PSA_ECC_FAMILY_MONTGOMERY) {
/* A Weierstrass public key is represented as:
@@ -160,7 +169,15 @@ psa_status_t mbedtls_psa_ecp_load_representation(
}
/* Load the key material. */
- if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
+ if (MBEDTLS_CT_SPEC_RISKY_BOOL(PSA_KEY_TYPE_IS_PUBLIC_KEY(type))) {
+ /* If this branch is executed speculatively due to misprediction,
+ * and the key type actually indicates a private key,
+ * don't pass the key to mbedtls_ecp_point_read_binary(), whose
+ * timing would reveal information about the key. */
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(data,
+ type & PSA_KEY_TYPE_CATEGORY_MASK,
+ PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY);
+
/* Load the public value. */
status = mbedtls_to_psa_error(
mbedtls_ecp_point_read_binary(&ecp->grp, &ecp->Q,
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_rsa.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_rsa.c
index 27b552fc00a..a65ac6072f3 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_rsa.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_rsa.c
@@ -26,6 +26,7 @@
#include <mbedtls/private/rsa.h>
#include <mbedtls/private/error_common.h>
#include "rsa_internal.h"
+#include "constant_time_internal.h"
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
@@ -64,9 +65,21 @@ psa_status_t mbedtls_psa_rsa_load_representation(
mbedtls_rsa_init(*p_rsa);
/* Parse the data. */
- if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
+ if (MBEDTLS_CT_SPEC_RISKY_BOOL(PSA_KEY_TYPE_IS_KEY_PAIR(type))) {
+ /* Avoid potentially leaking information through the timing of the
+ * parsing of the key material if this function was called
+ * speculatively and the key is actually of a different type. */
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(data,
+ type,
+ PSA_KEY_TYPE_RSA_KEY_PAIR);
status = mbedtls_to_psa_error(mbedtls_rsa_parse_key(*p_rsa, data, data_length));
} else {
+ /* Avoid potentially leaking information through the timing of the
+ * parsing of the key material if this function was called
+ * speculatively and the key is actually of a different type. */
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(data,
+ type,
+ PSA_KEY_TYPE_RSA_PUBLIC_KEY);
status = mbedtls_to_psa_error(mbedtls_rsa_parse_pubkey(*p_rsa, data, data_length));
}
if (status != PSA_SUCCESS) {
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ripemd160.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ripemd160.c
index b63e0859a51..47130e5bcfc 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ripemd160.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/ripemd160.c
@@ -279,6 +279,10 @@ int mbedtls_ripemd160_update(mbedtls_ripemd160_context *ctx,
ctx->total[1]++;
}
+#if SIZE_MAX > 0xFFFFFFFF
+ ctx->total[1] += (uint32_t) (ilen >> 32);
+#endif
+
if (left && ilen >= fill) {
memcpy((void *) (ctx->buffer + left), input, fill);
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/sha1.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/sha1.c
index 643206af7f1..162e1d6ccfc 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/sha1.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/sha1.c
@@ -252,6 +252,10 @@ int mbedtls_sha1_update(mbedtls_sha1_context *ctx,
ctx->total[1]++;
}
+#if SIZE_MAX > 0xFFFFFFFF
+ ctx->total[1] += (uint32_t) (ilen >> 32);
+#endif
+
if (left && ilen >= fill) {
memcpy((void *) (ctx->buffer + left), input, fill);
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/sha256.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/sha256.c
index 3eb38dd1a88..7d36d4c95ea 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/sha256.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/sha256.c
@@ -642,6 +642,10 @@ int mbedtls_sha256_update(mbedtls_sha256_context *ctx,
ctx->total[1]++;
}
+#if SIZE_MAX > 0xFFFFFFFF
+ ctx->total[1] += (uint32_t) (ilen >> 32);
+#endif
+
if (left && ilen >= fill) {
memcpy((void *) (ctx->buffer + left), input, fill);
diff --git a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/sha3.c b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/sha3.c
index 2bac57eaee0..d9eb7b22e52 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/sha3.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/src/sha3.c
@@ -73,7 +73,7 @@
* YMMV.
*/
/* Helper macro to set the values of the higher bits in unused low positions */
-#define H(b63, b31, b15) (b63 << 6 | b31 << 5 | b15 << 4)
+#define H(b63, b31, b15) (((b63) << 6) | ((b31) << 5) | ((b15) << 4))
static const uint8_t iota_r_packed[24] = {
H(0, 0, 0) | 0x01, H(0, 0, 1) | 0x82, H(1, 0, 1) | 0x8a, H(1, 1, 1) | 0x00,
H(0, 0, 1) | 0x8b, H(0, 1, 0) | 0x01, H(1, 1, 1) | 0x81, H(1, 0, 1) | 0x09,
diff --git a/thirdparty/mbedtls/tf-psa-crypto/extras/pk_internal.h b/thirdparty/mbedtls/tf-psa-crypto/extras/pk_internal.h
index c843c4973b5..aef798dd69c 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/extras/pk_internal.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/extras/pk_internal.h
@@ -98,7 +98,7 @@ static inline mbedtls_ecp_group_id mbedtls_pk_get_ec_group_id(const mbedtls_pk_c
#endif /* PSA_WANT_ECC_MONTGOMERY_255 || PSA_WANT_ECC_MONTGOMERY_448 */
#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
- ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
+ (((id) == MBEDTLS_ECP_DP_CURVE25519) || ((id) == MBEDTLS_ECP_DP_CURVE448))
static inline int mbedtls_pk_is_rfc8410(const mbedtls_pk_context *pk)
{
diff --git a/thirdparty/mbedtls/tf-psa-crypto/extras/pk_wrap.c b/thirdparty/mbedtls/tf-psa-crypto/extras/pk_wrap.c
index 8e4c773877f..233c78bcb0e 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/extras/pk_wrap.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/extras/pk_wrap.c
@@ -330,10 +330,18 @@ static void *eckey_rs_alloc(mbedtls_pk_rs_op_t op_type)
rs_ctx->pub_id = MBEDTLS_SVC_KEY_ID_INIT;
if (op_type == MBEDTLS_PK_RS_OP_VERIFY) {
rs_ctx->op = mbedtls_calloc(1, sizeof(psa_verify_hash_interruptible_operation_t));
+ if (rs_ctx->op == NULL) {
+ mbedtls_free(rs_ctx);
+ return NULL;
+ }
psa_verify_hash_interruptible_operation_t *op = rs_ctx->op;
*op = psa_verify_hash_interruptible_operation_init();
} else {
rs_ctx->op = mbedtls_calloc(1, sizeof(psa_sign_hash_interruptible_operation_t));
+ if (rs_ctx->op == NULL) {
+ mbedtls_free(rs_ctx);
+ return NULL;
+ }
psa_sign_hash_interruptible_operation_t *op = rs_ctx->op;
*op = psa_sign_hash_interruptible_operation_init();
}
diff --git a/thirdparty/mbedtls/tf-psa-crypto/extras/pkparse.c b/thirdparty/mbedtls/tf-psa-crypto/extras/pkparse.c
index ae3ef583644..1ccf7ff369c 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/extras/pkparse.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/extras/pkparse.c
@@ -796,7 +796,8 @@ MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_unencrypted_der(
#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
if (pk_alg == MBEDTLS_PK_RSA) {
- if ((ret = mbedtls_pk_rsa_set_key(pk, p, len)) != 0) {
+ if (((ret = mbedtls_pk_rsa_set_key(pk, p, len)) != 0) ||
+ ((ret = mbedtls_pk_set_pubkey_from_prv(pk)) != 0)) {
mbedtls_pk_free(pk);
return ret;
}
diff --git a/thirdparty/mbedtls/tf-psa-crypto/include/mbedtls/constant_time.h b/thirdparty/mbedtls/tf-psa-crypto/include/mbedtls/constant_time.h
index d31bff677e9..6ebcfbeb0b4 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/include/mbedtls/constant_time.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/include/mbedtls/constant_time.h
@@ -11,6 +11,10 @@
#include <stddef.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/** Constant-time buffer comparison without branches.
*
* This is equivalent to the standard memcmp function, but is likely to be
@@ -33,4 +37,8 @@ int mbedtls_ct_memcmp(const void *a,
const void *b,
size_t n);
+#ifdef __cplusplus
+}
+#endif
+
#endif /* MBEDTLS_CONSTANT_TIME_H */
diff --git a/thirdparty/mbedtls/tf-psa-crypto/include/mbedtls/pem.h b/thirdparty/mbedtls/tf-psa-crypto/include/mbedtls/pem.h
index 636d33b61b7..43d2652aa2e 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/include/mbedtls/pem.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/include/mbedtls/pem.h
@@ -64,6 +64,34 @@ void mbedtls_pem_init(mbedtls_pem_context *ctx);
* \brief Read a buffer for PEM information and store the resulting
* data into the specified context buffers.
*
+ * \warning PEM decryption is inherently prone to oracle attacks.
+ * If an adversary can submit arbitrary ciphertexts and
+ * can observe whether they are accepted or rejected,
+ * the adversary may be able to decrypt ciphertexts without
+ * knowing the password. Furthermore, this function is
+ * not constant-time, which improves the adversary's
+ * efficiency. (Note also that subsequent processing after
+ * decryption is likely to improve the adversary's efficiency
+ * as well.)
+ * Therefore, PEM decryption should only be used in
+ * environments where the ciphertext comes from a trusted
+ * source (for example, a storage medium that may be stolen
+ * but is not at risk of silent modifications by an attacker).
+ *
+ * \warning PEM encryption uses an obsolete password protection
+ * mechanism that makes brute force attack fast.
+ * Therefore, the password used for encryption should be
+ * a sufficiently long, randomly generated string.
+ *
+ * \note To encrypt private keys, we recommend PKCS8 encryption, which
+ * is supported by TF-PSA-Crypto's PK module, is a
+ * significant improvement on the weaknesses of PEM encryption,
+ * and works equally well with PEM and binary (DER) encoding.
+ *
+ * \deprecated Encrypted PEM (with a non-null password) is deprecated, and
+ * should only be used to access legacy data in environemnts
+ * where it is safe.
+ *
* \param ctx context to use
* \param header header string to seek and expect
* \param footer footer string to seek and expect
@@ -76,10 +104,6 @@ void mbedtls_pem_init(mbedtls_pem_context *ctx);
* #MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is
* the length to skip.
*
- * \note Attempts to check password correctness by verifying if
- * the decrypted text starts with an ASN.1 sequence of
- * appropriate length
- *
* \note \c mbedtls_pem_free must be called on PEM context before
* the PEM context can be reused in another call to
* \c mbedtls_pem_read_buffer
diff --git a/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_config.h b/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_config.h
index 36d218db172..cdde84071c6 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_config.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_config.h
@@ -1527,12 +1527,23 @@
* Uncomment this macro to use precomputed AES tables stored in ROM.
* Comment this macro to generate AES tables in RAM at runtime.
*
- * Tradeoff: Using precomputed ROM tables reduces RAM usage by ~8kb
- * (or ~2kb if \c MBEDTLS_AES_FEWER_TABLES is used) and reduces the
- * initialization time before the first AES operation can be performed.
- * It comes at the cost of additional ~8kb ROM use (resp. ~2kb if \c
- * MBEDTLS_AES_FEWER_TABLES below is used), and potentially degraded
- * performance if ROM access is slower than RAM access.
+ * Size tradeoff: Using precomputed ROM tables reduces RAM usage by ~8kb
+ * (or ~2kb if \c MBEDTLS_AES_FEWER_TABLES is used). It comes at the cost
+ * of additional ~8kb ROM use (resp. ~2kb if \c MBEDTLS_AES_FEWER_TABLES
+ * below is used).
+ *
+ * Performance tradeoff: Using precomputed ROM tables reduces the
+ * initialization time before the first AES operation can be performed,
+ * but has potentially degraded performance if ROM access is slower than
+ * RAM access.
+ *
+ * \warning Security tradeoff: note that the timing of table lookups can
+ * leak key material. On many microcontrollers, code is executed
+ * from flash memory and the timing of flash reads is more strongly
+ * correlated to the sequence of indices than RAM reads. As a
+ * consequence, when this option is enabled on a device that
+ * executes from flash, an external observer may be able to
+ * reconstruct long-term keys from timing measurements.
*
* This option is independent of \c MBEDTLS_AES_FEWER_TABLES.
*/
@@ -2010,39 +2021,17 @@
//#define MBEDTLS_RSA_GEN_KEY_MIN_BITS 1024 /**< Minimum RSA key size that can be generated in bits (Minimum possible value is 128 bits) */
/**
- * \def TF_PSA_CRYPTO_PQCP_MLDSA_ENABLED
+ * \def MBEDTLS_PK_DECRYPT_MAX_ITERATIONS
*
- * Enable mldsa-native from the PQCP (post-quantum code package) driver.
- * This is an integration of https://github.com/pq-code-package/mldsa-native
- * in TF-PSA-Crypto.
+ * Maximum number of iterations for the PBKDF2 algorithm used when decrypting
+ * PKCS#8 private keys.
*
- * \warning This option is experimental. It may change or be removed without
- * notice.
- *
- * Module: drivers/pqcp/src/wrap_mldsa_native.c
- *
- * Uncomment to include mldsa-native in libtfpsacrypto.
- */
-//#define TF_PSA_CRYPTO_PQCP_MLDSA_ENABLED
-
-/**
- * \def TF_PSA_CRYPTO_PQCP_MLDSA_87_ENABLED
- *
- * Enable mldsa-native from the PQCP (post-quantum code package) driver
- * for the security level 87.
- * This is an integration of https://github.com/pq-code-package/mldsa-native
- * in TF-PSA-Crypto.
- *
- * \warning This option is experimental. It may change or be removed without
- * notice.
- *
- * Requires: TF_PSA_CRYPTO_PQCP_MLDSA_ENABLED
- *
- * Module: drivers/pqcp/src/wrap_mldsa_native.c
- *
- * Uncomment to include MLDSA-87 from mldsa-native in libtfpsacrypto.
+ * As suggested in NIST SP 800-132 this value should be a compromise between
+ * security (the higher iterations the lower the probabilty an attacker can
+ * guess the key from the password) and computation time (the higher the
+ * iterations the longer it will take to compute the key).
*/
-//#define TF_PSA_CRYPTO_PQCP_MLDSA_87_ENABLED
+#define MBEDTLS_PK_DECRYPT_MAX_ITERATIONS 10000000
/** \} name SECTION: Builtin drivers */
diff --git a/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_extra.h b/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_extra.h
index cc887b02107..3b14d80d775 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_extra.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_extra.h
@@ -1042,12 +1042,12 @@ typedef uint32_t psa_pake_primitive_t;
* return 0.
*/
#define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \
- (PSA_ALG_IS_JPAKE(alg) && \
- primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
- PSA_ECC_FAMILY_SECP_R1, 256) ? \
+ (PSA_ALG_IS_JPAKE(alg) && \
+ (primitive) == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
+ PSA_ECC_FAMILY_SECP_R1, 256) ? \
( \
- output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
- output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
+ (output_step) == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
+ (output_step) == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
32 \
) : \
0)
@@ -1072,12 +1072,12 @@ typedef uint32_t psa_pake_primitive_t;
* the parameters are incompatible, return 0.
*/
#define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \
- (PSA_ALG_IS_JPAKE(alg) && \
- primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
- PSA_ECC_FAMILY_SECP_R1, 256) ? \
+ (PSA_ALG_IS_JPAKE(alg) && \
+ (primitive) == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
+ PSA_ECC_FAMILY_SECP_R1, 256) ? \
( \
- input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
- input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
+ (input_step) == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
+ (input_step) == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
32 \
) : \
0)
diff --git a/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_struct.h b/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_struct.h
index 063808743e8..beeb27d74be 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_struct.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_struct.h
@@ -157,14 +157,6 @@ static inline struct psa_cipher_operation_s psa_cipher_operation_init(void)
* algorithms. */
#include "psa/crypto_driver_contexts_composites.h"
-/* Include context definitions from the PQCP driver.
- * (There are none yet at the time of writing, that will come with multipart
- * ML-DSA sign/verify).
- */
-#if defined(TF_PSA_CRYPTO_PQCP_MLDSA_ENABLED)
-#include "tf-psa-crypto/private/crypto_struct_pqcp.h"
-#endif
-
struct psa_mac_operation_s {
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
mbedtls_psa_client_handle_t handle;
diff --git a/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_values.h b/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_values.h
index d30b8d2f496..a2f4da44648 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_values.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/include/psa/crypto_values.h
@@ -583,7 +583,7 @@
0))
/** Check if the curve of given family is Weierstrass elliptic curve. */
-#define PSA_ECC_FAMILY_IS_WEIERSTRASS(family) ((family & 0xc0) == 0)
+#define PSA_ECC_FAMILY_IS_WEIERSTRASS(family) (((family) & 0xc0) == 0)
/** SEC Koblitz curves over prime fields.
*
diff --git a/thirdparty/mbedtls/tf-psa-crypto/include/tf-psa-crypto/build_info.h b/thirdparty/mbedtls/tf-psa-crypto/include/tf-psa-crypto/build_info.h
index 4dcb3d8d87a..9a83150bf2f 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/include/tf-psa-crypto/build_info.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/include/tf-psa-crypto/build_info.h
@@ -26,16 +26,16 @@
*/
#define TF_PSA_CRYPTO_VERSION_MAJOR 1
#define TF_PSA_CRYPTO_VERSION_MINOR 1
-#define TF_PSA_CRYPTO_VERSION_PATCH 1
+#define TF_PSA_CRYPTO_VERSION_PATCH 2
/**
* The single version number has the following structure:
* MMNNPP00
* Major version | Minor version | Patch version
*/
-#define TF_PSA_CRYPTO_VERSION_NUMBER 0x01010100
-#define TF_PSA_CRYPTO_VERSION_STRING "1.1.1"
-#define TF_PSA_CRYPTO_VERSION_STRING_FULL "TF-PSA-Crypto 1.1.1"
+#define TF_PSA_CRYPTO_VERSION_NUMBER 0x01010200
+#define TF_PSA_CRYPTO_VERSION_STRING "1.1.2"
+#define TF_PSA_CRYPTO_VERSION_STRING_FULL "TF-PSA-Crypto 1.1.2"
/* Macros for build-time platform detection */
diff --git a/thirdparty/mbedtls/tf-psa-crypto/platform/platform_util.c b/thirdparty/mbedtls/tf-psa-crypto/platform/platform_util.c
index 3dd58f3aa50..37c653673e6 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/platform/platform_util.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/platform/platform_util.c
@@ -291,10 +291,16 @@ int mbedtls_platform_get_entropy(psa_driver_get_entropy_flags_t flags,
/*
* Test for Linux getrandom() support.
- * Since there is no wrapper in the libc yet, use the generic syscall wrapper
- * available in GNU libc and compatible libc's (eg uClibc).
+ * Use the generic syscall wrapper when sys/syscall.h is available.
*/
-#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
+#if defined(__linux__) && defined(__has_include)
+#if __has_include(<sys/syscall.h>)
+#define MBEDTLS_PLATFORM_HAS_SYS_SYSCALL_H
+#endif
+#endif
+
+#if defined(MBEDTLS_PLATFORM_HAS_SYS_SYSCALL_H) || \
+ (defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__)
#include <unistd.h>
#include <sys/syscall.h>
#if defined(SYS_getrandom)
@@ -312,7 +318,9 @@ static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
return (int) syscall(SYS_getrandom, buf, buflen, flags);
}
#endif /* SYS_getrandom */
-#endif /* __linux__ || __midipix__ */
+#endif /* sys/syscall.h or a known generic syscall wrapper */
+
+#undef MBEDTLS_PLATFORM_HAS_SYS_SYSCALL_H
#if defined(__FreeBSD__) || defined(__DragonFly__)
#include <sys/param.h>
diff --git a/thirdparty/mbedtls/tf-psa-crypto/utilities/asn1write.c b/thirdparty/mbedtls/tf-psa-crypto/utilities/asn1write.c
index 494fe6f940b..b296fea1692 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/utilities/asn1write.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/utilities/asn1write.c
@@ -403,7 +403,6 @@ int mbedtls_asn1_write_integer(unsigned char **p,
size_t integer_length)
{
- int asn1_frame_size = 0;
unsigned int number_of_leading_zeros = 0;
size_t output_buffer_size = (*p-start);
const unsigned char *integer_start = NULL;
@@ -454,12 +453,6 @@ int mbedtls_asn1_write_integer(unsigned char **p,
}
}
- asn1_frame_size =
- mbedtls_asn1_write_len_and_tag(p, start, integer_length, MBEDTLS_ASN1_INTEGER);
- if (asn1_frame_size < 0) {
- return asn1_frame_size;//TC4 mbedtls_asn1_write_len_and_tag failed.
- }
-
- return asn1_frame_size;
+ return mbedtls_asn1_write_len_and_tag(p, start, integer_length, MBEDTLS_ASN1_INTEGER);
}
#endif /* MBEDTLS_ASN1_WRITE_C */
diff --git a/thirdparty/mbedtls/tf-psa-crypto/utilities/constant_time_impl.h b/thirdparty/mbedtls/tf-psa-crypto/utilities/constant_time_impl.h
index 334764ebc11..bdcce19c684 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/utilities/constant_time_impl.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/utilities/constant_time_impl.h
@@ -511,4 +511,14 @@ static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t
#pragma GCC diagnostic pop
#endif
+#if defined(MBEDTLS_SPECTRE_V1_COUNTERMEASURES)
+static inline uintptr_t mbedtls_ct_nullify_if_speculating(uintptr_t value,
+ mbedtls_ct_uint_t cond)
+{
+ MBEDTLS_STATIC_ASSERT(sizeof(uintptr_t) <= sizeof(mbedtls_ct_condition_t),
+ "mbedtls_ct_condition_t smaller than uintptr_t is not supported");
+ return value & mbedtls_ct_bool(cond);
+}
+#endif /* MBEDTLS_SPECTRE_V1_COUNTERMEASURES */
+
#endif /* TF_PSA_CRYPTO_CONSTANT_TIME_IMPL_H */
diff --git a/thirdparty/mbedtls/tf-psa-crypto/utilities/constant_time_internal.h b/thirdparty/mbedtls/tf-psa-crypto/utilities/constant_time_internal.h
index d6dcec4fae7..a3eb66bdcde 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/utilities/constant_time_internal.h
+++ b/thirdparty/mbedtls/tf-psa-crypto/utilities/constant_time_internal.h
@@ -534,6 +534,139 @@ int mbedtls_ct_memcmp_partial(const void *a,
#endif
+/* The code below provides countermeasures for Spectre v1 countermeasures,
+ * i.e. defenses against timing-based side channels in speculatively
+ * executed code. It is currently possible to disable these countermeasures
+ * by configuring the library with -DMBEDTLS_SPECTRE_V1_COUNTERMEASURES=0,
+ * however this is not officially supported and may be removed without
+ * notice.
+ * */
+#if defined(MBEDTLS_SPECTRE_V1_COUNTERMEASURES)
+# if !(MBEDTLS_SPECTRE_V1_COUNTERMEASURES)
+# undef MBEDTLS_SPECTRE_V1_COUNTERMEASURES
+# endif
+#else
+# define MBEDTLS_SPECTRE_V1_COUNTERMEASURES
+#endif
+
+#if defined(MBEDTLS_SPECTRE_V1_COUNTERMEASURES)
+
+/** Use this macro on the condition of if statements where wrongly executing
+ * one of the branches speculatively may leak sensitive data.
+ *
+ * \note This macro alone has no useful effect. It is a
+ * complement for e.g. #MBEDTLS_CT_NULLIFY_IF_SPECULATING.
+ *
+ * This is part of a countermeasure against Spectre v1 attacks.
+ *
+ * \param value An integral-valued expression.
+ * It will be executed exactly once.
+ * \return The value of \p value.
+ */
+#define MBEDTLS_CT_SPEC_RISKY_BOOL(value) \
+ mbedtls_ct_compiler_opaque(value)
+
+static inline uintptr_t mbedtls_ct_nullify_if_speculating(uintptr_t value,
+ mbedtls_ct_uint_t cond);
+
+/** Mask under speculative execution.
+ *
+ * Use this macro in a branch whose condition is \p cond.
+ * If the branch is being executed speculatively, set \p ptr to \c NULL.
+ * (Note: this sets the pointer variable. The pointer is not dereferenced.)
+ * If the branch is being executed normally, leave \p ptr unchanged.
+ *
+ * On processors that do not perform speculative execution, this is always
+ * a no-op (except for possible side effects of the given expressions).
+ *
+ * The behavior is unspecified when not in a branch whose condition is \p cond.
+ *
+ * This macro is intended to prevent potential Spectre v1 (Spectre-PHT)
+ * side channels, where the speculative execution of a branch can leak
+ * sensitive data through the address of a memory access.
+ *
+ * \note Compilers may optimize \p cond in a way that makes
+ * this countermeasure ineffective on its own. To prevent
+ * this, use #MBEDTLS_CT_SPEC_RISKY_BOOL on the if condition.
+ *
+ * Usage example:
+ * ```
+ * if (MBEDTLS_CT_SPEC_RISKY_BOOL(key.is_public)) {
+ * MBEDTLS_CT_NULLIFY_IF_SPECULATING(key_buffer, key.is_public);
+ * parse_public_key_in_variable_time(key_buffer, key_buffer_length);
+ * } else {
+ * parse_private_key_in_constant_time(key_buffer, key_buffer_length);
+ * }
+ * ```
+ *
+ * \param ptr A pointer lvalue.
+ * It may be evaluated zero, one or multiple times, hence
+ * this expression should not have any side effects.
+ * \param cond An expression with integral value.
+ * It may be evaluated zero, one or multiple times, hence
+ * this expression should not have any side effects.
+ * This expression should be compiled to code that does
+ * not use branches or other conditional instructions,
+ * otherwise the calculation of \p cond here may
+ * reintroduce a Spectre v1 gadget. To avoid this,
+ * use another macro such as
+ * #MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ or
+ * #MBEDTLS_CT_NULLIFY_IF_SPECULATING_NE if applicable.
+ */
+#define MBEDTLS_CT_NULLIFY_IF_SPECULATING(ptr, cond) \
+ ((ptr) = (void *) mbedtls_ct_nullify_if_speculating((uintptr_t) (ptr), cond))
+
+#else /* MBEDTLS_SPECTRE_V1_COUNTERMEASURES */
+
+#define MBEDTLS_CT_SPEC_RISKY_BOOL(value) (value)
+
+#define MBEDTLS_CT_NULLIFY_IF_SPECULATING(ptr, cond) \
+ (void) (ptr)
+
+#endif /* MBEDTLS_SPECTRE_V1_COUNTERMEASURES */
+
+/** Mask under speculative execution of an equality condition.
+ *
+ * This is functionally equivalent to
+ * #MBEDTLS_CT_NULLIFY_IF_SPECULATING(\p ptr, (\p lhs) == (\p rhs))
+ * but it protects against the equality being compiled to code using
+ * a conditional instruction that may introduce a further Spectre v1
+ * gadget.
+ *
+ * \param ptr A pointer lvalue.
+ * It may be evaluated zero, one or multiple times, hence
+ * this expression should not have any side effects.
+ * \param lhs An expression with an integral value.
+ * It may be evaluated zero, one or multiple times, hence
+ * this expression should not have any side effects.
+ * \param rhs An expression with an integral value.
+ * It may be evaluated zero, one or multiple times, hence
+ * this expression should not have any side effects.
+ */
+#define MBEDTLS_CT_NULLIFY_IF_SPECULATING_EQ(ptr, lhs, rhs) \
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING(ptr, mbedtls_ct_uint_eq(lhs, rhs))
+
+/** Mask under speculative execution of a negated equality condition.
+ *
+ * This is functionally equivalent to
+ * #MBEDTLS_CT_NULLIFY_IF_SPECULATING(\p ptr, (\p lhs) != (\p rhs))
+ * but it protects against the equality being compiled to code using
+ * a conditional instruction that may introduce a further Spectre v1
+ * gadget.
+ *
+ * \param ptr A pointer lvalue.
+ * It may be evaluated zero, one or multiple times, hence
+ * this expression should not have any side effects.
+ * \param lhs An expression with an integral value.
+ * It may be evaluated zero, one or multiple times, hence
+ * this expression should not have any side effects.
+ * \param rhs An expression with an integral value.
+ * It may be evaluated zero, one or multiple times, hence
+ * this expression should not have any side effects.
+ */
+#define MBEDTLS_CT_NULLIFY_IF_SPECULATING_NE(ptr, lhs, rhs) \
+ MBEDTLS_CT_NULLIFY_IF_SPECULATING(ptr, mbedtls_ct_uint_ne(lhs, rhs))
+
/* Include the implementation of static inline functions above. */
#include "constant_time_impl.h"
diff --git a/thirdparty/mbedtls/tf-psa-crypto/utilities/pkcs5.c b/thirdparty/mbedtls/tf-psa-crypto/utilities/pkcs5.c
index e2ca86367e2..a4404df642b 100644
--- a/thirdparty/mbedtls/tf-psa-crypto/utilities/pkcs5.c
+++ b/thirdparty/mbedtls/tf-psa-crypto/utilities/pkcs5.c
@@ -158,6 +158,10 @@ int mbedtls_pkcs5_pbes2_ext(const mbedtls_asn1_buf *pbe_params, int mode,
return ret;
}
+ if ((size_t) iterations > MBEDTLS_PK_DECRYPT_MAX_ITERATIONS) {
+ return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
+ }
+
if ((ret = mbedtls_asn1_get_alg(&p, end, &enc_scheme_oid,
&enc_scheme_params)) != 0) {
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);