Skip to content

Fix XMLSignatureUtil to omit KeyInfo when includeKeyInfoInSignature is false#48219

Open
ozimakov wants to merge 3 commits into
keycloak:mainfrom
ozimakov:fix/xml-signature-omit-keyinfo
Open

Fix XMLSignatureUtil to omit KeyInfo when includeKeyInfoInSignature is false#48219
ozimakov wants to merge 3 commits into
keycloak:mainfrom
ozimakov:fix/xml-signature-omit-keyinfo

Conversation

@ozimakov

Copy link
Copy Markdown

Summary

XMLSignatureUtil.signImpl() was not respecting the includeKeyInfoInSignature=false flag:

  • When keyName is non-null: a stub <KeyInfo><KeyName/></KeyInfo> was still included in the signature
  • When keyName is null: an IllegalArgumentException was thrown (content cannot be empty) because the XML crypto API rejects an empty KeyInfo items list

The fix passes null directly to XMLSignatureFactory.newXMLSignature(), which is the correct way to produce a signature without a <KeyInfo> element per the javax.xml.crypto.dsig API contract and SAML spec section 4.5 (KeyInfo is optional).

A new unit test (XMLSignatureUtilTest) covers all three cases:

  • happy path (includeKeyInfoInSignature=true) — KeyInfo is present
  • includeKeyInfoInSignature=false with non-null keyName — KeyInfo is absent
  • includeKeyInfoInSignature=false with null keyName — no exception thrown, KeyInfo is absent

Closes #46302


Note: Claude Code (AI assistant) was used to help navigate the codebase, research the issue history, and draft the fix. All changes were reviewed and are fully understood by the author.

…s false

When setIncludeKeyInfoInSignature(false) was called, signImpl() still
invoked createKeyInfo(keyName, null, null), which produced a stub
<KeyInfo><KeyName/></KeyInfo> element when keyName was non-null, and
threw IllegalArgumentException when keyName was null (empty items list
is rejected by the XML crypto API).

The fix passes null directly to XMLSignatureFactory.newXMLSignature(),
which is the correct way to produce a signature without a KeyInfo element
per the javax.xml.crypto.dsig API contract.

Closes keycloak#46302

Signed-off-by: Oleg Zimakov <oleg@zimakov.net>
@ozimakov

Copy link
Copy Markdown
Author

@stianst May I get a review for this one? Thx.

Copilot AI review requested due to automatic review settings July 19, 2026 05:49

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

Fixes XML signature generation so disabling KeyInfo produces a valid signature without a <KeyInfo> element.

Changes:

  • Passes null KeyInfo when inclusion is disabled.
  • Adds regression tests for enabled, disabled, and null-key-name cases.

Reviewed changes

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

File Description
XMLSignatureUtil.java Omits KeyInfo when configured.
XMLSignatureUtilTest.java Covers KeyInfo inclusion and omission.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XMLSignatureUtil.setIncludeKeyInfoInSignature(false) fails to disinclude KeyInfo from a signature after signing

3 participants