Supriya Gaikwad

Supriya Gaikwad

  • 1.7k
  • 4
  • 554

Generating digital signature with sha256 algorithm showing

Jan 29 2024 12:05 PM

while generating digital signature with sha256 algorithm showing malformed reference element on signedxml.computesignature.

Code  :-  Algorithm is SHA256

 private SignedXml Sign(XmlDocument doc,string LogoutResponseID)
        {
            doc.PreserveWhitespace = true;
            var TimestampID = Guid.NewGuid().ToString();
           
            X509Certificate2 cert=new X509Certificate2();
            X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
            store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
            X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindByThumbprint, CertThumbprint, false);
            if (certificates.Count == 0)
            {
                // "Certificate not installed."
            }
            else
            {
                cert = certificates[0];
            }
            store.Close();

            MySignedXml signed = new MySignedXml(doc) { SigningKey = cert.GetRSAPrivateKey() };
            //MySignedXml signed = new MySignedXml(doc.DocumentElement);
            //signed.SigningKey = cert.PrivateKey;

            

           string signatureCanonicalizationMethod = "";
            string signatureMethod = "";
            string digestMethod = "";

            signed.SignedInfo.CanonicalizationMethod = signatureCanonicalizationMethod;
            signed.SignedInfo.SignatureMethod = signatureMethod;
            
            XmlDsigEnvelopedSignatureTransform envelopeTransform = new XmlDsigEnvelopedSignatureTransform();
            envelopeTransform.Algorithm = SignedXml.XmlDsigEnvelopedSignatureTransformUrl;
            XmlUrlResolver xmlresolver = new XmlUrlResolver();
            xmlresolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
            XmlDsigExcC14NTransform cn14Transform = new XmlDsigExcC14NTransform();
            cn14Transform.Algorithm = SignedXml.XmlDsigExcC14NTransformUrl;
      

            var signatureReference = new Reference() { Uri = "#"+ LogoutResponseID };
            //signatureReference.Uri = "#"+ signed.GetIdElement(doc, LogoutResponseID);
            signatureReference.AddTransform(envelopeTransform);
            signatureReference.AddTransform(cn14Transform);
            signatureReference.DigestMethod = digestMethod;
           

            signed.AddReference(signatureReference);
         
            signed.ComputeSignature();
            

            return signed;
        }

        internal sealed class MySignedXml : SignedXml
        {
            public MySignedXml(XmlDocument xml)
            : base(xml)
        {
            }

            public MySignedXml(XmlElement xmlElement)
            : base(xmlElement)
        {

            }

            public override XmlElement GetIdElement(XmlDocument doc, string ID)
            {
        
                XmlElement idElem = base.GetIdElement(doc, ID);

                if (idElem == null)
                {
                    XmlNamespaceManager xMan = new XmlNamespaceManager(doc.NameTable);
                    xMan.AddNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");

                    idElem = doc.SelectSingleNode("/samlp:LogoutResponse/@ID='{ID}'", xMan) as XmlElement;
                   

                }

                return idElem;
            }

        }


Answers (1)