You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@santuario.apache.org by Ian Hummel <hu...@parityinc.net> on 2008/02/15 23:31:10 UTC

Help validating XML signature

Hi there,


I am having some issues validating the signature on the following XML  
file using both xmlsec-1.4.0 and xmlsec-1.4.1:

<saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion"  
AssertionID="urn:uuid:44FFFE2742C5DBB6311203096718823"  
IssueInstant="2008-02-15T17:31:58.817Z" Issuer="https://rh150.sohosmart.net/TokenService/services/Trust 
" MajorVersion="1" MinorVersion="1">
     <saml:Conditions NotBefore="2008-02-15T17:31:58.817Z"  
NotOnOrAfter="2008-02-22T17:31:58.817Z"/>
     <saml:AttributeStatement>
         <saml:Subject>
             <saml:SubjectConfirmation>
                 <saml:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer 
</saml:ConfirmationMethod>
             </saml:SubjectConfirmation>
         </saml:Subject>
         <saml:Attribute AttributeName="privatepersonalidentifier"  
AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims 
">
              
<saml:AttributeValue>NBkhyg1zm4UTqbpjkQg7LhXFlS8EpMpDtnphO1SvASA=</ 
saml:AttributeValue>
         </saml:Attribute>
         <saml:Attribute AttributeName="emailaddress"  
AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims 
">
             <saml:AttributeValue>j@j.com</saml:AttributeValue>
         </saml:Attribute>
     </saml:AttributeStatement>
     <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
         <ds:SignedInfo>
             <CanonicalizationMethod xmlns="http://www.w3.org/2000/09/xmldsig# 
" Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
             <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1 
"/>
             <ds:Reference URI="#urn:uuid:44FFFE2742C5DBB6311203096718823 
">
                 <ds:Transforms>
                     <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature 
"/>
                     <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n# 
"/>
                 </ds:Transforms>
                 <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1 
"/>
                 <DigestValue xmlns="http://www.w3.org/2000/09/ 
xmldsig#">IjaxSnB43LryrBM25gCeFFEoaMc=</DigestValue>
             </ds:Reference>
         </ds:SignedInfo>
         <SignatureValue xmlns="http://www.w3.org/2000/09/ 
xmldsig#">BQadvf3/ 
JZquVfzTGVa0OSGkmddVwMWdn30JEHTKYZvT26Goxg62iYg9xkB527dphU2bHBd2KICyo2cliivKsOxFqKpOPcIxgft 
/y+vv+RqE5cTn2BDsVZ6WfWWfiXHgEUAkzF+BUBoGG7mJ1Gs8ycZoIl/ 
9pYgCzeUjSJXYNSU=</SignatureValue>
         <ds:KeyInfo>
             <ds:KeyValue>
                 <ds:RSAKeyValue>
                     <Modulus xmlns="http://www.w3.org/2000/09/ 
xmldsig#">
imhZHVDvtboiubhWbcNFyIDOamOaOVWIdD6QpDq8i/D3MltwgBTDorX+/ 
prqfj8RMWxbmYlmbuts
q9ZBHlaCz8eKdXqZQJ3bUmqDtAXU6PnAM0J4UsW/ 
S1ikTEVgcpV6mGpjsEF8UojhcNOJkwMyDipk
xmtcY+YknWkiJ5sl+LE=
</Modulus>
                     <Exponent xmlns="http://www.w3.org/2000/09/ 
xmldsig#">AQAB</Exponent>
                 </ds:RSAKeyValue>
             </ds:KeyValue>
         </ds:KeyInfo>
     </ds:Signature>
</saml:Assertion>


The code I am using to verify the signature is the following:

	public static boolean verifyXmlSignature(Document doc) {
	    Element signatureElement = (Element)  
doc.getElementsByTagNameNS(Constants.SignatureSpecNS,  
"Signature").item(0);
	    logger.debug("signatureElement? " + signatureElement);
	    XMLSignature signature;
		try {
			signature = new XMLSignature(signatureElement,  
System.getProperty("java.io.tmpdir"));
		} catch (XMLSecurityException e) {
			logger.warn("error verifying digital signature", e);
			return false;
		}

	    SignedInfo signedInfo = signature.getSignedInfo();
	    logger.info("signedInfo? " + signedInfo);
	    signature.setFollowNestedManifests(true);
	    KeyInfo ki = signature.getKeyInfo();
		logger.info("keyInfo is: " + ki);
	    PublicKey pk;
		try {
			pk = signature.getKeyInfo().getPublicKey();
			logger.info("public key is: " + pk);

		} catch (KeyResolverException e) {
			logger.warn("Signature did not contain public key data", e);
			return false;
		}
		try {
			System.out.println("KEY The XML signature in file "
					                     + (signature.checkSignatureValue(pk)
					                        ? "valid (good)"
					                        : "invalid !!!!! (bad)"));
		} catch (XMLSignatureException e) {
			logger.warn("Signature was invalid", e);
			return false;
		}
		return true;
	}


it always says the signature is invalid... I wonder if I am even  
setting everything up correctly?  I got most of the code above from  
the sample files included in the 1.4.0 dist...

Am I missing something fundamental?


Thank you for any insight!

- ian.


Re: Help validating XML signature

Posted by Satish Burnwal <sb...@cisco.com>.
Ian Hummel <hummel <at parityinc.net writes:

 
 Hi Brent, thanks for the reply.
 
 I made the two changes you suggested but the signature is still not  
 validating.  I have been adding println's all throughout the 1.4.0  
 code to better understand why, and it seems that the C14N transform is  
 returning null at one point.
 
 The Reference object loops through all the configured transforms, the
 http://www.w3.org/2000/09/xmldsig#enveloped-signature 
   transform returns a (modified, i assume) XMLSignatureInput, then we  
 run through the C14N and it returns null!
 
 The block of code is in CanonicalizerBase.java in method  
 engineCanonicalizeXPathNodeSetInternal.
 
           this.canonicalizeXPathNodeSet(doc,doc);
           System.out.println("canonicalizeXPathNodeSet is ok i  
 guess...");
           this._writer.close();
           if (this._writer instanceof ByteArrayOutputStream) {
              byte []  
 sol=((ByteArrayOutputStream)this._writer).toByteArray();
              if (reset) {
              	((ByteArrayOutputStream)this._writer).reset();
              }
           	return sol;
           }  else if (this._writer instanceof  
 UnsyncByteArrayOutputStream) {
          	 byte  
 []result=((UnsyncByteArrayOutputStream)this._writer).toByteArray();
               if (reset) {
                   ((UnsyncByteArrayOutputStream)this._writer).reset();
               }
            	 return result;
           }
           return null;   <------ returns this null!
 
 The _writer instance in this case was created in Reference.java method  
 calculateDigest
 
           MessageDigestAlgorithm mda = this.getMessageDigestAlgorithm();
 
           mda.reset();
           DigesterOutputStream diOs=new DigesterOutputStream(mda);
           OutputStream os=new UnsyncBufferedOutputStream(diOs);   <---  
 not check via instanceof in CanonicalizerBase
           XMLSignatureInput  
 output=this.dereferenceURIandPerformTransforms(os);
           output.updateOutputStream(os);
           os.flush();
 
 Do you (or anyone) think this could be a bug?  I 1.4.1 mentioned some  
 bug in C14N, but I have tried that version as well without success....
 
 thanks for the help!
 
 - ian.

Ian and all,
Did you get any solution to the problem you are facing above. I am also facing
the same problem - I also have as signed token which has transforms of
http://www.w3.org/2000/09/xmldsig#enveloped-signature followed by
http://www.w3.org/2001/10/xml-exc-c14n#. I tried to debug this by taking the
xmlsec src codes and I found the same problem as mentioned by Ian above. Pls
help in resolving this.

-Satish



Re: Help validating XML signature

Posted by Ian Hummel <hu...@parityinc.net>.
Hi Brent, thanks for the reply.

I made the two changes you suggested but the signature is still not  
validating.  I have been adding println's all throughout the 1.4.0  
code to better understand why, and it seems that the C14N transform is  
returning null at one point.

The Reference object loops through all the configured transforms, the http://www.w3.org/2000/09/xmldsig#enveloped-signature 
  transform returns a (modified, i assume) XMLSignatureInput, then we  
run through the C14N and it returns null!

The block of code is in CanonicalizerBase.java in method  
engineCanonicalizeXPathNodeSetInternal.

          this.canonicalizeXPathNodeSet(doc,doc);
          System.out.println("canonicalizeXPathNodeSet is ok i  
guess...");
          this._writer.close();
          if (this._writer instanceof ByteArrayOutputStream) {
             byte []  
sol=((ByteArrayOutputStream)this._writer).toByteArray();
             if (reset) {
             	((ByteArrayOutputStream)this._writer).reset();
             }
          	return sol;
          }  else if (this._writer instanceof  
UnsyncByteArrayOutputStream) {
         	 byte  
[]result=((UnsyncByteArrayOutputStream)this._writer).toByteArray();
              if (reset) {
                  ((UnsyncByteArrayOutputStream)this._writer).reset();
              }
           	 return result;
          }
          return null;   <------ returns this null!

The _writer instance in this case was created in Reference.java method  
calculateDigest


          MessageDigestAlgorithm mda = this.getMessageDigestAlgorithm();

          mda.reset();
          DigesterOutputStream diOs=new DigesterOutputStream(mda);
          OutputStream os=new UnsyncBufferedOutputStream(diOs);   <---  
not check via instanceof in CanonicalizerBase
          XMLSignatureInput  
output=this.dereferenceURIandPerformTransforms(os);
          output.updateOutputStream(os);
          os.flush();



Do you (or anyone) think this could be a bug?  I 1.4.1 mentioned some  
bug in C14N, but I have tried that version as well without success....


thanks for the help!

- ian.




On Feb 15, 2008, at 5:50 PM, Brent Putman wrote:

>
>
> Ian Hummel wrote:
>>
>>
>>            signature = new XMLSignature(signatureElement,
>> System.getProperty("java.io.tmpdir"));
>
>
>
> That second arg doesn't look right, it's the base URI for resolving
> relative URI references.  The examples you saw may have that because
> they were signing files that lived in that directory or something  
> (so a
> detached signature).  For this kind of enveloped signature with a same
> document fragment URI, just pass an "" (empty string) there.  May or  
> may
> not be the actual problem.
>
>
>>
>>        SignedInfo signedInfo = signature.getSignedInfo();
>>        logger.info("signedInfo? " + signedInfo);
>>        signature.setFollowNestedManifests(true);
>
>
> You don't need to set that flag.  You don't have any Manifests in an
> enveloped SAML Assertion signature like this.  Probably is not doing
> anything bad, so I doubt is the problem.
>
>
>
>
> Everything else looks good off-hand.
>
>
> --Brent
>


Re: Help validating XML signature

Posted by Brent Putman <pu...@georgetown.edu>.

Ian Hummel wrote:
>
>
>             signature = new XMLSignature(signatureElement, 
> System.getProperty("java.io.tmpdir"));



That second arg doesn't look right, it's the base URI for resolving 
relative URI references.  The examples you saw may have that because 
they were signing files that lived in that directory or something (so a 
detached signature).  For this kind of enveloped signature with a same 
document fragment URI, just pass an "" (empty string) there.  May or may 
not be the actual problem.


>      
>         SignedInfo signedInfo = signature.getSignedInfo();
>         logger.info("signedInfo? " + signedInfo);
>         signature.setFollowNestedManifests(true);


You don't need to set that flag.  You don't have any Manifests in an 
enveloped SAML Assertion signature like this.  Probably is not doing 
anything bad, so I doubt is the problem.




Everything else looks good off-hand.


--Brent


Re: Help validating XML signature

Posted by Sean Mullan <Se...@Sun.COM>.
Ian Hummel wrote:
> Hi Sean,
> 
> That's a very good observation, thanks for your help!  Basically this 
> information is coming from a Java Infocard implementation called 
> "Higgins."  To be safe, I tried to validate a token that Cardspace (MS' 
> implementation in Vista) uploads to my webapp, but it fails too!  Below 
> you can find the XML that cardspace uploads to my app.

Yep, that's not valid either.

I also notice that the signature below is nicely formatted, indented, 
etc..  Whitespace of this sort is significant. The whitespace would have 
to be inserted *before* the signature was generated. However, this nice 
formatting is not very useful since it wastes space, unless you want the 
signature to be more readable as part of a course or test suite or 
something like that. I'm taking a wild guess, but I suspect that 
something (your webapp, or a serializer) is reformatting the signature 
and invalidating it.

--Sean

> 
> But, what you said about namespaces got me thinking... this XML is 
> initially encrypted with xml-enc, so I wonder if perhaps I am decrypting 
> incorrectly and stripping some necessary namespace info that would be 
> needed to verify the sig...
> 
> 
> Thanks!
> 
> - ian.
> 
> 
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" 
> AssertionID="uuid:8e024334-b6bb-44ec-a3b1-493b4e426cc6" 
> IssueInstant="2008-02-06T22:16:05.460Z" 
> Issuer="http://schemas.xmlsoap.org/ws/2005/05/identity/issuer/self" 
> MajorVersion="1" MinorVersion="1">
>     <saml:Conditions NotBefore="2008-02-06T22:16:05.460Z" 
> NotOnOrAfter="2008-02-06T23:16:05.460Z">
>         <saml:AudienceRestrictionCondition>
>             
> <saml:Audience>https://192.168.1.101:8443/jumpstart/congratulations.jsp</saml:Audience> 
> 
>         </saml:AudienceRestrictionCondition>
>     </saml:Conditions>
>     <saml:AttributeStatement>
>         <saml:Subject>
>             <saml:SubjectConfirmation>
>                 
> <saml:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer</saml:ConfirmationMethod> 
> 
>             </saml:SubjectConfirmation>
>         </saml:Subject>
>         <saml:Attribute AttributeName="emailaddress" 
> AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims">
>             <saml:AttributeValue>t@u.com</saml:AttributeValue>
>         </saml:Attribute>
>         <saml:Attribute AttributeName="privatepersonalidentifier" 
> AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims">
>             
> <saml:AttributeValue>loKpgGdoggnZW8llohOq7lPXvD8YeEVEWHYtvBsObLw=</saml:AttributeValue> 
> 
>         </saml:Attribute>
>     </saml:AttributeStatement>
>     <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
>         <SignedInfo>
>             <CanonicalizationMethod 
> Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
>             <SignatureMethod 
> Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
>             <Reference URI="#uuid:8e024334-b6bb-44ec-a3b1-493b4e426cc6">
>                 <Transforms>
>                     <Transform 
> Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
>                     <Transform 
> Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
>                 </Transforms>
>                 <DigestMethod 
> Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
>                 <DigestValue>qSrQXyGPKvrh37EABA47zCalZb0=</DigestValue>
>             </Reference>
>         </SignedInfo>
>         
> <SignatureValue>vYVqFEoae5+qOcVGQpWrvrs/1xAk4NYyljxO59D6zB3Jr8eHqO3EgPbWl12BOTsMeET1+Y68amao6pZ9MyEtdjZ0BNCFpPEBDvkm/jwy+gYyLtmRk7G/vhtjfCZAQgJoTs1jiTG8ZUIIAEgfGtKuD4u6hiYwf+5XRaLSZ7JzuDiTqt7Z/zzyepQGofKG9L11+KvniZnqt8Iux8JjNWqnKv8unjbBIb//cdAi7khFmqy1/JmvITCPW9DRDWnKQwGe9/hKFpdkdxTH7EHYgTaHR6O1ekJe7BzSNCo9t5Qo4pAtOQ0HSnlHhMUWWjUMGvkYS/8gJm/6F3FP47YVvKM2DA==</SignatureValue> 
> 
>         <KeyInfo>
>             <KeyValue>
>                 <RSAKeyValue>
>                     
> <Modulus>3YlDc8RMJV3EH2dkE4ayP1krDn3IFe0mW7gXeMWj5W7MPyL48zYlsjNWMKw8Kf7AjNYnI5DzSzTWOq98nYaK4fIRHib9CQs9dw/IRZkFyxifKFLtQmAsA2Z7jXj5sLFCIt1sAPx78nWUulxod8UMaWwKDFp2Jw6GO+bwg/X8IS7kAjKtZC7R4UFbhgbNmYbwPUYL1+y8nVtbE9FOXVlUqve+zPdmIN93N2vcKNVo1a2sfbn63IY7pfGWxJh06LaCJL2FqCe7HsV3YHgUmxuot2oMjr5gWa3SoKCrSzdTtx6dnEeW189NsflBsNV09xFd/BH2N4cEGqbe4tSH/36IYw==</Modulus> 
> 
>                     <Exponent>AQAB</Exponent>
>                 </RSAKeyValue>
>             </KeyValue>
>         </KeyInfo>
>     </Signature>
> </saml:Assertion>
> 
> On Feb 20, 2008, at 2:05 PM, Sean Mullan wrote:
> 
>> The signature looks a bit odd. Some of the XML Signature elements are
>> defined in the dsig namespace bound by a 'ds' prefix, while others don't
>> have a 'ds' prefix (see CanonicalizationMethod, SignatureValue and the
>> DigestValue elements).
>>
>> When I validated the signature, the cryptographic signature itself was
>> invalid, so I'm wondering if the SignedInfo contents have been modified
>> since the signature was generated. How did you generate this signature
>> and why the inconsistency in namespaces?
>>
>> --Sean
>>
>> Ian Hummel wrote:
>>> Hi there,
>>>
>>>
>>> I am having some issues validating the signature on the following XML
>>> file using both xmlsec-1.4.0 and xmlsec-1.4.1:
>>>
>>> <saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion"
>>> AssertionID="urn:uuid:44FFFE2742C5DBB6311203096718823"
>>> IssueInstant="2008-02-15T17:31:58.817Z"
>>> Issuer="https://rh150.sohosmart.net/TokenService/services/Trust"
>>> MajorVersion="1" MinorVersion="1">
>>>    <saml:Conditions NotBefore="2008-02-15T17:31:58.817Z"
>>> NotOnOrAfter="2008-02-22T17:31:58.817Z"/>
>>>    <saml:AttributeStatement>
>>>        <saml:Subject>
>>>            <saml:SubjectConfirmation>
>>>
>>> <saml:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer</saml:ConfirmationMethod> 
>>>
>>>
>>>            </saml:SubjectConfirmation>
>>>        </saml:Subject>
>>>        <saml:Attribute AttributeName="privatepersonalidentifier"
>>> AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims"> 
>>>
>>>
>>> <saml:AttributeValue>NBkhyg1zm4UTqbpjkQg7LhXFlS8EpMpDtnphO1SvASA=</saml:AttributeValue> 
>>>
>>>
>>>        </saml:Attribute>
>>>        <saml:Attribute AttributeName="emailaddress"
>>> AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims"> 
>>>
>>>            <saml:AttributeValue>j@j.com</saml:AttributeValue>
>>>        </saml:Attribute>
>>>    </saml:AttributeStatement>
>>>    <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
>>>        <ds:SignedInfo>
>>>            <CanonicalizationMethod
>>> xmlns="http://www.w3.org/2000/09/xmldsig#"
>>> Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
>>>            <ds:SignatureMethod
>>> Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
>>>            <ds:Reference 
>>> URI="#urn:uuid:44FFFE2742C5DBB6311203096718823">
>>>                <ds:Transforms>
>>>                    <ds:Transform
>>> Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
>>>                    <ds:Transform
>>> Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
>>>                </ds:Transforms>
>>>                <ds:DigestMethod
>>> Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
>>>                <DigestValue
>>> xmlns="http://www.w3.org/2000/09/xmldsig#">IjaxSnB43LryrBM25gCeFFEoaMc=</DigestValue> 
>>>
>>>
>>>            </ds:Reference>
>>>        </ds:SignedInfo>
>>>        <SignatureValue
>>> xmlns="http://www.w3.org/2000/09/xmldsig#">BQadvf3/JZquVfzTGVa0OSGkmddVwMWdn30JEHTKYZvT26Goxg62iYg9xkB527dphU2bHBd2KICyo2cliivKsOxFqKpOPcIxgft/y+vv+RqE5cTn2BDsVZ6WfWWfiXHgEUAkzF+BUBoGG7mJ1Gs8ycZoIl/9pYgCzeUjSJXYNSU=</SignatureValue> 
>>>
>>>
>>>        <ds:KeyInfo>
>>>            <ds:KeyValue>
>>>                <ds:RSAKeyValue>
>>>                    <Modulus xmlns="http://www.w3.org/2000/09/xmldsig#">
>>> imhZHVDvtboiubhWbcNFyIDOamOaOVWIdD6QpDq8i/D3MltwgBTDorX+/prqfj8RMWxbmYlmbuts 
>>>
>>>
>>> q9ZBHlaCz8eKdXqZQJ3bUmqDtAXU6PnAM0J4UsW/S1ikTEVgcpV6mGpjsEF8UojhcNOJkwMyDipk 
>>>
>>>
>>> xmtcY+YknWkiJ5sl+LE=
>>> </Modulus>
>>>                    <Exponent
>>> xmlns="http://www.w3.org/2000/09/xmldsig#">AQAB</Exponent>
>>>                </ds:RSAKeyValue>
>>>            </ds:KeyValue>
>>>        </ds:KeyInfo>
>>>    </ds:Signature>
>>> </saml:Assertion>
>>>
>>>
>>> The code I am using to verify the signature is the following:
>>>
>>>    public static boolean verifyXmlSignature(Document doc) {
>>>        Element signatureElement = (Element)
>>> doc.getElementsByTagNameNS(Constants.SignatureSpecNS, 
>>> "Signature").item(0);
>>>        logger.debug("signatureElement? " + signatureElement);
>>>        XMLSignature signature;
>>>        try {
>>>            signature = new XMLSignature(signatureElement,
>>> System.getProperty("java.io.tmpdir"));
>>>        } catch (XMLSecurityException e) {
>>>            logger.warn("error verifying digital signature", e);
>>>            return false;
>>>        }
>>>
>>>        SignedInfo signedInfo = signature.getSignedInfo();
>>>        logger.info("signedInfo? " + signedInfo);
>>>        signature.setFollowNestedManifests(true);
>>>        KeyInfo ki = signature.getKeyInfo();
>>>        logger.info("keyInfo is: " + ki);
>>>        PublicKey pk;
>>>        try {
>>>            pk = signature.getKeyInfo().getPublicKey();
>>>            logger.info("public key is: " + pk);
>>>
>>>        } catch (KeyResolverException e) {
>>>            logger.warn("Signature did not contain public key data", e);
>>>            return false;
>>>        }
>>>        try {
>>>            System.out.println("KEY The XML signature in file "
>>>                                         +
>>> (signature.checkSignatureValue(pk)
>>>                                            ? "valid (good)"
>>>                                            : "invalid !!!!! (bad)"));
>>>        } catch (XMLSignatureException e) {
>>>            logger.warn("Signature was invalid", e);
>>>            return false;
>>>        }
>>>        return true;
>>>    }
>>>
>>>
>>> it always says the signature is invalid... I wonder if I am even setting
>>> everything up correctly?  I got most of the code above from the sample
>>> files included in the 1.4.0 dist...
>>>
>>> Am I missing something fundamental?
>>>
>>>
>>> Thank you for any insight!
>>>
>>> - ian.
>>>
>>
> 


Re: Help validating XML signature

Posted by Ian Hummel <hu...@parityinc.net>.
Hi Sean,

That's a very good observation, thanks for your help!  Basically this  
information is coming from a Java Infocard implementation called  
"Higgins."  To be safe, I tried to validate a token that Cardspace  
(MS' implementation in Vista) uploads to my webapp, but it fails too!   
Below you can find the XML that cardspace uploads to my app.

But, what you said about namespaces got me thinking... this XML is  
initially encrypted with xml-enc, so I wonder if perhaps I am  
decrypting incorrectly and stripping some necessary namespace info  
that would be needed to verify the sig...


Thanks!

- ian.



<?xml version="1.0" encoding="UTF-8"?>
<saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion"  
AssertionID="uuid:8e024334-b6bb-44ec-a3b1-493b4e426cc6"  
IssueInstant="2008-02-06T22:16:05.460Z" Issuer="http://schemas.xmlsoap.org/ws/2005/05/identity/issuer/self 
" MajorVersion="1" MinorVersion="1">
     <saml:Conditions NotBefore="2008-02-06T22:16:05.460Z"  
NotOnOrAfter="2008-02-06T23:16:05.460Z">
         <saml:AudienceRestrictionCondition>
             <saml:Audience>https://192.168.1.101:8443/jumpstart/congratulations.jsp 
</saml:Audience>
         </saml:AudienceRestrictionCondition>
     </saml:Conditions>
     <saml:AttributeStatement>
         <saml:Subject>
             <saml:SubjectConfirmation>
                 <saml:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer 
</saml:ConfirmationMethod>
             </saml:SubjectConfirmation>
         </saml:Subject>
         <saml:Attribute AttributeName="emailaddress"  
AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims 
">
             <saml:AttributeValue>t@u.com</saml:AttributeValue>
         </saml:Attribute>
         <saml:Attribute AttributeName="privatepersonalidentifier"  
AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims 
">
              
<saml:AttributeValue>loKpgGdoggnZW8llohOq7lPXvD8YeEVEWHYtvBsObLw=</ 
saml:AttributeValue>
         </saml:Attribute>
     </saml:AttributeStatement>
     <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
         <SignedInfo>
             <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n# 
"/>
             <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1 
"/>
             <Reference URI="#uuid:8e024334-b6bb-44ec- 
a3b1-493b4e426cc6">
                 <Transforms>
                     <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature 
"/>
                     <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n# 
"/>
                 </Transforms>
                 <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1 
"/>
                 <DigestValue>qSrQXyGPKvrh37EABA47zCalZb0=</DigestValue>
             </Reference>
         </SignedInfo>
         <SignatureValue>vYVqFEoae5+qOcVGQpWrvrs/ 
1xAk4NYyljxO59D6zB3Jr8eHqO3EgPbWl12BOTsMeET1+Y68amao6pZ9MyEtdjZ0BNCFpPEBDvkm/jwy+gYyLtmRk7G/vhtjfCZAQgJoTs1jiTG8ZUIIAEgfGtKuD4u6hiYwf+5XRaLSZ7JzuDiTqt7Z/zzyepQGofKG9L11+KvniZnqt8Iux8JjNWqnKv8unjbBIb//cdAi7khFmqy1/JmvITCPW9DRDWnKQwGe9/hKFpdkdxTH7EHYgTaHR6O1ekJe7BzSNCo9t5Qo4pAtOQ0HSnlHhMUWWjUMGvkYS/8gJm/6F3FP47YVvKM2DA==</SignatureValue>
         <KeyInfo>
             <KeyValue>
                 <RSAKeyValue>
                      
<Modulus>3YlDc8RMJV3EH2dkE4ayP1krDn3IFe0mW7gXeMWj5W7MPyL48zYlsjNWMKw8Kf7AjNYnI5DzSzTWOq98nYaK4fIRHib9CQs9dw/IRZkFyxifKFLtQmAsA2Z7jXj5sLFCIt1sAPx78nWUulxod8UMaWwKDFp2Jw6GO+bwg/X8IS7kAjKtZC7R4UFbhgbNmYbwPUYL1+y8nVtbE9FOXVlUqve+zPdmIN93N2vcKNVo1a2sfbn63IY7pfGWxJh06LaCJL2FqCe7HsV3YHgUmxuot2oMjr5gWa3SoKCrSzdTtx6dnEeW189NsflBsNV09xFd/BH2N4cEGqbe4tSH/36IYw==</Modulus>
                     <Exponent>AQAB</Exponent>
                 </RSAKeyValue>
             </KeyValue>
         </KeyInfo>
     </Signature>
</saml:Assertion>

On Feb 20, 2008, at 2:05 PM, Sean Mullan wrote:

> The signature looks a bit odd. Some of the XML Signature elements are
> defined in the dsig namespace bound by a 'ds' prefix, while others  
> don't
> have a 'ds' prefix (see CanonicalizationMethod, SignatureValue and the
> DigestValue elements).
>
> When I validated the signature, the cryptographic signature itself was
> invalid, so I'm wondering if the SignedInfo contents have been  
> modified
> since the signature was generated. How did you generate this signature
> and why the inconsistency in namespaces?
>
> --Sean
>
> Ian Hummel wrote:
>> Hi there,
>>
>>
>> I am having some issues validating the signature on the following XML
>> file using both xmlsec-1.4.0 and xmlsec-1.4.1:
>>
>> <saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion"
>> AssertionID="urn:uuid:44FFFE2742C5DBB6311203096718823"
>> IssueInstant="2008-02-15T17:31:58.817Z"
>> Issuer="https://rh150.sohosmart.net/TokenService/services/Trust"
>> MajorVersion="1" MinorVersion="1">
>>    <saml:Conditions NotBefore="2008-02-15T17:31:58.817Z"
>> NotOnOrAfter="2008-02-22T17:31:58.817Z"/>
>>    <saml:AttributeStatement>
>>        <saml:Subject>
>>            <saml:SubjectConfirmation>
>>
>> <saml:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer</ 
>> saml:ConfirmationMethod>
>>
>>            </saml:SubjectConfirmation>
>>        </saml:Subject>
>>        <saml:Attribute AttributeName="privatepersonalidentifier"
>> AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims 
>> ">
>>
>> <saml:AttributeValue>NBkhyg1zm4UTqbpjkQg7LhXFlS8EpMpDtnphO1SvASA=</ 
>> saml:AttributeValue>
>>
>>        </saml:Attribute>
>>        <saml:Attribute AttributeName="emailaddress"
>> AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims 
>> ">
>>            <saml:AttributeValue>j@j.com</saml:AttributeValue>
>>        </saml:Attribute>
>>    </saml:AttributeStatement>
>>    <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
>>        <ds:SignedInfo>
>>            <CanonicalizationMethod
>> xmlns="http://www.w3.org/2000/09/xmldsig#"
>> Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
>>            <ds:SignatureMethod
>> Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
>>            <ds:Reference URI="#urn:uuid:44FFFE2742C5DBB6311203096718823 
>> ">
>>                <ds:Transforms>
>>                    <ds:Transform
>> Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
>>                    <ds:Transform
>> Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
>>                </ds:Transforms>
>>                <ds:DigestMethod
>> Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
>>                <DigestValue
>> xmlns="http://www.w3.org/2000/09/ 
>> xmldsig#">IjaxSnB43LryrBM25gCeFFEoaMc=</DigestValue>
>>
>>            </ds:Reference>
>>        </ds:SignedInfo>
>>        <SignatureValue
>> xmlns="http://www.w3.org/2000/09/xmldsig#">BQadvf3/ 
>> JZquVfzTGVa0OSGkmddVwMWdn30JEHTKYZvT26Goxg62iYg9xkB527dphU2bHBd2KICyo2cliivKsOxFqKpOPcIxgft 
>> /y+vv+RqE5cTn2BDsVZ6WfWWfiXHgEUAkzF+BUBoGG7mJ1Gs8ycZoIl/ 
>> 9pYgCzeUjSJXYNSU=</SignatureValue>
>>
>>        <ds:KeyInfo>
>>            <ds:KeyValue>
>>                <ds:RSAKeyValue>
>>                    <Modulus xmlns="http://www.w3.org/2000/09/ 
>> xmldsig#">
>> imhZHVDvtboiubhWbcNFyIDOamOaOVWIdD6QpDq8i/D3MltwgBTDorX+/ 
>> prqfj8RMWxbmYlmbuts
>>
>> q9ZBHlaCz8eKdXqZQJ3bUmqDtAXU6PnAM0J4UsW/ 
>> S1ikTEVgcpV6mGpjsEF8UojhcNOJkwMyDipk
>>
>> xmtcY+YknWkiJ5sl+LE=
>> </Modulus>
>>                    <Exponent
>> xmlns="http://www.w3.org/2000/09/xmldsig#">AQAB</Exponent>
>>                </ds:RSAKeyValue>
>>            </ds:KeyValue>
>>        </ds:KeyInfo>
>>    </ds:Signature>
>> </saml:Assertion>
>>
>>
>> The code I am using to verify the signature is the following:
>>
>>    public static boolean verifyXmlSignature(Document doc) {
>>        Element signatureElement = (Element)
>> doc.getElementsByTagNameNS(Constants.SignatureSpecNS,  
>> "Signature").item(0);
>>        logger.debug("signatureElement? " + signatureElement);
>>        XMLSignature signature;
>>        try {
>>            signature = new XMLSignature(signatureElement,
>> System.getProperty("java.io.tmpdir"));
>>        } catch (XMLSecurityException e) {
>>            logger.warn("error verifying digital signature", e);
>>            return false;
>>        }
>>
>>        SignedInfo signedInfo = signature.getSignedInfo();
>>        logger.info("signedInfo? " + signedInfo);
>>        signature.setFollowNestedManifests(true);
>>        KeyInfo ki = signature.getKeyInfo();
>>        logger.info("keyInfo is: " + ki);
>>        PublicKey pk;
>>        try {
>>            pk = signature.getKeyInfo().getPublicKey();
>>            logger.info("public key is: " + pk);
>>
>>        } catch (KeyResolverException e) {
>>            logger.warn("Signature did not contain public key data",  
>> e);
>>            return false;
>>        }
>>        try {
>>            System.out.println("KEY The XML signature in file "
>>                                         +
>> (signature.checkSignatureValue(pk)
>>                                            ? "valid (good)"
>>                                            : "invalid !!!!! (bad)"));
>>        } catch (XMLSignatureException e) {
>>            logger.warn("Signature was invalid", e);
>>            return false;
>>        }
>>        return true;
>>    }
>>
>>
>> it always says the signature is invalid... I wonder if I am even  
>> setting
>> everything up correctly?  I got most of the code above from the  
>> sample
>> files included in the 1.4.0 dist...
>>
>> Am I missing something fundamental?
>>
>>
>> Thank you for any insight!
>>
>> - ian.
>>
>


Re: Help validating XML signature

Posted by Sean Mullan <Se...@Sun.COM>.
The signature looks a bit odd. Some of the XML Signature elements are 
defined in the dsig namespace bound by a 'ds' prefix, while others don't 
have a 'ds' prefix (see CanonicalizationMethod, SignatureValue and the 
DigestValue elements).

When I validated the signature, the cryptographic signature itself was 
invalid, so I'm wondering if the SignedInfo contents have been modified 
since the signature was generated. How did you generate this signature 
and why the inconsistency in namespaces?

--Sean

Ian Hummel wrote:
> Hi there,
> 
> 
> I am having some issues validating the signature on the following XML 
> file using both xmlsec-1.4.0 and xmlsec-1.4.1:
> 
> <saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" 
> AssertionID="urn:uuid:44FFFE2742C5DBB6311203096718823" 
> IssueInstant="2008-02-15T17:31:58.817Z" 
> Issuer="https://rh150.sohosmart.net/TokenService/services/Trust" 
> MajorVersion="1" MinorVersion="1">
>     <saml:Conditions NotBefore="2008-02-15T17:31:58.817Z" 
> NotOnOrAfter="2008-02-22T17:31:58.817Z"/>
>     <saml:AttributeStatement>
>         <saml:Subject>
>             <saml:SubjectConfirmation>
>                 
> <saml:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer</saml:ConfirmationMethod> 
> 
>             </saml:SubjectConfirmation>
>         </saml:Subject>
>         <saml:Attribute AttributeName="privatepersonalidentifier" 
> AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims">
>             
> <saml:AttributeValue>NBkhyg1zm4UTqbpjkQg7LhXFlS8EpMpDtnphO1SvASA=</saml:AttributeValue> 
> 
>         </saml:Attribute>
>         <saml:Attribute AttributeName="emailaddress" 
> AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims">
>             <saml:AttributeValue>j@j.com</saml:AttributeValue>
>         </saml:Attribute>
>     </saml:AttributeStatement>
>     <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
>         <ds:SignedInfo>
>             <CanonicalizationMethod 
> xmlns="http://www.w3.org/2000/09/xmldsig#" 
> Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
>             <ds:SignatureMethod 
> Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
>             <ds:Reference URI="#urn:uuid:44FFFE2742C5DBB6311203096718823">
>                 <ds:Transforms>
>                     <ds:Transform 
> Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
>                     <ds:Transform 
> Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
>                 </ds:Transforms>
>                 <ds:DigestMethod 
> Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
>                 <DigestValue 
> xmlns="http://www.w3.org/2000/09/xmldsig#">IjaxSnB43LryrBM25gCeFFEoaMc=</DigestValue> 
> 
>             </ds:Reference>
>         </ds:SignedInfo>
>         <SignatureValue 
> xmlns="http://www.w3.org/2000/09/xmldsig#">BQadvf3/JZquVfzTGVa0OSGkmddVwMWdn30JEHTKYZvT26Goxg62iYg9xkB527dphU2bHBd2KICyo2cliivKsOxFqKpOPcIxgft/y+vv+RqE5cTn2BDsVZ6WfWWfiXHgEUAkzF+BUBoGG7mJ1Gs8ycZoIl/9pYgCzeUjSJXYNSU=</SignatureValue> 
> 
>         <ds:KeyInfo>
>             <ds:KeyValue>
>                 <ds:RSAKeyValue>
>                     <Modulus xmlns="http://www.w3.org/2000/09/xmldsig#">
> imhZHVDvtboiubhWbcNFyIDOamOaOVWIdD6QpDq8i/D3MltwgBTDorX+/prqfj8RMWxbmYlmbuts 
> 
> q9ZBHlaCz8eKdXqZQJ3bUmqDtAXU6PnAM0J4UsW/S1ikTEVgcpV6mGpjsEF8UojhcNOJkwMyDipk 
> 
> xmtcY+YknWkiJ5sl+LE=
> </Modulus>
>                     <Exponent 
> xmlns="http://www.w3.org/2000/09/xmldsig#">AQAB</Exponent>
>                 </ds:RSAKeyValue>
>             </ds:KeyValue>
>         </ds:KeyInfo>
>     </ds:Signature>
> </saml:Assertion>
> 
> 
> The code I am using to verify the signature is the following:
> 
>     public static boolean verifyXmlSignature(Document doc) {
>         Element signatureElement = (Element) 
> doc.getElementsByTagNameNS(Constants.SignatureSpecNS, "Signature").item(0);
>         logger.debug("signatureElement? " + signatureElement);
>         XMLSignature signature;
>         try {
>             signature = new XMLSignature(signatureElement, 
> System.getProperty("java.io.tmpdir"));
>         } catch (XMLSecurityException e) {
>             logger.warn("error verifying digital signature", e);
>             return false;
>         }
> 
>         SignedInfo signedInfo = signature.getSignedInfo();
>         logger.info("signedInfo? " + signedInfo);
>         signature.setFollowNestedManifests(true);
>         KeyInfo ki = signature.getKeyInfo();
>         logger.info("keyInfo is: " + ki);
>         PublicKey pk;
>         try {
>             pk = signature.getKeyInfo().getPublicKey();
>             logger.info("public key is: " + pk);
> 
>         } catch (KeyResolverException e) {
>             logger.warn("Signature did not contain public key data", e);
>             return false;
>         }
>         try {
>             System.out.println("KEY The XML signature in file "
>                                          + 
> (signature.checkSignatureValue(pk)
>                                             ? "valid (good)"
>                                             : "invalid !!!!! (bad)"));
>         } catch (XMLSignatureException e) {
>             logger.warn("Signature was invalid", e);
>             return false;
>         }
>         return true;
>     }
> 
> 
> it always says the signature is invalid... I wonder if I am even setting 
> everything up correctly?  I got most of the code above from the sample 
> files included in the 1.4.0 dist...
> 
> Am I missing something fundamental?
> 
> 
> Thank you for any insight!
> 
> - ian.
>