2014-12-26 56 views
0

请参阅下面的xml。我一直在问到数字签名记的文档片段:当完成如何在Java中对XML文档片段进行数字签名

<saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="XYZAssertion" IssueInstant="2014-12-09T18:56:16.636Z" Version="2.0"> 

,标签目前低于指出:

<Reference URI=""> 

应该变成:

<Reference URI="#XYZAssertion"> 

我使用Apache Santuario并找到许多示例来完整地签署xml文档,但没有一个用于碎片并使用Java设置Reference URI。鉴于这种环境,我如何签名片段?

这里的全部签署文件的基本代码:

// Instantiate the document to be signed 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
dbf.setNamespaceAware(true); 
Document doc = dbf.newDocumentBuilder().parse(new FileInputStream(fileNameIn)); 
DOMSignContext dsc = new DOMSignContext(privateKey, doc.getDocumentElement()); 
XMLSignature signature = fac.newXMLSignature(si, ki); 

// Marshal, generate (and sign) the enveloped signature 
signature.sign(dsc); 

XML:

<?xml version="1.0" encoding="UTF-8"?> 
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ID="XYZResponse" IssueInstant="2014-12-26T11:40:12.901-06:00" Version="2.0"> 
    <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">ComEdRRTPAssertion</saml:Issuer> 
    <samlp:Status> 
     <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" /> 
    </samlp:Status> 
    <saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="XYZAssertion" IssueInstant="2014-12-09T18:56:16.636Z" Version="2.0"> 
     <saml:Issuer>ComEdRRTP</saml:Issuer> 
     <saml:Subject> 
     <saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">user's RRTPID</saml:NameID> 
     <saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"> 
      <saml:SubjectConfirmationData NotOnOrAfter="2014-12-26T17:40:12.901-06:00" Recipient="https://test.amplifinity.net/ee/sso/HandleSamlLoginResponse" /> 
     </saml:SubjectConfirmation> 
     </saml:Subject> 
     <saml:Conditions NotBefore="2014-12-26T11:40:12.901-06:00" NotOnOrAfter="2014-12-26T17:40:12.901-06:00"> 
     <saml:AudienceRestriction> 
      <saml:Audience>sso:sp:amplifinity</saml:Audience> 
     </saml:AudienceRestriction> 
     </saml:Conditions> 
     <saml:AuthnStatement AuthnInstant="2014-12-26T11:40:12.901-06:00" SessionIndex="1"> 
     <saml:AuthnContext> 
      <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified</saml:AuthnContextClassRef> 
     </saml:AuthnContext> 
     </saml:AuthnStatement> 
     <saml:AttributeStatement> 
     </saml:AttributeStatement> 
     <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> 
     <SignedInfo> 
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" /> 
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#dsa-sha1" /> 
      <Reference URI=""> 
       <Transforms> 
        <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /> 
       </Transforms> 
       <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /> 
       <DigestValue>Xq7+w0EUWGyM1dsJqKsIlV1hPO0=</DigestValue> 
      </Reference> 
     </SignedInfo> 
     <SignatureValue>VNPKl2vfj62PLCgcDxvGHL1R8noreaeOuHK0cKcTOOsNJ2SZ9q9n9A==</SignatureValue> 
     <KeyInfo> 
      <KeyValue> 
       <DSAKeyValue> 
        <P>...</P> 
        <Q>...</Q> 
        <G>.../G> 
        <Y>...</Y> 
       </DSAKeyValue> 
      </KeyValue> 
     </KeyInfo> 
     </Signature> 
    </saml:Assertion> 
</samlp:Response> 
+0

你不是说你是如何创建SignedInfo(si)的。要更改引用中的URI,需要使用fac.newReference(“#XYZAssertion”,DigestMethod dm)',然后使用'fac.newSignedInfo(CanonicalizationMethod cm,SignatureMethod sm,List references)'。 – Moez

回答

0

的DOMSignContext需要一个XML元素和它的孩子们那么的XMLSignature的迹象了。因此,不要使用doc.getDocumentElement(),而只需用您选择的XML元素替换它。该元素及其子元素将被签名。

请注意,我没有亲自使用API​​,但这正是文档所指出的。你试过了吗?

+0

是的,我尝试过。我通过步行DOM找到元素,然后将它传递给签名:'DOMSignContext dsc = new DOMSignContext \t(privateKey,foundEle);'但是结果签名仍然显示:''。 – JamesJ

相关问题