Subject: SignSHA256 is not working
Date: 2015-06-08 14:23:40
From: Per Härdig
Source: signsha256-not-working
----------------------------------------------------------------------

I have used the example code from the SignandvalidateSHA256_2012 but is getting the error 'Unable to generate xml signature' when signing my assertion. I'm using .net 4.5

Added the code to global.asax.cs

protected void Application_Start(object sender, EventArgs e)
{
    //Enable SHA-256 XML signature support.
        CryptoConfig.AddAlgorithm(
         typeof(RSAPKCS1SHA256SignatureDescription),
         "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
}

and then

CryptoConfig.AddAlgorithm(typeof(RSAPKCS1SHA256SignatureDescription), "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
String CertificatePath;
var samlResponse = new ComponentPro.Saml2.Response();
CertificatePath = "Certificate";
CertificatePath = (CertificatePath.Substring(1, 2) == @":\") ? CertificatePath : Server.MapPath(".") + "\\" + CertificatePath;
X509Certificate2 signingCert = new X509Certificate2(CertificatePath + "\\per.pfx", "password");
var assertion = CreateAssertion();
assertion.Sign(signingCert, "http://www.w3.org/2001/04/xmlenc#sha256", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");

Signing SHA1 as below works fine
assertion.Sign(signingCert);

Error details:

ComponentPro.Saml.SamlException was unhandled by user code
  HResult=-2146233088
  Message=Unable to generate XML signature.
  Source=ComponentPro.Saml
  StackTrace:
       at ComponentPro.Saml.SamlUtil.ComputeSignature(XmlElement xmlElement, String elementId, AsymmetricAlgorithm signingKey, KeyInfo keyInfo, SignedXml signedXml, String inclusiveNamespacesPrefixList, String digestMethod, String signatureMethod)
       at ComponentPro.Saml.SamlUtil.ComputeSignature(XmlElement xmlElement, String elementId, AsymmetricAlgorithm signingKey, X509Certificate2Collection x509Certificates, SignedXml signedXml, String inclusiveNamespacesPrefixList, String digestMethod, String signatureMethod)
       at ComponentPro.Saml.SamlUtil.ComputeSignature(XmlElement xmlElement, String elementId, AsymmetricAlgorithm signingKey, X509Certificate2 x509Certificate, SignedXml signedXml, String inclusiveNamespacesPrefixList, String digestMethod, String signatureMethod)
       at ComponentPro.Saml.SignableSamlObject.Sign(AsymmetricAlgorithm signingKey, X509Certificate2 x509Certificate, String inclusiveNamespacesPrefixList, String digestMethod, String signatureMethod)
       at ComponentPro.Saml.SignableSamlObject.Sign(X509Certificate2 x509Certificate, String digestMethod, String signatureMethod)
       at _Default.CreateSamlResponse() in c:\Users\sehardipe\Documents\Projects\SYMSSO\SYMSSO\Default.aspx.cs:line 76
       at _Default.Page_Load(Object sender, EventArgs e) in c:\Users\sehardipe\Documents\Projects\SYMSSO\SYMSSO\Default.aspx.cs:line 54
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: System.Security.Cryptography.CryptographicException
       HResult=-2146893816
       Message=Invalid algorithm specified.

       Source=mscorlib
       StackTrace:
            at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
            at System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash, Int32 cbHash, ObjectHandleOnStack retSignature)
            at System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash)
            at System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] rgbHash, Int32 calgHash)
            at System.Security.Cryptography.RSAPKCS1SignatureFormatter.CreateSignature(Byte[] rgbHash)
            at System.Security.Cryptography.AsymmetricSignatureFormatter.CreateSignature(HashAlgorithm hash)
            at System.Security.Cryptography.Xml.SignedXml.ComputeSignature()
            at ComponentPro.Saml.SamlUtil.ComputeSignature(XmlElement xmlElement, String elementId, AsymmetricAlgorithm signingKey, KeyInfo keyInfo, SignedXml signedXml, String inclusiveNamespacesPrefixList, String digestMethod, String signatureMethod)
       InnerException:
 

---------------------------------------------------------------------- Note: This question has been asked on the Q&A forum of Thang Dang's fraudulent ComponentPro brand If you purchased anything from ComponentPro, you have been scammed. Contact the payment processor who sold you the license and ask for your money back. Back to ComponentPro Q&A Forum Index