This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit d9b5b94609219ca39d469ad14b39226cb7188e0d Author: makejian <[email protected]> AuthorDate: Tue Nov 24 17:23:43 2026 +0800 crypto/openssl_mbedtls_wrapper: fix build and Mbed TLS 3.6 compatibility The wrapper no longer compiled against Mbed TLS 3.x, which moved mbedtls_cipher_context_t.cipher_info, mbedtls_md_context_t.md_info, mbedtls_ssl_context.state and the ecp_keypair members behind MBEDTLS_PRIVATE(). Use the public accessors (mbedtls_cipher_get_key_bitlen(), mbedtls_cipher_get_iv_size(), mbedtls_md_info_from_ctx()) and the MBEDTLS_PRIVATE macro instead of direct member access. Also fix: - SSL_SESSION.references must be atomic_t for the atomic_add() and atomic_sub() calls in ssl_lib.c - include <nuttx/config.h> from ssl_dbg.h so the CONFIG_OPENSSL_* assert macros are always defined - mbedtls_x509_crt_parse_file() and mbedtls_x509_crt_parse_path() return the number of skipped (bad) certificates on partial success; only a negative return is an error - EVP_CipherInit_ex() configured the PKCS7 padding mode before mbedtls_cipher_setup(), which cleared it again, so padding was never applied - pin the min and max version to TLS 1.2: the wrapper only implements TLS 1.2 cipher handling, and Mbed TLS 3.6 removed MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET and renamed MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE to MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE Signed-off-by: makejian <[email protected]> --- .../include/openssl/ssl_dbg.h | 6 +++ .../include/openssl/ssl_local.h | 4 +- crypto/openssl_mbedtls_wrapper/mbedtls/aes.c | 3 +- crypto/openssl_mbedtls_wrapper/mbedtls/cipher.c | 9 ++-- crypto/openssl_mbedtls_wrapper/mbedtls/digest.c | 3 +- crypto/openssl_mbedtls_wrapper/mbedtls/hmac.c | 2 +- crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c | 54 +++++----------------- 7 files changed, 31 insertions(+), 50 deletions(-) diff --git a/crypto/openssl_mbedtls_wrapper/include/openssl/ssl_dbg.h b/crypto/openssl_mbedtls_wrapper/include/openssl/ssl_dbg.h index 84fc7ac62..e277f01b3 100644 --- a/crypto/openssl_mbedtls_wrapper/include/openssl/ssl_dbg.h +++ b/crypto/openssl_mbedtls_wrapper/include/openssl/ssl_dbg.h @@ -20,6 +20,12 @@ #ifndef OPENSSL_MBEDTLS_WRAPPER_SSL_DBG_H #define OPENSSL_MBEDTLS_WRAPPER_SSL_DBG_H +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + #ifdef __cplusplus extern "C" { diff --git a/crypto/openssl_mbedtls_wrapper/include/openssl/ssl_local.h b/crypto/openssl_mbedtls_wrapper/include/openssl/ssl_local.h index 053da27de..5fa7625b5 100644 --- a/crypto/openssl_mbedtls_wrapper/include/openssl/ssl_local.h +++ b/crypto/openssl_mbedtls_wrapper/include/openssl/ssl_local.h @@ -24,6 +24,8 @@ * Included Files ****************************************************************************/ +#include <nuttx/atomic.h> + #include <openssl/ssl.h> #include <openssl/statem.h> #include <openssl/x509.h> @@ -111,7 +113,7 @@ struct ssl_session_st long time; X509 *peer; const SSL_CIPHER *cipher; - int references; + atomic_t references; struct { char hostname[TLSEXT_MAXLEN_host_name]; diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/aes.c b/crypto/openssl_mbedtls_wrapper/mbedtls/aes.c index 988fec80a..53ff0e259 100644 --- a/crypto/openssl_mbedtls_wrapper/mbedtls/aes.c +++ b/crypto/openssl_mbedtls_wrapper/mbedtls/aes.c @@ -133,7 +133,6 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, const unsigned char *iv, int enc) { mbedtls_cipher_context_t *ctx_ = (mbedtls_cipher_context_t *)ctx; - mbedtls_cipher_set_padding_mode(ctx_, MBEDTLS_PADDING_PKCS7); const mbedtls_operation_t operation = enc ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT; int ret = mbedtls_cipher_setup(ctx_, @@ -158,6 +157,8 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, goto error; } + mbedtls_cipher_set_padding_mode(ctx_, MBEDTLS_PADDING_PKCS7); + return 1; error: errno = ret; diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/cipher.c b/crypto/openssl_mbedtls_wrapper/mbedtls/cipher.c index 3cbb25cc6..44e376d91 100644 --- a/crypto/openssl_mbedtls_wrapper/mbedtls/cipher.c +++ b/crypto/openssl_mbedtls_wrapper/mbedtls/cipher.c @@ -41,9 +41,9 @@ int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER * cipher, mbedtls_cipher_context_t *m_ctx = (mbedtls_cipher_context_t *)ctx; mbedtls_cipher_init(m_ctx); mbedtls_cipher_setup(m_ctx, (const mbedtls_cipher_info_t *)cipher); - mbedtls_cipher_setkey(m_ctx, key, m_ctx->cipher_info->key_bitlen, + mbedtls_cipher_setkey(m_ctx, key, mbedtls_cipher_get_key_bitlen(m_ctx), MBEDTLS_DECRYPT); - mbedtls_cipher_set_iv(m_ctx, iv, m_ctx->cipher_info->iv_size); + mbedtls_cipher_set_iv(m_ctx, iv, mbedtls_cipher_get_iv_size(m_ctx)); return 1; } @@ -99,14 +99,15 @@ int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, } if (mbedtls_cipher_setkey(m_ctx, key, - m_ctx->cipher_info->key_bitlen, + mbedtls_cipher_get_key_bitlen(m_ctx), MBEDTLS_ENCRYPT) != 0) { mbedtls_cipher_free(m_ctx); return 0; } - if (mbedtls_cipher_set_iv(m_ctx, iv, m_ctx->cipher_info->iv_size) != 0) + if (mbedtls_cipher_set_iv(m_ctx, iv, + mbedtls_cipher_get_iv_size(m_ctx)) != 0) { mbedtls_cipher_free(m_ctx); return 0; diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/digest.c b/crypto/openssl_mbedtls_wrapper/mbedtls/digest.c index 41412d396..5dbf6d3f6 100644 --- a/crypto/openssl_mbedtls_wrapper/mbedtls/digest.c +++ b/crypto/openssl_mbedtls_wrapper/mbedtls/digest.c @@ -84,7 +84,8 @@ int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) { - const mbedtls_md_info_t *md_info = ((mbedtls_md_context_t *)ctx)->md_info; + const mbedtls_md_info_t *md_info = + mbedtls_md_info_from_ctx((mbedtls_md_context_t *)ctx); if (md_info == NULL) { return 0; diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/hmac.c b/crypto/openssl_mbedtls_wrapper/mbedtls/hmac.c index 2e21de016..325018e97 100644 --- a/crypto/openssl_mbedtls_wrapper/mbedtls/hmac.c +++ b/crypto/openssl_mbedtls_wrapper/mbedtls/hmac.c @@ -106,7 +106,7 @@ int HMAC_Final(HMAC_CTX *ctx, uint8_t *out, unsigned int *out_len) size_t md_size; const mbedtls_md_info_t *md_info = - ((mbedtls_md_context_t *)(ctx))->md_info; + mbedtls_md_info_from_ctx((mbedtls_md_context_t *)ctx); if (md_info == NULL) { return 0; diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c index 4924969c9..55e5d384e 100644 --- a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c +++ b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c @@ -121,7 +121,6 @@ int ssl_pm_new(SSL *ssl) size_t pers_len = sizeof(pers); int endpoint; - int version; const SSL_METHOD *method = ssl->method; @@ -178,39 +177,13 @@ int ssl_pm_new(SSL *ssl) goto mbedtls_err2; } - if (TLS_ANY_VERSION != ssl->version) - { - if (TLS1_2_VERSION == ssl->version) - { - version = MBEDTLS_SSL_MINOR_VERSION_3; - } - else if (TLS1_1_VERSION == ssl->version) - { - version = MBEDTLS_SSL_MINOR_VERSION_2; - } - else if (TLS1_VERSION == ssl->version) - { - version = MBEDTLS_SSL_MINOR_VERSION_1; - } - else - { - version = MBEDTLS_SSL_MINOR_VERSION_0; - } + mbedtls_ssl_conf_min_version(&ssl_pm->conf, + MBEDTLS_SSL_MAJOR_VERSION_3, + MBEDTLS_SSL_MINOR_VERSION_3); - mbedtls_ssl_conf_max_version(&ssl_pm->conf, - MBEDTLS_SSL_MAJOR_VERSION_3, version); - mbedtls_ssl_conf_min_version(&ssl_pm->conf, - MBEDTLS_SSL_MAJOR_VERSION_3, version); - } - else - { - mbedtls_ssl_conf_max_version(&ssl_pm->conf, - MBEDTLS_SSL_MAJOR_VERSION_3, - MBEDTLS_SSL_MINOR_VERSION_3); - mbedtls_ssl_conf_min_version(&ssl_pm->conf, - MBEDTLS_SSL_MAJOR_VERSION_3, - MBEDTLS_SSL_MINOR_VERSION_0); - } + mbedtls_ssl_conf_max_version(&ssl_pm->conf, + MBEDTLS_SSL_MAJOR_VERSION_3, + MBEDTLS_SSL_MINOR_VERSION_3); mbedtls_ssl_conf_rng(&ssl_pm->conf, mbedtls_ctr_drbg_random, &ssl_pm->ctr_drbg); @@ -321,7 +294,7 @@ static int mbedtls_handshake(mbedtls_ssl_context *ssl) { int ret = 0; - while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) + while (ssl->MBEDTLS_PRIVATE(state) != MBEDTLS_SSL_HANDSHAKE_OVER) { ret = mbedtls_ssl_handshake_step(ssl); @@ -350,7 +323,7 @@ int ssl_pm_handshake(SSL *ssl) return 0; } - if (ssl_pm->ssl.state != MBEDTLS_SSL_HANDSHAKE_OVER) + if (ssl_pm->ssl.MBEDTLS_PRIVATE(state) != MBEDTLS_SSL_HANDSHAKE_OVER) { ssl_speed_up_enter(); @@ -486,7 +459,7 @@ int ssl_pm_read(SSL *ssl, void *buffer, int len) SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_read() return -0x%x", -ret); if (ret == MBEDTLS_ERR_NET_CONN_RESET || - ret <= MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE) /* fatal errors */ + ret <= MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE) /* fatal errors */ { ssl->err = SSL_ERROR_SYSCALL; } @@ -594,7 +567,7 @@ OSSL_HANDSHAKE_STATE ssl_pm_get_state(const SSL *ssl) struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; - switch (ssl_pm->ssl.state) + switch (ssl_pm->ssl.MBEDTLS_PRIVATE(state)) { case MBEDTLS_SSL_CLIENT_HELLO: state = TLS_ST_CW_CLNT_HELLO; @@ -629,9 +602,6 @@ OSSL_HANDSHAKE_STATE ssl_pm_get_state(const SSL *ssl) case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: state = TLS_ST_SR_KEY_EXCH; break; - case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET: - state = TLS_ST_SW_SESSION_TICKET; - break; case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT: state = TLS_ST_SW_CERT_REQ; break; @@ -776,7 +746,7 @@ int x509_pm_load_file(X509 *x, const char *path) mbedtls_x509_crt_free(&x509_pm->x509_crt); mbedtls_x509_crt_init(&x509_pm->x509_crt); ret = mbedtls_x509_crt_parse_file(&x509_pm->x509_crt, path); - if (ret) + if (ret < 0) { SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_x509_crt_parse_file return -0x%x", -ret); @@ -795,7 +765,7 @@ int x509_pm_load_path(X509 *x, const char *path) mbedtls_x509_crt_free(&x509_pm->x509_crt); mbedtls_x509_crt_init(&x509_pm->x509_crt); ret = mbedtls_x509_crt_parse_path(&x509_pm->x509_crt, path); - if (ret) + if (ret < 0) { SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_x509_crt_parse_file return -0x%x", -ret);
