You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by "Peter De Maeyer (Jira)" <ji...@apache.org> on 2020/11/15 21:06:00 UTC
[jira] [Comment Edited] (SANTUARIO-541) Indent secure XML elements
the same way as the input XML
[ https://issues.apache.org/jira/browse/SANTUARIO-541?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17232398#comment-17232398 ]
Peter De Maeyer edited comment on SANTUARIO-541 at 11/15/20, 9:05 PM:
----------------------------------------------------------------------
After doing a lot of research, I finally have a fix ready.
As a prerequisite for understanding my fix, let me explain a couple of things I've learned myself whilst working on this.
h1. Indentation of embedded XML signatures
Indenting secure XML elements is a bit tricky for signatures because the additional indentation that is put around the {{<Signature/>}} needs to be part of the digest (and thus signature) itself, while the {{<Signature/>}} itself is _excluded_ from the digest (and signature).
Please realize that XML canonicalization, which XML security heavily depends upon, does _not_ ignore whitespace.
Regardless of whether you indent secure elements or not, XML signatures are sensitive to whitespace, period.
Once signed, you cannot change _any_ whitespace in the signed content, or the signature will no longer match.
Consider the following input XML, where {{·}} is a space and {{¶}} is a line break:
{code:xml}
<Root>¶
··<Node/>¶
</Root>¶
{code}
Assume the signature is embedded right after the root tag:
{code:xml}
<Root>¶
··<dsig:Signature/>¶
··<Node/>¶
</Root>¶
{code}
The digest (which in turn is used to compute the signature) is computed by _excluding_ the {{<dsig:Signature/>}} element but _not_ the additional whitespace {{¶··}}, so the additional {{¶··}} is _included_ in the digest.
The input for the digest is thus:
{code:xml}
<Root>¶
··¶
··<Node/>¶
</Root>¶
{code}
h1. Where the hell did the newline between the XML document declaration and the start of the root tag go?
Consider an XML input document like this:
{code:xml}
<?xml version='1.0' encoding='UTF-8'?>¶
<Root/>
{code}
When you hook this up to a reader/writer using {{XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);}}, the {{¶}} has been inhibited somewhere so the result looks like this:
{code:xml}
<?xml version='1.0' encoding='UTF-8'?><Root/>
{code}
The reason is that the Woodstox XML reader by default inhibits this what they call "prolog whitespace".
I don't understand why they consider that a good default, but that's besides the point here, what matters is that you can fix this with a property on the {{XMLInputFactory}}:
{code:java}
XMLInputFactory.newInstance().setProperty("org.codehaus.stax2.reportPrologWhitespace", true);
{code}
was (Author: peterdm):
After doing a lot of research, I finally have a fix ready.
As a prerequisite for understanding my fix, let me explain a couple of things I've learned myself whilst working on this.
h1. Indentation of embedded XML signatures
Indenting secure XML elements is a bit tricky for signatures because the additional indentation that is put around the {{<Signature/>}} needs to be part of the digest (and thus signature) itself, while the {{<Signature/>}} itself is _excluded_ from the digest (and signature).
Please realize that XML canonicalization, which XML security heavily depends upon, does _not_ ignore whitespace.
Regardless of whether you indent secure elements or not, XML signatures are sensitive to whitespace, period.
Once signed, you cannot change _any_ whitespace in the signed content, or the signature will no longer match.
Consider the following input XML, where {{·}} is a space and {{¶}} is a line break:
{code:xml}
<Root>¶
··<Node/>¶
</Root>¶
{code}
Assume the signature is embedded right after the root tag:
{code:xml}
<Root>¶
··<dsig:Signature/>¶
··<Node/>¶
··</Root>¶
{code}
The digest (which in turn is used to compute the signature) is computed by _excluding_ the {{<dsig:Signature/>}} element but _not_ the additional whitespace {{¶··}}, so the additional {{¶··}} is _included_ in the digest.
The input for the digest is thus:
{code:xml}
<Root>¶
··¶
··<Node/>¶
··</Root>¶
{code}
h1. Where the hell did the newline between the XML document declaration and the start of the root tag go?
Consider an XML input document like this:
{code:xml}
<?xml version='1.0' encoding='UTF-8'?>¶
<Root/>
{code}
When you hook this up to a reader/writer using {{XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);}}, the {{¶}} has been inhibited somewhere so the result looks like this:
{code:xml}
<?xml version='1.0' encoding='UTF-8'?><Root/>
{code}
The reason is that the Woodstox XML reader by default inhibits this what they call "prolog whitespace".
I don't understand why they consider that a good default, but that's besides the point here, what matters is that you can fix this with a property on the {{XMLInputFactory}}:
{code:java}
XMLInputFactory.newInstance().setProperty("org.codehaus.stax2.reportPrologWhitespace", true);
{code}
> Indent secure XML elements the same way as the input XML
> --------------------------------------------------------
>
> Key: SANTUARIO-541
> URL: https://issues.apache.org/jira/browse/SANTUARIO-541
> Project: Santuario
> Issue Type: Improvement
> Components: Java
> Affects Versions: Java 2.1.5
> Reporter: Peter De Maeyer
> Assignee: Colm O hEigeartaigh
> Priority: Major
>
> Currently, secured XML elements are not indented, even when the input XML is.
> Take for example {{EncryptionCreationTest.testEncryptionIdToSecureSupersedesName}}, which has input with line breaks and indentation:
> {code:xml}
> <?xml version='1.0'?>
> <Root>
> <Branch1 attr1='abc'/>
> <Branch2 attr1='def'/>
> </Root>
> {code}
> and encrypted output with secured elements that don't have line breaks and indentation:
> {code}
> <?xml version='1.0' encoding='UTF-8'?><Root>
> <Branch1 attr1="abc"/>
> <xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Id="G79d19501-544f-403d-831a-f99e5558a102" Type="http://www.w3.org/2001/04/xmlenc#Element"><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/><dsig:KeyInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"><xenc:EncryptedKey Id="EKb7c2cb54-1548-44cd-978a-be83fa46c707"><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#kw-aes192"/><xenc:CipherData><xenc:CipherValue>D5c97seyBtid8tGxwjKEzz+xpKt1oCwk</xenc:CipherValue></xenc:CipherData></xenc:EncryptedKey></dsig:KeyInfo><xenc:CipherData><xenc:CipherValue>jWJS/BLcC03RlyqxKYUXnK7zuqvdO8Mu5T9OPIItvmHjeHLEWf9GZrHZMukDFviZ
> </xenc:CipherValue></xenc:CipherData></xenc:EncryptedData>
> </Root>
> {code}
> It would be nice if the encrypted elements would use the same indentation and line breaks as the original input.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)