locked
How to sign/validate with X509 certificate using RSA SHA2 algorithm with the System.Security.Cryptography.Xml.SignedXml class without exporting/importing the key? RRS feed

  • Question

  • I need to support RSA SHA2 algorithm for signing and validating messages and SAML tokens through X509 certificate.

    When the certificate was issued it was destined for RSA SHA1.

    Now anytime I try to use the RSA SHA2 algorithm it assumes RSA SHA1 and throws exception.

    I have found a workaround this, but it requires that the private key to be exportable which is a big security hole waiting for disaster.

    Currently what I have is the following registering of algorithm and it's handler:

    //registers RSA SHA256 provider
    CryptoConfig.AddAlgorithm(typeof(RSAPKCS1SHA256SignatureDescription), SecurityAlgorithms.RsaSha256Signature);
    

    And the code for signing:

                    

    using (RSACryptoServiceProvider rsa = cert.PrivateKey as RSACryptoServiceProvider) { byte[] privateKeyBlob = rsa.ExportCspBlob(true); using (RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider()) { rsa2.ImportCspBlob(privateKeyBlob); //this is the piece of code which activates the above mapping

    //but it requires for us to make the private keys exportable which is unacceptable xmldsig.SigningKey = rsa2; xmldsig.ComputeSignature(); } }

    Can someone help by giving an alternative which doesn't require certificate private key export, we're using .NET 4.0.

    Thank You

    Visar

      








    Saturday, October 5, 2013 10:22 AM

All replies

  • Thank you for the reply.

    But I don't understand in what respect this answers my question?!

    Saturday, October 5, 2013 8:04 PM
  • Hi Visar,

    According to your description, you want to use RSA without key pair. I think it is impossible, because the key pair are required for the encryption. The public key is the key which is used to encrypt data while the private key is the key which allows one to decrypt data. See http://security.stackexchange.com/questions/9260/sha-rsa-and-the-relation-between-them for more information.

    In order to solve your issue, I pick up a workaround. Please refer to reference http://stackoverflow.com/questions/4715463/is-it-possible-to-sign-an-xml-document-without-having-to-use-keycontainername?rq=1.

    Hope useful to you.

    Best Regards,

    Hetro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 8, 2013 4:52 AM
  • Hi Hetro, thanks for the reply.

    I reviewed the links that you provided, they also recommend exporting the private key, which is impossible if you have not marked the private key exportable when installing it in the store. (which is highly unlikely and absolutely not recommended that anyone would want to mark the private key exportable)

    I just want to create an instance of the RSACryptoServiceProvider which uses SHA2 algorithm instead of SHA1, without resorting to exporting/importing the private key of the certificate.


    Thanks

    Visar

    Tuesday, October 8, 2013 7:02 AM
  • Hi Visar,

    I find a workaround about using SHA256 with RSACryptoServiceProvider. Please refer to http://connect.microsoft.com/VisualStudio/feedback/details/584754/using-sha256-with-rsacryptoserviceprovider-in-a-webapplication to see the details.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 9, 2013 8:01 AM
  • Thanks Hetro, this is close but no cigar.

    (If you check the original question)

    Currently we are using the Microsoft .NET implementation of XML-DSIG SignedXml class, and we are not in control how the generation of digests and signature is performed. We only pass to the SignedXml instance either KeyedHashAlgorithm or AssymetricKey and the rest is being performed by the underlying code. 

    But we can use this workaround if we finally decide to ditch .NET XML-DSIG SignedXml implementation and write our own implementation from scratch - which it seems to me is what we might have to ultimately do.


    Wednesday, October 9, 2013 10:15 AM
  • Hetro, even the workaround you provided doesn't work-around.
    Friday, October 11, 2013 5:02 PM