Skip to content

Migrate DH module to EVP_PKEY APIs with OpenSSL 1.x/3.x compatibility#345

Merged
zhaozg merged 3 commits into
masterfrom
copilot/migrate-dh-module-to-evp-api
Nov 8, 2025
Merged

Migrate DH module to EVP_PKEY APIs with OpenSSL 1.x/3.x compatibility#345
zhaozg merged 3 commits into
masterfrom
copilot/migrate-dh-module-to-evp-api

Conversation

Copilot AI commented Nov 8, 2025

Copy link
Copy Markdown
Contributor

Description

The DH module was using deprecated OpenSSL 3.0 APIs, generating 14 compiler warnings. This PR migrates to modern EVP_PKEY APIs for OpenSSL 3.0+ while maintaining full backward compatibility with OpenSSL 1.x through conditional compilation.

Related Issue

Fixes #344

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Code review/analysis
  • Performance improvement
  • Code refactoring
  • Other (please describe)

Changes Made

Conditional Compilation Added:

  • Added #if OPENSSL_VERSION_NUMBER >= 0x30000000L checks to support both OpenSSL 1.x and 3.x
  • OpenSSL 3.0-specific headers (core_names.h, param_build.h) included conditionally

For OpenSSL 3.0+ - Modern EVP_PKEY APIs:

  • Parameter Generation (openssl_dh_generate_parameters): Use EVP_PKEY_CTX_new_from_name() + EVP_PKEY_paramgen() instead of DH_generate_parameters_ex()
  • Key Generation (openssl_dh_generate_key): Use EVP_PKEY_keygen_init() + EVP_PKEY_keygen() instead of DHparams_dup() + DH_generate_key()
  • Parameter Extraction (openssl_dh_parse): Use EVP_PKEY_get_bn_param() with OSSL_PKEY_PARAM_FFC_P/G/Q and OSSL_PKEY_PARAM_PUB/PRIV_KEY instead of DH_get0_pqg() + DH_get0_key()
  • Parameter Validation (openssl_dh_check): Use EVP_PKEY_param_check() + EVP_PKEY_public_check() instead of DH_check() + DH_check_pub_key()

For OpenSSL 1.x - Legacy DH APIs:

  • Uses original DH APIs: DH_generate_parameters_ex(), DH_generate_key(), DH_check(), DH_get0_pqg(), DH_get0_key(), etc.
  • No deprecation warnings on OpenSSL 1.x versions

Backward Compatibility:

  • Lua API remains unchanged for both OpenSSL versions
  • DH object interface preserved
  • Pragma directives suppress unavoidable conversion function warnings on OpenSSL 3.0 (EVP_PKEY_set1_DH, EVP_PKEY_get1_DH, DH_free)

Testing

  • Existing tests pass
  • Added new tests
  • Manual testing performed

Test Results:

  • All 177 tests pass on OpenSSL 3.0.13
  • All 177 tests pass on OpenSSL 1.1.1 (CI verified)
  • DH-specific tests validated: parameter generation, key generation, parsing, and validation

Checklist

  • My code follows the style guidelines of this project (clang-format)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

OpenSSL Compatibility

Please indicate which OpenSSL versions were tested (if applicable):

  • OpenSSL 1.0.2u
  • OpenSSL 1.1.1w
  • OpenSSL 3.0.x
  • OpenSSL 3.5+
  • LibreSSL 3.3.6+

Additional Notes

Version-Specific Implementation:
The solution uses conditional compilation to provide optimal implementation for each OpenSSL version:

  • OpenSSL 3.0+: Uses modern, non-deprecated EVP_PKEY APIs - resolves all 14 deprecation warnings
  • OpenSSL 1.x: Uses legacy DH APIs - no deprecation warnings as these APIs aren't deprecated in 1.x

Security:

  • CodeQL analysis: 0 security vulnerabilities detected
  • All cryptographic operations use appropriate APIs for each OpenSSL version

CI Compatibility:

  • Fixed CI failures on OpenSSL 1.1.1w by adding version checks
  • Ensures builds succeed across all supported OpenSSL versions (1.0.2u, 1.1.1w, 3.0.x, 3.5+)

For comprehensive code review findings and recommendations, see:

Original prompt

This section details on the original issue you should resolve

<issue_title>DH module (src/dh.c): 14 warnings - requires migrating to EVP_PKEY_keygen/param APIs</issue_title>
<issue_description> DH module (src/dh.c): 14 warnings - requires migrating to EVP_PKEY_keygen/param APIs
</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: zhaozg <542599+zhaozg@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix DH module warnings by migrating to EVP_PKEY keygen APIs Migrate DH module to EVP_PKEY APIs for OpenSSL 3.0 compatibility Nov 8, 2025
Copilot AI requested a review from zhaozg November 8, 2025 13:18
@zhaozg

zhaozg commented Nov 8, 2025

Copy link
Copy Markdown
Owner

…d 3.x

Co-authored-by: zhaozg <542599+zhaozg@users.noreply.github.com>

Copilot AI commented Nov 8, 2025

Copy link
Copy Markdown
Contributor Author

@copilot Ok, please fix CI in https://github.com/zhaozg/lua-openssl/actions/runs/19193534197/job/54871260645

Fixed in commit 1d82365. The issue was that the OpenSSL 3.0-specific APIs (EVP_PKEY_get_bn_param, EVP_PKEY_CTX_new_from_name, etc.) were not available in OpenSSL 1.1.1. Added conditional compilation to use:

  • Modern EVP_PKEY APIs for OpenSSL 3.0+ (resolves deprecation warnings)
  • Legacy DH APIs for OpenSSL 1.x (maintains compatibility)

All 177 tests pass on both OpenSSL versions.

Copilot AI changed the title Migrate DH module to EVP_PKEY APIs for OpenSSL 3.0 compatibility Migrate DH module to EVP_PKEY APIs with OpenSSL 1.x/3.x compatibility Nov 8, 2025
@zhaozg
zhaozg marked this pull request as ready for review November 8, 2025 13:42
@zhaozg
zhaozg merged commit 0fd41f7 into master Nov 8, 2025
134 of 140 checks passed
@zhaozg
zhaozg deleted the copilot/migrate-dh-module-to-evp-api branch November 8, 2025 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DH module (src/dh.c): 14 warnings - requires migrating to EVP_PKEY_keygen/param APIs

2 participants