19

I'm trying to sign an XML file using a x.509 certificate, I can use the private key to sign the document and then use the CheckSignature method (it has an overload that receives a certificate as parameter) to verify the signature.

The problem is that the user who validates the signature must have the certificate, my concern is, if the user has the certificate then he has access to the private key, and as I understand, this is private and should be available only to the user who signs.

What am I missing?

Thanks for your help.

skaffman
  • 398,947
  • 96
  • 818
  • 769
willvv
  • 8,439
  • 16
  • 66
  • 101

3 Answers3

20

In .NET, If you get your X509 cert from a .pfx file, like this:

 X509Certificate2 certificate = new X509Certificate2(certFile, pfxPassword);
 RSACryptoServiceProvider rsaCsp = (RSACryptoServiceProvider) certificate.PrivateKey;   

Then you can export the public key portion like so:

 rsaCsp.ToXmlString(false);

The "false" part says, only export the public piece, don't export the private piece. (doc for RSA.ToXmlString)

And then in the verifying application, use

 RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
 csp.FromXmlString(PublicKeyXml);
 bool isValid = VerifyXml(xmlDoc, rsa2);

And the VerifyXml calls CheckSignature(). It looks something like this:

private Boolean VerifyXml(XmlDocument Doc, RSA Key)
{
    // Create a new SignedXml object and pass it
    // the XML document class.
    var signedXml = new System.Security.Cryptography.Xml.SignedXml(Doc);

    // Find the "Signature" node and create a new XmlNodeList object.
    XmlNodeList nodeList = Doc.GetElementsByTagName("Signature");

    // Throw an exception if no signature was found.
    if (nodeList.Count <= 0)
    {
        throw new CryptographicException("Verification failed: No Signature was found in the document.");
    }

    // Though it is possible to have multiple signatures on 
    // an XML document, this app only supports one signature for
    // the entire XML document.  Throw an exception 
    // if more than one signature was found.
    if (nodeList.Count >= 2)
    {
        throw new CryptographicException("Verification failed: More that one signature was found for the document.");
    }

    // Load the first <signature> node.  
    signedXml.LoadXml((XmlElement)nodeList[0]);

    // Check the signature and return the result.
    return signedXml.CheckSignature(Key);
}
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • 2
    The code seems to be from http://msdn.microsoft.com/en-us/library/ms229950(v=vs.110).aspx. I think it's cool that you include the code here instead of just linking to it, though, but give credit where it's due. – Jan Aagaard Jul 09 '14 at 12:06
  • In this line of code: bool isValid = VerifyXml(xmlDoc, rsa2); Where is the ras2 coming from ? How is it related to the PCX file? – Sagi Jun 08 '22 at 11:34
5

Any certificate has a public and a private part. You only send around the public part. Just open any SSL enabled website in your browser, click on the padlock symbol and have a look at their certificate.

chris166
  • 4,769
  • 4
  • 24
  • 25
  • 1
    Thank you, that's exactly what I didn't have clear. Now I know that I have to use a certificate from the X509Store to get the "signer" certificate, and use a .cer file as the "verifier". – willvv Jul 28 '09 at 19:48
0

First off all you need to be sure that the certificate .pfx or .cer that you are using is intended for signing purpose.

You can check same in General Tab of a certificate

*.Proves your identity to a remote computer
*.Protects e-mail messages
*.Allows data to be signed with the current time
*.Allows data on disk to be encrypted
*.2.16.356.100.2
**Document Signing**

A Complete console application to digitally sign/verify XmlDocument in C# is written here.

Raj kumar
  • 1,275
  • 15
  • 20