Fix load_certificate/load_private_key ignoring explicit format argument#1954
Merged
oroulet merged 1 commit intoFreeOpcUa:masterfrom Apr 5, 2026
Merged
Conversation
When an explicit `extension` parameter is provided (e.g., extension="der"), it should take precedence over the file extension inferred from the path. Previously, a file named "cert.pem" would always be loaded as PEM format, even when extension="der" was explicitly passed. Extract a shared `_is_pem_format()` helper that checks the explicit extension first, falling back to file extension inference only when no explicit format is given. Closes FreeOpcUa#1927 Signed-off-by: Lidang-Jiang <lidangjiang@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
load_certificateandload_private_keyto respect the explicitextensionparameter instead of always inferring from the file extension_is_pem_format()helper that checks the explicit extension first, falling back to file extension inference only when no explicit format is givenChanges
asyncua/crypto/uacrypto.py: Add_is_pem_format()helper, refactor bothload_certificateandload_private_keyto use ittests/test_gen_certificates.py: Add 4 regression tests covering explicit format override and format inferenceRoot Cause
The condition
if ext == ".pem" or extension == "pem"checked the file extension (ext) before the explicitextensionparameter, soext == ".pem"would short-circuit even whenextension="der"was explicitly passed.Fix
When
extensionis explicitly provided (notNone), use it exclusively. Only infer from the file path suffix whenextension is None.Before (bug reproduction)
After (fix verified)
Test output
Full regression (110 passed, 3 skipped):
Test plan
.pemextension loads correctly withextension="der".pemextension loads correctly withextension="der".derextension loads correctly withextension="pem"