drzk21632 2019-05-13 08:31
浏览 225

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

I installed the RobRichards\XMLSecLibs PHP library to sign my XML files. These files have to be generated according to a XSD file we have

<xs:element name="Lote">
        <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"/>

so, in my PHP code I wrote this:

// Create a new Security object
$objDSig = new XMLSecurityDSig();
// Use the c14n exclusive canonicalization
// Sign using SHA-256

// 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

// Add the associated public key to the signature

// Append the signature to the XML

and the resulting XML file is:

<?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">
    <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
            <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:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
                    <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
                <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>

and when I try to validate this file I get the following error:

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)

How can I fix this? Is there something I can change in my code to make this XML valid? Because, since I received this XSD from an external authority, it should be untouchable

  • 写回答

1条回答 默认 最新

  • dongwo8827523 2019-05-16 14:35

    I found the solution on another question: XSD Signature issue

    Basically, my validator are not able to reach the remote XSD so the only way I have to solve this is to download a copy of that XSD file and modify the XSD file in order to look for the schema definition in a local directory, instead of look for it online.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)
  • ¥15 c# 直接使用c++ 类库文件
  • ¥15 一个主机电脑有两个显示器,当前有两个软件主体是网页html打包的exe程序,如何通过cmd命令bat同时打开软件一个在主屏幕显示,另外一个软件在2副屏幕上显示