Skip to content

Accept bundles containing cross-signed certificates in ParseSingleCertificateChain - #9150

Open
wallrj wants to merge 4 commits into
cert-manager:masterfrom
wallrj:parse-cross-signed-chain
Open

wallrj wants to merge 4 commits into
cert-manager:masterfrom
wallrj:parse-cross-signed-chain

Conversation

@wallrj

@wallrj wallrj commented Aug 15, 2026 •

Copy link
Copy Markdown
Member

Pull Request Motivation

Cross-signing an intermediate or root CA — signing the same subject and public key with two different issuers — is the recommended way to rotate a root CA, for example in Vault PKI. ParseSingleCertificateChain models a bundle as a single linear chain, so a bundle containing both halves of a cross-signed pair could never be merged into one chain and was rejected with certificate chain is malformed or broken. Property-testing showed the rejection happens in every input order and subset — a hard limitation of the merge algorithm, not an ordering fluke.

This fixes the parser itself, so all callers benefit: the Vault issuer (#7645), the Venafi issuers, and the CA issuer (which re-parses stored bundles in SignCSRTemplate). It is an alternative to #8786, which works around the parser by falling back to Vault's raw response; that fallback bypasses the parser's validation, size limits, root-stripping and deduplication for the Vault issuer only, and leaves the other issuers broken on the same input.

When the chain merge gets stuck, the parser now discards a chain whose head (leaf-most) certificate has the same subject and public key as a certificate in another chain. Treating certificates with the same subject and public key as versions of the same logical certificate follows RFC 4158 §2.4.2, which recommends disallowing the same subject name and public key pair from being repeated when building certification paths, to eliminate superfluous paths; Chromium's certificate path builder applies the same rule (BoringSSL pki/path_builder.cc:394-398). Such a chain is a redundant cross-sign branch: its head is an alternative version of a certificate already in another chain, and the certificates above the head exist only to certify that alternative. The chain containing the leaf is never discarded (its head is the leaf, which duplicates nothing), and bundles containing genuinely unrelated certificates still fail (no chain qualifies for discarding), so broken chains are still rejected. The result is that one path through the cross-signed bundle is selected, deterministically for a given input set. Inputs that previously parsed are byte-for-byte unaffected: the new logic runs only where the function previously returned the error.

The new test exhaustively parses every permutation of cross-signed bundles (an intermediate cross-signed by two roots, with both/one/no roots present; a root cross-signed by an older root, as served by Let's Encrypt) and asserts the result is one of the enumerated valid paths and is independent of input order. Without the parser change, every one of these cases fails with certificate chain is malformed or broken (verified by running the new test against the unmodified parser).

Note on scope: this makes issuance succeed by selecting one path; for a cross-signed intermediate, one variant is selected and the other discarded — it does not put both variants into tls.crt, nor both roots into ca.crt. One nuance (spotted by Copilot's review): a cross-signed root pair carries the same key in each version, so the pair can linearise into a genuine chain (the self-signed version verifies from the cross-signed one) — in that case both versions are retained in tls.crt, which is exactly what previous versions of the parser produced when they accepted such bundles, and the deterministic-per-input-set guarantee still holds. Serving both variants of a cross-signed intermediate would require relaxing the single-chain contract of PEMBundle and is best discussed separately.

Fixes #7645

Kind

/kind bug

Release Note

Certificate chains containing cross-signed certificates (as used for CA rotation, e.g. with Vault PKI) are now accepted by the Vault, Venafi and CA issuers: the chain parser deterministically selects a single path instead of failing with "certificate chain is malformed or broken".

with claude fable-5

…tificateChain

Cross-signing an intermediate or root CA (signing the same subject and
public key with two different issuers) is the recommended way to rotate
a root CA, for example in Vault PKI:
https://developer.hashicorp.com/vault/docs/secrets/pki/rotation-primitives

ParseSingleCertificateChain models a bundle as a single linear chain, so
any bundle containing both halves of a cross-signed pair could never be
merged into one chain and was rejected with 'certificate chain is
malformed or broken'. This broke the Vault, Venafi and CA issuers on
cross-signed chains.

When the chain merge gets stuck, discard a chain whose head (leaf-most)
certificate has the same subject and public key as a certificate in
another chain: such a chain is a redundant cross-sign branch, and the
certificates above its head exist only to certify the duplicate. The
chain containing the leaf is never discarded because its head is the
leaf itself, and bundles containing genuinely unrelated certificates are
still rejected because no chain qualifies for discarding.

The result is that one path through a cross-signed bundle is selected
deterministically. Inputs that previously parsed successfully are
unaffected: the new logic only runs where the function previously
returned an error.

Fixes cert-manager#7645

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: Richard Wall <[email protected]>
@cert-manager-prow cert-manager-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. labels Aug 15, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign munnerz for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@cert-manager-prow cert-manager-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 15, 2026
wallrj and others added 2 commits August 15, 2026 06:59
The PKITS section 4.5 self-issued certificates share a subject but carry
different public keys across CA key generations. They pin the boundary of
the cross-signed duplicate rule: same subject with a different public key
is an ordinary chain link, not a redundant cross-sign branch.

Certificates copied from the pyca/cryptography test vectors; PKITS is a
work of the United States government and is in the public domain.

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: Richard Wall <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates certificate-chain parsing to select one path from cross-signed CA bundles while retaining validation for unrelated certificates.

Changes:

  • Adds redundant cross-sign branch detection.
  • Adds permutation and NIST PKITS rollover tests.
  • Adds seven public-domain PKITS certificate fixtures.

Reviewed changes

Copilot reviewed 3 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pkg/util/pki/parse_certificate_chain.go Implements cross-sign branch removal.
pkg/util/pki/parse_certificate_chain_test.go Tests cross-signing and key rollover.
pkg/util/pki/testdata/pkits/README.md Documents PKITS fixture provenance.
pkg/util/pki/testdata/pkits/TrustAnchorRootCertificate.crt PKITS trust-anchor fixture.
pkg/util/pki/testdata/pkits/BasicSelfIssuedNewKeyCACert.crt PKITS new-key CA fixture.
pkg/util/pki/testdata/pkits/BasicSelfIssuedNewKeyOldWithNewCACert.crt PKITS rollover fixture.
pkg/util/pki/testdata/pkits/ValidBasicSelfIssuedOldWithNewTest1EE.crt PKITS end-entity fixture.
pkg/util/pki/testdata/pkits/BasicSelfIssuedOldKeyCACert.crt PKITS old-key CA fixture.
pkg/util/pki/testdata/pkits/BasicSelfIssuedOldKeyNewWithOldCACert.crt PKITS reverse-rollover fixture.
pkg/util/pki/testdata/pkits/ValidBasicSelfIssuedNewWithOldTest3EE.crt PKITS end-entity fixture.
Suppressed comments (1)

pkg/util/pki/parse_certificate_chain.go:299

  • Matching only subject and SPKI does not establish that these are equivalent cross-signed CA certificates. Distinct certificates that reuse a key/name—such as renewed end-entity certificates, or certificates with different CA/basic-constraints/key-usage semantics—will now cause a malformed multi-leaf bundle to be accepted while one certificate is silently dropped. Restrict elimination to the supported CA case and require the relevant certificate semantics to be compatible, with negative tests for same-key/name non-cross-sign variants.
				if bytes.Equal(chain.cert.RawSubject, node.cert.RawSubject) &&
					bytes.Equal(chain.cert.RawSubjectPublicKeyInfo, node.cert.RawSubjectPublicKeyInfo) {
					return slices.Delete(chains, i, i+1), true

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread pkg/util/pki/parse_certificate_chain.go Outdated
Comment on lines +289 to +291
func discardRedundantChain(chains []*chainNode) ([]*chainNode, bool) {
for i, chain := range chains {
for j, other := range chains {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — fixed in 2f9cd92. discardRedundantChain now builds a per-scan index from (subject, public key) to the chains containing that identity (two distinct chain indices per identity are enough to answer "does it appear outside chain i?"), so each scan visits every certificate a constant number of times, and the up to O(n) scans stay within the merge loop's own complexity bound. Also added TestParseSingleCertificateChainCrossSignedDoS, which feeds 499 unrelated cross-signed pairs (998 certificates, just under the 1000-certificate limit) — the worst case for repeated pruning — and asserts rejection in under a second, matching the file's existing DoS tests.

with claude fable-5

Comment on lines 152 to +154
if !mergedTwoChains {
return PEMBundle{}, errors.NewInvalidData("certificate chain is malformed or broken")
var discardedRedundantChain bool
chains, discardedRedundantChain = discardRedundantChain(append(chains, lastChain))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The observation is correct, but this behaviour is retained deliberately; I've documented it rather than changed it (2f9cd92), for two reasons:

  1. When a cross-signed pair linearises, the resulting chain is genuine: every link is a real, verified signature. The self-signed root verifies from its cross-signed version because both carry the same key, so leaf → int → newRoot → newRootByOldRoot with CA oldRoot is a valid single path through the bundle, not a merge accident. Normalising it away would mean pruning mid-chain self-signed certificates, which is a behaviour change that also affects inputs with no cross-signing involved.
  2. It is the pre-existing behaviour: before this PR, the same bundles produced exactly this output whenever the (sorted, deterministic) merge happened to succeed, and errored otherwise. This PR's compatibility guarantee is that the new logic only runs where the function previously returned an error — pruning the linearised pair would alter the output of previously accepted inputs and break that guarantee.

The output remains deterministic for a given input set in all cases. I've updated the function documentation, the test comment, and the PR description to state this nuance explicitly. If pruning mid-chain self-signed certificates is desirable (it would slim TLS handshakes), that's a follow-up that should be considered for all inputs, not just cross-signed ones.

with claude fable-5

Address Copilot review on cert-manager#9150: the pruning scan compared every chain
head against every certificate of every other chain, and pruning can run
once per discarded branch, making an adversarial bundle of many
cross-signed pairs cubic overall. Build a per-scan index from subject and
public key to the chains containing them, so each scan visits every
certificate a constant number of times and repeated pruning stays within
the merge loop's complexity bound. Add a worst-case test with 499
cross-signed pairs at the 1000-certificate limit.

Also document that a cross-signed pair can linearize into a genuine
chain containing both versions (for example a root that is both
self-signed and signed by an older root), which is the pre-existing
behavior for such inputs when they were accepted.

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: Richard Wall <[email protected]>
@wallrj

wallrj commented Aug 15, 2026

Copy link
Copy Markdown
Member Author

For reviewers: here is how the new tests behave when run against the unmodified parser (this branch's test file, with pkg/util/pki/parse_certificate_chain.go reverted to upstream/master):

$ git checkout upstream/master -- pkg/util/pki/parse_certificate_chain.go
$ go test ./pkg/util/pki/ -run 'TestParseSingleCertificateChainPEMCrossSigned|TestParseSingleCertificateChainPEMPKITSKeyRollover|TestParseSingleCertificateChainCrossSignedDoS' -count=1
--- FAIL: TestParseSingleCertificateChainPEMCrossSigned (0.02s)
    --- FAIL: TestParseSingleCertificateChainPEMCrossSigned/cross-signed_intermediate_with_both_roots (0.00s)
        parse_certificate_chain_test.go:377:
            	Error:      	Received unexpected error:
            	            	certificate chain is malformed or broken
    --- FAIL: TestParseSingleCertificateChainPEMCrossSigned/cross-signed_intermediate_with_one_root (0.00s)
        parse_certificate_chain_test.go:377:
            	Error:      	Received unexpected error:
            	            	certificate chain is malformed or broken
    --- FAIL: TestParseSingleCertificateChainPEMCrossSigned/cross-signed_intermediate_without_roots (0.00s)
        parse_certificate_chain_test.go:377:
            	Error:      	Received unexpected error:
            	            	certificate chain is malformed or broken
    --- FAIL: TestParseSingleCertificateChainPEMCrossSigned/cross-signed_root_with_both_roots (0.00s)
        parse_certificate_chain_test.go:377:
            	Error:      	Received unexpected error:
            	            	certificate chain is malformed or broken
FAIL
FAIL	github.com/cert-manager/cert-manager/pkg/util/pki	0.546s

All four cross-signed cases fail on master. The cross-signed-intermediate cases fail in every permutation (the merge can never linearise both variants). The cross-signed-root case fails only for some input orders — master's merge is order-dependent, so that bundle parses or errors depending on how the certificates happen to be sorted; the test asserts order-independence, which master cannot satisfy.

The tests that do pass on master are the ones codifying behaviour this PR deliberately does not change: the unrelated-certificate negative case, the PKITS key-rollover cases (same subject, different keys — must not be collapsed), and the DoS timing test (master also rejects that input quickly, just for the wrong reason).

With the fixed parser restored, the same test selection passes:

$ git checkout HEAD -- pkg/util/pki/parse_certificate_chain.go
$ go test ./pkg/util/pki/ -run 'TestParseSingleCertificateChainPEMCrossSigned|TestParseSingleCertificateChainPEMPKITSKeyRollover|TestParseSingleCertificateChainCrossSignedDoS' -count=1
ok  	github.com/cert-manager/cert-manager/pkg/util/pki	0.939s

with claude fable-5

@cert-manager-prow cert-manager-prow Bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 17, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

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

Labels

dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/bug Categorizes issue or PR as related to a bug. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support cross-signed intermediate CAs issued with Vault

2 participants