【问题标题】:How do I put a Type attribute in the Reference tag when signing a xml file?签署 xml 文件时,如何在 Reference 标记中添加 Type 属性?
【发布时间】:2015-08-05 19:41:05
【问题描述】:

我使用following code 来签署一个xml文件:

public void firmar(String rutaAlXml, String rutaAlXmlFirmado, PrivateKey key, Provider p, Certificate[] chain, PublicKey llavePublica) 
{    
    File fXmlFile = new File(rutaAlXml);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);    
    DOMSignContext dsc = new DOMSignContext(key, doc.getDocumentElement());

    XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
    javax.xml.crypto.dsig.Reference ref = fac.newReference(
        "", 
        fac.newDigestMethod(DigestMethod.SHA1, null), 
        Collections.singletonList(
        fac.newTransform(Transform.ENVELOPED, 
        (TransformParameterSpec) null)), 
        null, null);

    SignedInfo si = fac.newSignedInfo(
                    fac.newCanonicalizationMethod(
                    CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, 
                    (C14NMethodParameterSpec) null), 
                    fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), 
                    Collections.singletonList(ref));

    KeyInfoFactory kif = fac.getKeyInfoFactory();
    KeyValue kv = kif.newKeyValue(llavePublica);
    KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));
    XMLSignature signature = fac.newXMLSignature(si, ki);

    signature.sign(dsc);    
    OutputStream os = new FileOutputStream(rutaAlXmlFirmado);

    TransformerFactory tf = TransformerFactory.newInstance();
    javax.xml.transform.Transformer trans = tf.newTransformer();
    trans.transform(new DOMSource(doc), new StreamResult(os));
}

生成的 xml 文件是有效的,如下所示:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><root>
<nombre>cesar</nombre>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#" Id="EMPRESA"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"/><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-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>digest goes here</DigestValue></Reference></SignedInfo><SignatureValue>signature goes here</SignatureValue><KeyInfo><X509Data><X509SubjectName>subject's goes here</X509SubjectName><X509Certificate>certificate goes here</X509Certificate></X509Data></KeyInfo></Signature></root>

当我尝试使用 Xades4j 验证我的 xml 文件时,我的问题发生了。 I'm using the code in this link:

XadesVerificationProfile p = new XadesVerificationProfile(certValidator);
XadesVerifier verifier = p.newVerifier();
XAdESVerificationResult r = null;

SignatureSpecificVerificationOptions ssvo = 
new SignatureSpecificVerificationOptions();
ssvo.useDataForAnonymousReference(xmlFile);
Element sigElem = (Element) nl.item(0); //Which contains the complete signature segment from the xml       
r = verifier.verify(sigElem, ssvo); // this is the problematic line

当我尝试验证签名时,出现以下异常:

Exception in thread "main" xades4j.verification.QualifyingPropertiesIncorporationException: SignedProperties reference not found
    at xades4j.verification.SignatureUtils.processReferences(SignatureUtils.java:230)
    at xades4j.verification.XadesVerifierImpl.verify(XadesVerifierImpl.java:132)
    at myCode.main(myCode.java:11)
Java Result: 1

read here这是因为Reference标签中没有Type属性造成的。

我在 Internet 上进行了搜索,但找不到如何在 Reference 标记中放置有效的 Type 属性。如何执行此操作来验证我的 xml 文件?

【问题讨论】:

    标签: java xml xades4j


    【解决方案1】:

    您的代码会生成一个简单的 XML-DSIG 签名。 Xades4j 用于高级 XML 签名 (XAdES),无法验证简单的 XML-DSIG。

    您提到的问题是指除了您的数据对象引用之外,还应在 XAdES 上添加的特定 Reference 元素。此参考涵盖了包含签名属性的签名的特定部分。

    不过,Type 实际上是您传递给newReference call 的空参数之一。

    【讨论】:

      猜你喜欢
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      • 2022-09-24
      • 1970-01-01
      相关资源
      最近更新 更多