vendredi 1 juillet 2016

Digital signature verification error for accessing webservice

I need to send a digital signature as one the parameters to an external webservice.

The steps to create as per documentation is :

  1. Create a DOM representation of the XML data
  2. Create a canonicalised representation of the DOM data. The canonicalised representation should follow the form described in http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments;
  3. Create the signature RSA encryption of the SHA1 digest of the canonicalised representation. The signature is encrypted using the Participant‟s private key;
  4. Encode the binary signature into a base64-encoded string
  5. Place the Signature string in the SOAP message ReqDigSig element;
  6. Store the XML data as it may be needed later to support Non-Repudiation of the submitted XML data.

I have used the following code:

private string SignXML4(X509Certificate2 Cert, string data)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.PreserveWhitespace = false;
        xmlDoc.LoadXml(data);

        XmlDsigC14NWithCommentsTransform t = new XmlDsigC14NWithCommentsTransform();
        t.LoadInput(xmlDoc);
        Stream s = (Stream)t.GetOutput(typeof(Stream));

        SHA1 sha1 = SHA1.Create();
        byte[] hash = sha1.ComputeHash(s);


        RSACryptoServiceProvider rsaKey =
        (RSACryptoServiceProvider)Cert.PrivateKey;
        RSAParameters rsaPrivateParams = rsaKey.ExportParameters(true);
        rsaKey.ImportParameters(rsaPrivateParams);
        byte[] signature =  rsaKey.Encrypt(hash, false);

        return Convert.ToBase64String(signature);
    }

But the response from the webservice says digital signature verification error.

is the code above as per the description in the documentation? How would i verify if the digital signaature is valid? is there any online tool?

Aucun commentaire:

Enregistrer un commentaire