.net डिजिटल एक्सएमएल हस्ताक्षर कैसे सत्यापित करें



vb.net soap (1)

वास्तव में यह एक ज्ञात और दस्तावेज मुद्दा है:

.NET 3.5 और .NET 4.0 के बीच कैनोनाइलाइज़ेशन कार्यान्वयन बदल गया है।

मुझे नहीं पता कि यह सभी एक्सएमएल हस्ताक्षरों पर काम करता है, लेकिन यह मेरे द्वारा किए गए परीक्षण से काम करता है।

अपने प्रोजेक्ट में निम्न C14N रूपांतरण कक्षा जोड़ें:

public class MyXmlDsigC14NTransform: XmlDsigC14NTransform {
  static XmlDocument _document;
  public static XmlDocument document {
    set {
      _document = value;
    }
  }

  public MyXmlDsigC14NTransform() {}

  public override Object GetOutput() {
    return base.GetOutput();
  }

  public override void LoadInnerXml(XmlNodeList nodeList) {
    base.LoadInnerXml(nodeList);
  }

  protected override XmlNodeList GetInnerXml() {
    XmlNodeList nodeList = base.GetInnerXml();
    return nodeList;
  }

  public XmlElement GetXml() {
    return base.GetXml();
  }

  public override void LoadInput(Object obj) {
    int n;
    bool fDefaultNS = true;

    XmlElement element = ((XmlDocument) obj).DocumentElement;

    if (element.Name.Contains("SignedInfo")) {
      XmlNodeList DigestValue = element.GetElementsByTagName("DigestValue", element.NamespaceURI);
      string strHash = DigestValue[0].InnerText;
      XmlNodeList nodeList = _document.GetElementsByTagName(element.Name);

      for (n = 0; n < nodeList.Count; n++) {
        XmlNodeList DigestValue2 = ((XmlElement) nodeList[n]).GetElementsByTagName("DigestValue", ((XmlElement) nodeList[n]).NamespaceURI);
        string strHash2 = DigestValue2[0].InnerText;
        if (strHash == strHash2) break;
      }

      XmlNode node = nodeList[n];

      while (node.ParentNode != null) {
        XmlAttributeCollection attrColl = node.ParentNode.Attributes;
        if (attrColl != null) {
          for (n = 0; n < attrColl.Count; n++) {
            XmlAttribute attr = attrColl[n];
            if (attr.Prefix == "xmlns") {
              element.SetAttribute(attr.Name, attr.Value);
            } else if (attr.Name == "xmlns") {
              if (fDefaultNS) {
                element.SetAttribute(attr.Name, attr.Value);
                fDefaultNS = false;
              }
            }
          }
        }

        node = node.ParentNode;
      }
    }

    base.LoadInput(obj);
  }
}

और क्रिप्टो कॉन्फिफ.एड एल्गोरिथ्म विधि का उपयोग करके क्लास को पंजीकृत करें जैसा कि नीचे दिखाया गया है:

CryptoConfig.AddAlgorithm(typeof(MyXmlDsigC14NTransform), "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"); 

var message = new XmlDocument();
message.PreserveWhitespace = true;
message.Load("XmlSig.xml");

MyXmlDsigC14NTransform.document = message; // The transform class needs the xml document

// Validate signature as normal.  

यह आपको एक XML हस्ताक्षर को ठीक से सत्यापित करने और अपनी समस्या को हल करने की अनुमति देनी चाहिए।

मैं यह सत्यापित करना चाहता हूं कि दस्तावेज़ को किसी भी तरह से संशोधित नहीं किया गया था।

जावा वेब सेवा से, मुझे एक डिजिटल एक्सएमएल हस्ताक्षर के साथ निम्नलिखित साबुन प्रतिक्रिया मिलती है:

<?xml version="1.0" encoding="UTF-8"?><Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">  
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" SOAP-ENV:mustUnderstand="1">
<wsse:BinarySecurityToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" wsu:Id="CertId-38670273">MIIHQzCCBSugAwIBAgIQY+wksDuKve+PKV1rHtR85TANBgkqhkiG9w0BAQsFADB9MQswCQYDVQQGEwJDSDEOMAwGA1UEChMFQWRtaW4xETAPBgNVBAsTCFNlc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gZfGTovX2a+6q0FKw5E9xqz4TyxmCj5P0ibnDvwOlcZB3S0xEx9yVjxZneGgtHzG8m4s7MEYJTYURwp3jDfIs6fej3MkSIuczZif9sk9CQBugWniX7JjI3hI5S4fUp4vvsjUCpRmoQvgpru78u4xgkHB5hUAcNZMDaOp3KyFiQfTqrg239cuIOCrPe2afD3LfbOEPEQrcVVbVSVdxmc6alfQI1fzKbUHt</wsse:BinarySecurityToken><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/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="#id-32516734">
<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>Rx5L4j8kF5RVYnC+spUCdvhh5N0=</ds:DigestValue>
</ds:Reference>
<ds:Reference URI="#id-7716709">
<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>fFr2j5DoKTgpEvX1Se7gTC55bWM=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>
jG1BaGgNbbk9JCc3R6JsJKY56p++f0+8RM2aL6TGOXS34NAGv48Sp3iAHEAuUt9+JV6w3VDAcFct
no3nCEISa0P4dVWTlPQJue3GVTWnnlcXao95tjukh9o8lIU7vZGgYHBUZLU+jgS6ZcaUlNW4KFUl
AdrPxR5DmJcFyGEtRY2yclqYhnJdnUc+ZBu5eWbRZgbJzR4MgtGsEQcgtftFe2i0CvRbOSe4mt3T
JQzbGY81ssFCnB44vitgjhVLfPd/08amSa5Xn8KRptbNatp2uq1iGXAifJLVup8T0yS0RzaqhCJg
CaHRPFVKFN3WaJcqPZex75KBwSZMZaaJDZW7lQ==
</ds:SignatureValue>
<ds:KeyInfo Id="KeyId-16708261">
<wsse:SecurityTokenReference xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="STRId-32258670"><wsse:Reference URI="#CertId-38670273" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/></wsse:SecurityTokenReference>
</ds:KeyInfo>
</ds:Signature>
<wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-32516734"><wsu:Created>2017-03-31T09:04:40.352Z</wsu:Created><wsu:Expires>2017-03-31T09:09:40.352Z</wsu:Expires></wsu:Timestamp></wsse:Security><wsa:MessageID xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" SOAP-ENV:mustUnderstand="0">uuid:12b93a00-15f1-11e7-af6b-f16d80f418ae</wsa:MessageID><wsa:To xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" SOAP-ENV:mustUnderstand="0">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To></SOAP-ENV:Header>


<SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-7716709">
...
 </SOAP-ENV:Body></Envelope>

मुझे MSDN पर XML फ़ाइल को सत्यापित करने के लिए एक उपयोगी उदाहरण मिला है:

  ' Verify the signature of an XML file and return the result.
Function VerifyXmlFile(ByVal Name As String) As [Boolean]
    ' Create a new XML document.
    Dim xmlDocument As New XmlDocument()

    ' Format using white spaces.
    xmlDocument.PreserveWhitespace = True

    ' Load the passed XML file into the document. 
    xmlDocument.Load(Name)

    ' Create a new SignedXml object and pass it
    ' the XML document class.
    Dim signedXml As New SignedXml(xmlDocument)

    ' Find the "Signature" node and create a new
    ' XmlNodeList object.
     Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("Signature", "http://www.w3.org/2000/09/xmldsig#")

    ' Load the signature node.
    signedXml.LoadXml(CType(nodeList(0), XmlElement))

    ' Check the signature and return the result.
    Return signedXml.CheckSignature()

End Function

दुर्भाग्य से, सत्यापन हमेशा झूठी वापसी करता है। मुझे समझ में आ रहा है कि यह काम क्यों नहीं करता।

हेनक होल्टरमैन और साइमन मोरीयर के प्रस्तावों के मुताबिक 1 अपडेट करें :

 Function VerifyXmlFile(ByVal Name As String) As Boolean

    ' <SOAP-ENV:Envelope ... > ... </SOAP-ENV:Envelope>
    Dim xDoc = XDocument.Load(Name)

    ' <wsse:binarySecurityToken ... > ... </wsse:binarySecurityToken>
    Dim xBinarySecurityToken = xDoc.Root.Descendants().Skip(2).FirstOrDefault

    ' <SOAP-ENV:Body ... > ... </SOAP-ENV:Body>
    Dim xBody = xDoc.Root.Elements().Skip(1).FirstOrDefault
    Dim signedXml = New SignedXml(ToXmlElement(xBody))

    Dim xmlDocument As New XmlDocument()
    xmlDocument.PreserveWhitespace = True
    xmlDocument.Load(Name)

    Dim nodeList = xmlDocument.GetElementsByTagName("Signature", "http://www.w3.org/2000/09/xmldsig#")
    signedXml.LoadXml(CType(nodeList(0), XmlElement))

    Dim byteCert = Encoding.UTF8.GetBytes(xBinarySecurityToken.Value)
    Dim cert = New X509Certificates.X509Certificate2(byteCert)

    Return signedXml.CheckSignature(cert, True)

End Function

Public Shared Function ToXmlElement(xElement As XElement) As XmlElement
    Dim xmlDoc = New XmlDocument() With {.PreserveWhitespace = True}
    xmlDoc.Load(xElement.CreateReader())
    Return xmlDoc.DocumentElement
End Function

क्रिप्टोग्राफिक एक्सपैशन में " फॉरवर्ड रेफरेंस एंटैंट " संदेश के साथ फ़ंक्शन को कॉल करने के लिए CheckSignature ()

@ सिमोन मूरियर और @ लैक्स 10 9 8 के प्रस्तावों के मुताबिक 2 अपडेट करें और समाधान

Private Function VerifyXmlfile(Name As String) As Boolean
    CryptoConfig.AddAlgorithm(GetType(MyXmlDsigC14NTransform), "http://www.w3.org/TR/2001/REC-xml-c14n-20010315")

    Dim xmlDocument As New XmlDocument()
    xmlDocument.PreserveWhitespace = True
    xmlDocument.Load(Name)
    MyXmlDsigC14NTransform.document = xmlDocument

    Dim soapBody As XmlElement = xmlDocument.GetElementsByTagName("SOAP-ENV:Body")(0)
    Dim securityToken = xmlDocument.GetElementsByTagName("SOAP-ENV:Header")(0).FirstChild.NextSibling.FirstChild.NextSibling.InnerText

    Dim signedXml = New SignedXmlWithId(soapBody)

    Dim nodeList = xmlDocument.GetElementsByTagName("Signature", "http://www.w3.org/2000/09/xmldsig#")
    signedXml.LoadXml(CType(nodeList(0), XmlElement))

    Dim byteCert = Convert.FromBase64String(securityToken)
    Dim cert = New X509Certificates.X509Certificate2(byteCert)

    Return signedXml.CheckSignature(cert, True)

End Function

कुत्ते के कान से चोरी करने वाला सहायक वर्ग

Public Class SignedXmlWithId
Inherits SignedXml

Public Sub New(xml As XmlDocument)
    MyBase.New(xml)
End Sub

Public Sub New(xmlElement As XmlElement)
    MyBase.New(xmlElement)
End Sub

Public Overrides Function GetIdElement(doc As XmlDocument, id As String) As XmlElement
    ' check to see if it's a standard ID reference
    Dim idElem As XmlElement = MyBase.GetIdElement(doc, id)

    If idElem Is Nothing Then
        Dim nsManager As New XmlNamespaceManager(doc.NameTable)
        nsManager.AddNamespace("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")

        idElem = TryCast(doc.SelectSingleNode((Convert.ToString("//*[@wsu:Id=""") & id) + """]", nsManager), XmlElement)
    End If

    Return idElem
End Function

End Class

@ सिमोन मूरियर और @ कार्लोस लोपेज-एमएसएफटी के लिए क्रेडिट





xml-signature