Skip to content

Conversation

@ThePassionate
Copy link
Contributor

@ThePassionate ThePassionate commented Jan 16, 2026

Summary

This commit introduces significant functional enhancements and code optimizations to the crypto/openssl_mbedtls_wrapper component. The primary goal is to improve the functional completeness, debugging convenience, code robustness, and memory safety of this wrapper layer, providing upper-layer applications with a more stable MbedTLS backend that more closely aligns with the standard OpenSSL API.

Key Changes and Rationale:

  1. New Configuration Options (Kconfig): Introduces four debug behavior configurations (DEBUG, EXIT, DEBUG_EXIT, DEBUG_BLOCK) for the SSL_ASSERT macros. This allows developers to flexibly choose the handling strategy upon assertion failure (print only, return error code only, print then return, print then block) based on debugging or production environment needs, significantly enhancing issue diagnosis capabilities.

  2. Greatly Expanded API Compatibility:

    • Header File Expansion: Added numerous function declarations, type definitions, and macros in headers such as bio.h, err.h, evp.h, ssl.h, ssl_local.h, tls1.h, types.h, and x509.h. For example, critical APIs for BIO chaining, SSL session management, TLS protocol version methods, and X509 certificate file loading were added, substantially improving compatibility with existing OpenSSL code.
    • New Implementation Files: Added implementations for key modules like bio_b64.c (Base64 BIO filter), bio_lib.c (BIO core library), and bss_mem.c (memory BIO), completing foundational functionality.
  3. Refactored and Optimized Core Implementation (ssl_pm.c, ssl_lib.c, etc.):

    • Memory Management Optimization: Changed the internal certificate structure (x509_pm.x509_crt) from a dynamically allocated pointer to a stack object. This simplifies lifecycle management and eliminates potential memory leaks and null pointer dereferences.
    • Functional Enhancements: Added x509_pm_load_file and x509_pm_load_path methods to the x509_pm structure, supporting direct certificate loading from files or paths for improved usability.
    • Strengthened Error Handling: Enhanced the translation logic from MbedTLS error codes to OpenSSL error codes (e.g., SSL_ERROR_WANT_READ/WRITE) in functions like ssl_pm_read/write, leading to more precise error handling.
    • Code Cleanup and Standardization: Removed the redundant LOCAL_ATTR macro for consistent coding style. Replaced printf debug outputs with the project-defined SSL_DEBUG macro for more standardized and controllable logging.

Impact

  • Impact on Users (Developers):

    • Positive Impact: Applications using this wrapper can now call more standard OpenSSL APIs (e.g., BIO_f_base64, SSL_CTX_use_certificate_chain_file, ERR_print_errors_cb), improving portability and development experience. The new debug options provide powerful tools for troubleshooting TLS/SSL issues.
    • Potential Impact: Due to the internal structure change of struct x509_pm (x509_crt changed from a pointer to an embedded object), any third-party code or downstream patches directly manipulating this internal structure may require adjustments. This change is necessary for advancing towards a safer, simpler memory model.
  • Impact on Security: Positive impact. The optimized memory management model reduces risks associated with improper memory handling. More complete error handling aids developers in writing more robust secure communication code.

Testing

  • Verification Methods:

    1. Build Testing: Performed full builds on multiple target architectures (e.g., armv7-m, armv8-m, risc-v) using default configurations and configurations with OPENSSL_MBEDTLS_WRAPPER enabled, ensuring no compilation errors or warnings.
    2. Functional Testing:
      • Wrote test programs using the new APIs to verify the read/write functionality of Base64 BIO and memory BIO.
      • Tested new file loading interfaces like SSL_CTX_use_certificate_chain_file.
      • Verified that different SSL_ASSERT configuration modes behave as expected under simulated error conditions (e.g., printing info, exiting, or blocking upon assertion failure).
  • Test Environment (Example, please update based on actual situation):

    • Host: Ubuntu 22.04, GCC 11.4.0
    • Target/Board: sim:nsh (simulator)
    • Configuration: enabling CRYPTO_MBEDTLS and OPENSSL_MBEDTLS_WRAPPER.

please ignore openssl mix case warning:

Error: /home/runner/work/nuttx-apps/nuttx-apps/apps/crypto/openssl_mbedtls_wrapper/include/openssl/bio.h:90:18: error: Mixed case identifier found
Error: /home/runner/work/nuttx-apps/nuttx-apps/apps/crypto/openssl_mbedtls_wrapper/include/openssl/bio.h:92:18: error: Mixed case identifier found
Error: /home/runner/work/nuttx-apps/nuttx-apps/apps/crypto/openssl_mbedtls_wrapper/include/openssl/bio.h:[94](https://github.com/apache/nuttx-apps/actions/runs/21057417883/job/60556297369?pr=3338#step:4:95):5: error: Mixed case identifier found
Error: /home/runner/work/nuttx-apps/nuttx-apps/apps/crypto/openssl_mbedtls_wrapper/include/openssl/bio.h:96:5: error: Mixed case identifier found
Error: /home/runner/work/nuttx-apps/nuttx-apps/apps/crypto/openssl_mbedtls_wrapper/include/openssl/bio.h:98:5: error: Mixed case identifier found
Error: /home/runner/work/nuttx-apps/nuttx-apps/apps/crypto/openssl_mbedtls_wrapper/include/openssl/bio.h:100:4: error: Mixed case identifier found

VELAPLATFO-62586

Change-Id: I5d7675c05dc3a52c1cb15a6132b969a19f848248
Signed-off-by: makejian <makejian@xiaomi.com>
(cherry picked from commit 409c86a062a816b56a3a48b1111102d12f24a48f)
…mqtt

VELAPLATFO-62586

Change-Id: I3b56b028e76aee118ed90211c097ac3fe86bc129
Signed-off-by: makejian <makejian@xiaomi.com>
(cherry picked from commit 7a567e98489f64b740044b9ce4066fa8d41af359)
VELAPLATFO-62586

Change-Id: I2ceac930c87196a16ea7ddf5e4130bb991b51025
Signed-off-by: makejian <makejian@xiaomi.com>
(cherry picked from commit d0547662d5006ec0a348d89d3d7e251ef4bb183c)
…hen ssl error

VELAPLATFO-66562

Change-Id: Ibb1446a7fcae1d2bc09d75052466a6ce084103b8
Signed-off-by: makejian <makejian@xiaomi.com>
@ThePassionate
Copy link
Contributor Author

@xiaoxiang781216 How can I ignore the case-sensitive style check for this commit?

@xiaoxiang781216
Copy link
Contributor

xiaoxiang781216 commented Jan 16, 2026

@xiaoxiang781216 How can I ignore the case-sensitive style check for this commit?

let's ignore the warning directly, but fix the spell warning. The 3rd party case issue doen't need fix.

@xiaoxiang781216
Copy link
Contributor

@ThePassionate please fix check error.

Fix coding style issues in OpenSSL/MbedTLS wrapper implementation:
- Align whitespace and indentation
- Fix line formatting
- Ensure consistent code style per NuttX standards

Signed-off-by: makejian <makejian@xiaomi.com>
@ThePassionate
Copy link
Contributor Author

@ThePassionate please fix check error.

Thanks, i have fixed check error expect 'error: Mixed case identifier found'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants