lundi 29 juin 2015

How to validate the SignedXML for the following code?

I am working on one project where I need to check if the XML provided is valid or not. For this purpose, I am using code to verify the XML using the signedXmlclass in c#.

Please find the below code for the verify() Method:

    public bool Verify(XmlDocument signedDocument)
{
    // create a signed xml object from xml 
    SignedXml signedXml = new SignedXml(signedDocument);
    // get the signature node and load the signature tag
    XmlNodeList nodeList = signedDocument.GetElementsByTagName("Signature");
    signedXml.LoadXml((XmlElement)nodeList[0]);

    // define and create the encryption key
    CspParameters CSPParam = new CspParameters();
    CSPParam.Flags = CspProviderFlags.UseMachineKeyStore;
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(CSPParam);
    rsa.FromXmlString(@"<RSAKeyValue><Modulus>03COUbx8JI6jkkmxrQ3N//67tgMt+ak1SKSXbGO3+4vVGFGhyGICJS8C+W6ON6dUdcm2/uxC5q4wyPRyFWMf8v8oslGOWJdADPnP8rvcy8PL3Nf67f8fMnTyHoEoZTfZjKuEoyhmPi6rfAX4QQA+OYhk4Qb1FJyOB3fpZ1QiTxX2k=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>");

    // load encryption key to the xml
    KeyInfo keyInfo = new KeyInfo();
    keyInfo.AddClause(new RSAKeyValue(rsa));
    signedXml.KeyInfo=keyInfo;
    // check if the signature provided in signature tag is valid with the key info
    bool b = signedXml.CheckSignature();
    return b;
}

and the xml data provided to the method is:

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY" xmlns:soap="http://ift.tt/sVJIaE">

<Signature xmlns="http://ift.tt/uq6naF" id="MySignature">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://ift.tt/11kTCUR" />
      <SignatureMethod Algorithm="http://ift.tt/zf1Wx4" />
      <Reference URI="#_ea559faf-417b-407f-bdc2-bccc76dab76c">
        <Transforms>
          <Transform Algorithm="http://ift.tt/A1C4L2" />
          <Transform Algorithm="http://ift.tt/11kTCUR">
          </Transform>
        </Transforms>
        <DigestMethod Algorithm="http://ift.tt/yuvO4a" />
        <DigestValue>fvQx+J90ZGKhwj8Mfhg6v/esOtI=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>fvQx+J90ZGKhwj8Mfhg6v/esOtI=</SignatureValue>
  <KeyInfo type="http://ift.tt/1GUYsMV">    
   <RSAKeyValue><Modulus>03COUbx8JI6jkLrQ3N//67tgMt+ak1SKSXbGO3+4vVGFGhyGICJS8C+W6ON6dUdcm2/uxC5q4wyPRyFWMf8v8oslGOWJdADPnP8rvcy8PL3Nf67f8fMnTyHoEoZTfZjKuEoyhmPi6rfAX4QQA+OYhk4Qb1FJyOB3fpZ1QiTxX2k=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
  </KeyInfo >   
</Signature>
</soap:Envelope>

I am not able to trace out which tag value in xml I am setting wrong. The result of signedXml.CheckSignature() is always getting false.

Can anyone help me to sort out this problem?

Thanks in advance. Please let me know if any more information is needed.

Aucun commentaire:

Enregistrer un commentaire