【问题标题】:XML schema validation 'Security' issueXML 模式验证“安全”问题
【发布时间】:2016-03-22 18:18:13
【问题描述】:

我有以下 xsd:

<xsd:schema
    targetNamespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
    elementFormDefault="qualified" attributeFormDefault="unqualified"
    blockDefault="#all" version="0.2">
    <xsd:import namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"  schemaLocation="../MSG/IRS-WSTimeStampElementMessage.xsd" />
    <xsd:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="../MSG/IRS-WSSignatureElementMessage.xsd"/>
    <xsd:complexType name="SecurityHeaderType">
        <xsd:annotation>
            <xsd:documentation>This complexType defines header block to use for
                security-relevant data directed at a specific SOAP actor.
            </xsd:documentation>
        </xsd:annotation>
        <xsd:sequence>
            <xsd:element ref="ds:Signature" minOccurs="0"/>
            <xsd:element ref="wsu:Timestamp" minOccurs="0"/>            
        </xsd:sequence>
        <xsd:anyAttribute namespace="##other" processContents="lax" />
    </xsd:complexType>
    <xsd:element name="Security" type="wsse:SecurityHeaderType">
        <xsd:annotation>
            <xsd:documentation>This element defines the wsse:Security SOAP header
                element per Section 4.</xsd:documentation>
        </xsd:annotation>
    </xsd:element>
</xsd:schema>

这是我要针对上述架构验证的 XML:

<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <u:Timestamp u:Id="TS-E68EBBF1696C5DD4AA143353323390073">
                <u:Created>2016-03-22T12:42:44.170Z</u:Created>
                <u:Expires>2016-03-22T12:52:44.170Z</u:Expires>
            </u:Timestamp>
            <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#WithComments" />
                    <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
                    <ds:Reference URI="#id-1">
                        <ds:Transforms>
                            <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>2gAU0kJV40nvR+Og=</ds:DigestValue>
                    </ds:Reference>
                    <ds:Reference URI="#id-2">
                        <ds:Transforms>
                            <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>pISNeszVQ59HKCRbQ=</ds:DigestValue>
                    </ds:Reference>
                    <ds:Reference URI="#TS-E68EBBF1696C5DD4AA143353323390073">
                        <ds:Transforms>
                            <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>mh7+cJJPRtrrn/s4N15AE=</ds:DigestValue>
                    </ds:Reference>
                </ds:SignedInfo>
                <ds:SignatureValue>fhpY2IlKEdwBFWqNxbVEw7p+ojhw54+op+g==</ds:SignatureValue>
                <ds:KeyInfo>
                    <ds:X509Data>fshPnGE6H36KNqWMZqTf+X0oBls3dLz7TY=</ds:X509Certificate>
                    </ds:X509Data>
                </ds:KeyInfo>
            </ds:Signature>
</wsse:Security>

我不明白为什么在验证过程中出现以下错误:

ERROR: The element 'Security' in namespace 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' has invalid child element 'Signature' in namespace 'http://www.w3.org/2000/09/xmldsig#'.

它看起来像是在命名空间中定义的元素,但验证器似乎没有注意到它,或者出于某种原因只是忽略了这个事实。

【问题讨论】:

    标签: xml validation xsd irs


    【解决方案1】:

    它们以错误的顺序出现.. 序列应该按照声明的顺序出现.. 您为签名设置的最少出现次数为 0,这就是为什么它没有给您带来时间戳为第一的问题。

    如果您不想强制执行子元素的出现顺序,则使用 xsd::all 但它有自己的问题/规则Difference between <xsd:all> and <xsd:sequence> in schema definition?

    【讨论】:

    • 非常感谢!序列是问题!感谢您的帮助!
    • 乐于助人。
    【解决方案2】:

    扩展@Rob 所说的内容。

    当我创建Signature 元素时,在使用SignedXml 对象的ComputeSignature() 方法后,我使用signedXml.GetXml() 方法将XML 输出到XmlElement 对象。用它来保存数字签名。我创建了一个XmlNode 对象来保存Security 元素的引用,然后使用InsertBefore 方法在Security 元素的FirstChild 之前添加数字签名。

    这样做会将Security 元素的子元素按适当的顺序排列。

    // Compute the signature.
    xSigned.ComputeSignature();
    
    // Get the Xml representation of the signature and save it to an XmlElement object.
    XmlElement xmlDigitalSignature = xSigned.GetXml();
    
    // Replace the KeyInfo element of the DigitalSignature with the appropriate KeyInfo information.
    xmlDigitalSignature = ReplaceKeyInfo(xmlDigitalSignature);
    
    // Create a reference to the Security Element under the SOAP Header.
    XmlNode securityNode = xdoc.DocumentElement.SelectSingleNode("//soapenv:Header/wsse:Security", XmlManager(xdoc));
    
    // Add the element to the XmlDocument by moving the Signature Element to be the first child under the Security Element.
    securityNode.InsertBefore(xdoc.ImportNode(xmlDigitalSignature, true), securityNode.FirstChild);
    

    【讨论】:

    • 谢谢,伙计!已经这样做了,但仍然无济于事:仍然出现臭名昭著的错误。再次阅读他们的指导并得出结论:应该更恰当地称呼它:误导:(
    • @fatherOfWine 您提交的内容是否已生效?
    • 是的,我做到了。我让你更新了你的问题。你见过吗?
    • 谢谢,我现在明白了。不幸的是,对我来说,我相信我在 Form1094C/1095C 文档中拥有符合其架构的所有正确和适当的字段。
    • 很抱歉听到这个消息。我给你留下了另一个更新。简要说明我的解决方案并提到我为扭转局面所做的其他更改。
    猜你喜欢
    • 2018-05-09
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 2012-06-16
    • 1970-01-01
    相关资源
    最近更新 更多