You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@santuario.apache.org by Bruno Harbulot <Br...@manchester.ac.uk> on 2009/03/24 00:59:56 UTC

Reusing XMLSignature for signing and verifying

Hello,

I've been writing a test based on OpenSAML, which uses Apache XML 
Security 1.4.2. In this test, I'm signing an org.opensaml.xml.XMLObject 
and verifying it shortly after.
In OpenSAML, org.opensaml.xml.signature.impl.SignatureImpl (an XMLObject 
that models the XML signature) keeps an association between the DOM 
element and the instance of org.apache.xml.security.signature.XMLSignature.

Unfortunately, if the nodes are reused, the same instance of 
XMLSignature is used for verifying after being used for signing.
This causes an exception:
org.apache.xml.security.signature.XMLSignatureException: object not 
initialized for verification

If I run the verification on a different thread, it works fine.



This problem isn't specific to OpenSAML and can be reproduced when 
re-using the same instance of XMLSignature for signing and verifying.
Unless this behaviour was intentional, I've tracked down the problem to 
be due to 
org.apache.xml.security.algorithms.SignatureAlgorithm#initializeAlgorithm(boolean):

     private void initializeAlgorithm(boolean isForSigning)
         throws XMLSignatureException {
         if (_signatureAlgorithm!=null) {
	   return;
         }
         _signatureAlgorithm=isForSigning ? 
getInstanceForSigning(algorithmURI) :
             getInstanceForVerify(algorithmURI);	
	this._signatureAlgorithm
             .engineGetContextFromElement(this._constructionElement);
     }


If '_signatureAlgorithm' has already been initialised, even if it's not 
for the purpose intended for another use, it won't be initialised again. 
Commenting out the 'if' block solves the problem.

I've noticed that there was an 'isForSigning' field commented out in 
revision 515521, which completely disappeared in revision 695520 
(current one). I guess it might have been the original intent for this flag.

I'm attaching a small Maven test case.


Best wishes,

Bruno.

RE: Reusing XMLSignature for signing and verifying

Posted by Scott Cantor <ca...@osu.edu>.
Sean Mullan wrote on 2009-03-25:
> JSR 105 is consistent with your view and documents the behavior. If the
> contents of the document are subsequently modified, then you need to
> instantiate a new XMLSignature object.

Well, in this case, nothing's being modified. What happens is you sign the
DOM, and then (via the APIs OpenSAML provides in this case), an attempt is
made to verify the signature that was just created. That won't work in
general if methods like getKeyInfo() don't return what they should, since
even if mechanically the signature might verify internally, the surrounding
code that eventually gets written needs access to the details for trust
verification.

-- Scott



Re: Reusing XMLSignature for signing and verifying

Posted by Sean Mullan <Se...@Sun.COM>.
Yeah you may be right, though I don't think the behavior has ever been 
documented one way or another. Hoping Raul will answer as he is more 
familiar with that code than I am.

JSR 105 is consistent with your view and documents the behavior. If the 
contents of the document are subsequently modified, then you need to 
instantiate a new XMLSignature object.

--Sean

Scott Cantor wrote:
> Sean Mullan wrote on 2009-03-25:
>> I don't think the behavior is intentional. Can you please file a bug at
>> http://issues.apache.org/bugzilla in the security category and attach
>> your test case?
> 
> At least dating back to the 1.2 days, it was quite intentional. I found
> numerous issues with reusing the objects back then because the library
> wouldn't reparse the result of signing so that information like the KeyInfo
> would be populated. At the time it seemed quite unsupported to do this, but
> I don't know if that's changed.
> 
> -- Scott
> 
> 


RE: Reusing XMLSignature for signing and verifying

Posted by Scott Cantor <ca...@osu.edu>.
Sean Mullan wrote on 2009-03-25:
> I don't think the behavior is intentional. Can you please file a bug at
> http://issues.apache.org/bugzilla in the security category and attach
> your test case?

At least dating back to the 1.2 days, it was quite intentional. I found
numerous issues with reusing the objects back then because the library
wouldn't reparse the result of signing so that information like the KeyInfo
would be populated. At the time it seemed quite unsupported to do this, but
I don't know if that's changed.

-- Scott



Re: Reusing XMLSignature for signing and verifying

Posted by Sean Mullan <Se...@Sun.COM>.
I don't think the behavior is intentional. Can you please file a bug at 
http://issues.apache.org/bugzilla in the security category and attach 
your test case?

Thanks,
Sean

Bruno Harbulot wrote:
> Hello,
> 
> I've been writing a test based on OpenSAML, which uses Apache XML 
> Security 1.4.2. In this test, I'm signing an org.opensaml.xml.XMLObject 
> and verifying it shortly after.
> In OpenSAML, org.opensaml.xml.signature.impl.SignatureImpl (an XMLObject 
> that models the XML signature) keeps an association between the DOM 
> element and the instance of org.apache.xml.security.signature.XMLSignature.
> 
> Unfortunately, if the nodes are reused, the same instance of 
> XMLSignature is used for verifying after being used for signing.
> This causes an exception:
> org.apache.xml.security.signature.XMLSignatureException: object not 
> initialized for verification
> 
> If I run the verification on a different thread, it works fine.
> 
> 
> 
> This problem isn't specific to OpenSAML and can be reproduced when 
> re-using the same instance of XMLSignature for signing and verifying.
> Unless this behaviour was intentional, I've tracked down the problem to 
> be due to 
> org.apache.xml.security.algorithms.SignatureAlgorithm#initializeAlgorithm(boolean): 
> 
> 
>     private void initializeAlgorithm(boolean isForSigning)
>         throws XMLSignatureException {
>         if (_signatureAlgorithm!=null) {
>        return;
>         }
>         _signatureAlgorithm=isForSigning ? 
> getInstanceForSigning(algorithmURI) :
>             getInstanceForVerify(algorithmURI);   
>     this._signatureAlgorithm
>             .engineGetContextFromElement(this._constructionElement);
>     }
> 
> 
> If '_signatureAlgorithm' has already been initialised, even if it's not 
> for the purpose intended for another use, it won't be initialised again. 
> Commenting out the 'if' block solves the problem.
> 
> I've noticed that there was an 'isForSigning' field commented out in 
> revision 515521, which completely disappeared in revision 695520 
> (current one). I guess it might have been the original intent for this 
> flag.
> 
> I'm attaching a small Maven test case.
> 
> 
> Best wishes,
> 
> Bruno.