Repository navigation
fix(auth): verify public/private key match with retries in mTLS keystore loading - #14829
Open
macastelaz wants to merge 3 commits into
Open
macastelaz wants to merge 3 commits into
macastelaz wants to merge 3 commits into
Conversation
…ore loading Verify that the parsed private key matches the leaf certificate's public key in MtlsKeyStoreUtils.createMtlsKeyStore() via a test signature and verification, and retry up to 3 times (with a 100ms interval) in X509Provider.getKeyStore() when loading separate cert_path and key_path files from disk to handle transient mid-rotation mismatches.
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces key pair verification when creating an mTLS KeyStore to ensure the private key matches the leaf certificate's public key. It also adds retry logic in X509Provider to handle transient mismatches during atomic key rotations. The review feedback recommends separating cryptographic setup/execution failures from actual key verification mismatches to prevent misleading error messages, and advises against adding out-of-scope algorithm normalization (such as converting 'ECDSA' to 'EC') to maintain behavioral alignment.
…ate MtlsKeyStoreUtils javadoc
…terministic tests
This branch has not been deployed
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
Verifies that the parsed
PrivateKeycryptographically matches the leafX509Certificate'sPublicKeyinMtlsKeyStoreUtils.createMtlsKeyStore(), and adds retry handling (3attempts with a100msbackoff, matchingAgentIdentityUtils.loadAndVerifyCredentials()) inX509Provider.getKeyStore()when loading separatecert_pathandkey_pathfiles from disk.Problem
KeyStoreentries:KeyStore.getInstance("JKS").setKeyEntry("alias", key, new char[] {}, chain)does not verify thatkeycorresponds tochain[0].getPublicKey()as long as the key algorithm matches (e.g., both are RSA or both are EC).cert_pathandkey_pathfiles: When an external certificate rotator updatescert_path(cert_A->cert_B) slightly beforekey_path(key_A->key_B), transport channel refresh (CertificateRotationTracker, which trackscert_path's SHA-256 fingerprint) can triggerX509Provider.getKeyStore()during the intermediate(cert_B, key_A)window. Without key-pair verification and retries,getKeyStore()succeeds with a mismatchedKeyStoreand the channel marksFP(cert_B)as refreshed, leaving the mTLS channel with a mismatched certificate and private key until the next rotation.Solution
MtlsKeyStoreUtils.createMtlsKeyStore(): Perform a test signature and verification (SHA256withRSAfor RSA keys,SHA256withECDSAfor EC/ECDSA keys with SunEC"ECDSA"->"EC"public key normalization) to ensure the parsed private key matches the leaf certificate's public key, throwingGeneralSecurityException("Certificate and private key do not match")on mismatch.X509Provider.getKeyStore(): Retry up to3times (100msinterval) when reading and verifying separatecert_pathandkey_pathfiles fromWorkloadCertificateConfiguration, handling transient mid-rotation key mismatches or partial file writes while preserving interrupt restoration onInterruptedException.Testing
MtlsKeyStoreUtilsTestcovering mismatched RSA and EC key pairs.X509ProviderTestcovering:key_pathfinishes rotating to matchcert_pathon a subsequent attempt.IOExceptioncaused byGeneralSecurityException("Certificate and private key do not match")after exhausting all 3 attempts.cert_pathis transiently empty/truncated on the first attempt.IOExceptionif interrupted during retry backoff.