首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用xmldsig (RobRichards\XMLSecLibs)签名的XML文件,但XSD验证失败

用xmldsig (RobRichards\XMLSecLibs)签名的XML文件,但XSD验证失败
EN

Stack Overflow用户
提问于 2019-05-13 08:31:52
回答 1查看 852关注 0票数 1

我安装了RobRichards\XMLSecLibs PHP库来签名我的XML文件。这些文件必须根据我们拥有的XSD文件生成。

代码语言:javascript
复制
...
<xs:element name="Lote">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Cabecera" type="LoteCabecera"/>
        <xs:element name="Registro" type="RegistroCJD" minOccurs="0" maxOccurs="unbounded"/>
        <xs:any namespace="http://www.w3.org/2000/09/xmldsig#" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
...

因此,在我的PHP代码中,我写了以下内容:

代码语言:javascript
复制
// Create a new Security object
$objDSig = new XMLSecurityDSig();
// Use the c14n exclusive canonicalization
$objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);
// Sign using SHA-256
$objDSig->addReference(
    $doc,
    XMLSecurityDSig::SHA256,
    array('http://www.w3.org/2000/09/xmldsig#enveloped-signature')
    );

// Create a new (private) Security key
$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type'=>'private'));
/*
 If key has a passphrase, set it using
 $objKey->passphrase = '<passphrase>';
 */
// Load the private key
$objKey->loadKey($key_path, TRUE);

// Sign the XML file
$objDSig->sign($objKey);

// Add the associated public key to the signature
$objDSig->add509Cert(file_get_contents($certificate_path));

// Append the signature to the XML
$objDSig->appendSignature($doc->documentElement);

生成的XML文件是:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Lote xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://coljuegos.gov.co/Online/data/v1.0" xsi:schemaLocation="http://coljuegos.gov.co/Online/data/v1.0 copia_lavoro.xsd">
    <ns1:Cabecera>
    </ns1:Cabecera>
    <ns1:Registro>
    </ns1:Registro>
    <ns1:Registro>
    </ns1:Registro>
    ...
    <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
        <ds:SignedInfo>
            <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
            <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
            <ds:Reference>
                <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"/>
                <ds:DigestValue>+LkHWYVOxJ48k/2TPizuFoHINBc=</ds:DigestValue>
            </ds:Reference>
        </ds:SignedInfo>
        <ds:SignatureValue>C/2buRFmei...66OpXg==</ds:SignatureValue>
        <ds:KeyInfo>
            <ds:X509Data>
                <ds:X509Certificate>MIID9TCCAt2...gR0Nf1</ds:X509Certificate>
            </ds:X509Data>
        </ds:KeyInfo>
    </ds:Signature>
</ns1:Lote>

当我试图验证这个文件时,我会得到以下错误:

代码语言:javascript
复制
XML error "Element '{http://www.w3.org/2000/09/xmldsig#}Signature': No matching global element declaration available, but demanded by the strict wildcard." [2] (Code 1845)

我怎么才能解决这个问题?我的代码中有什么东西可以更改,以使XML有效吗?因为,由于我从外部机构收到了这个XSD,它应该是不可触摸的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-16 14:35:43

我在另一个问题上找到了解决方案:XSD Signature issue

基本上,我的验证程序无法到达远程XSD,所以我必须解决的唯一方法是下载XSD文件的副本并修改XSD文件,以便在本地目录中查找模式定义,而不是在线查找它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56108472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档