Fix certificate chain ordering for jarsigner compatibility #47977
+277
−2
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.
jarsigner fails to validate JARs signed with non-exportable Azure Key Vault certificates due to certificate chain ordering issues. BouncyCastle's PKCS12 parser returns certificates in unpredictable order, causing "PKIX path building failed" errors.
Changes
CertificateUtil.orderCertificateChain()– Identifies the leaf certificate (one not issuing others) and builds chain leaf→intermediate(s)→root by following issuer relationshipsloadCertificatesFromSecretBundleValue()– Always orders certificates before returning, regardless of PEM or PKCS12 formatImplementation
The ordering algorithm:
Handles edge cases gracefully (null, empty, single cert) and maintains backward compatibility.
Original prompt
This section details on the original issue you should resolve
<issue_title>[BUG] jarsigner + jca still reports that entries in certificate chain are invalid</issue_title>
<issue_description>Describe the bug
When using a valid, DigiCert issued, non-exportable Azure Key Vault certificate to sign a .jar file with jarsigner + jca 2.10.0, jarsigner will produce a warning.
Issue seems to persist after bugfix, referencing #41832
Exception or Stack Trace
Warning: This jar contains entries whose certificate chain is invalid. Reason: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targetTo Reproduce
Steps to reproduce the behavior are pretty much identical to #44085:
Create new version of a non-exportable code signing certificate from Azure Key Vault (RSA-HSM, 4096)
Sign the CSR through DigiCert
Merge the signing request with Azure Key Vault
Configure the app registration with secret, along with RBAC on Azure Key Vault for access
Open Windows Terminal and run the Code Snippet below (With environment variables declared, disregard line feeds):
It will produce the following output: