473,382 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Unable to validate digitally signed XML document

Hi All,

I have got a digitally signed XML document from client an trying to validate it. I am using the Windows 2008 server R2 environment .

In this xml file the response I am getting is with Signature algorithm sha256
ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"

I created a digitally signed xml on my local machine. It is showing the digital signature with sha1 algorithm.
SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"

I am able to validate this file using the code I have with me.
But facing problems with client generated sha256 algorithm xml file.

I am getting the error: “SignatureDescription could not be created for the signature algorithm supplied.”

I tried with various posts explaining how to validate a SAML Response but unable to get the solution.

Please suggest how I could validate this file with ds:SignatureMethod Algorithm of sha256.
------------------------------------
Here is XML file that I am able to validate:
------------------------------------
Expand|Select|Wrap|Line Numbers
  1. <MyElement xmlns="samples">
  2.   Example text to be signed.<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
  3.     <SignedInfo>
  4.       <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
  5.       <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
  6.       <Reference URI="">
  7.         <Transforms>
  8.           <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
  9.         </Transforms>
  10.         <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
  11.         <DigestValue>zSI5ZAMmQ+8u7R2rP7aAPT6nNQw=</DigestValue>
  12.       </Reference>
  13.     </SignedInfo>
  14.     <SignatureValue>sroeP57d2oEGG/vWyXNgwtVHRD6FgJPlTObOLETuh7rzCDoTHZnk9iQzZnmYg4JPLrGpZ6Ii0zBV5TQnir6ye6B4lKdIliQ7/MBIb/w1rzj37PyfjIQhOtuHDMzehvHbBm9HOd3Q3x+jWhkQlIuDiEkxyN5MECJjg1YSXCOY+pk=</SignatureValue>
  15.     <KeyInfo>
  16.       <X509Data>
  17.         <X509Certificate>MIICIzCCAYygAwIBAgIQfIzmGyaIj6BHKoXsXkPnsTANBgkqhkiG9w0BAQQFADAjMSEwHwYDVQQDHhgAWABNAEwARABTAEkARwBfAFQAZQBzAHQwHhcNMDQxMjMxMTgzMDAwWhcNMDkxMjMxMTgzMDAwWjAjMSEwHwYDVQQDHhgAWABNAEwARABTAEkARwBfAFQAZQBzAHQwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAL+hg6AnrxZssR5x2WQOC/lvzIvKtfNRgOTUCohdsZUIQADDRNVU5rF97X8huKyqHmsJdq9tuPMPJpEkXgcynBhw9uLaQp4zEOUDOS2Z/WhAASf8InFGufEjqtU2mDvDcZa6ABAMjbKSFWFYaJ9jzo6O30ADwfaxmG1Pf5WmS5YfAgMBAAGjWDBWMFQGA1UdAQRNMEuAEKywDpG6PDEdbJMg6SJ1gWqhJTAjMSEwHwYDVQQDHhgAWABNAEwARABTAEkARwBfAFQAZQBzAHSCEHyM5hsmiI+gRyqF7F5D57EwDQYJKoZIhvcNAQEEBQADgYEAPF8qOEbM+64kt+sfRtZFMCw2b4QjF2GefKpqzYol37VAdjOvoE3OMRGInQLXPf5fqPY1DLTy83zbK3A31U2UJI5RiPyAJHQQxHmKGntyo/TyGGOXZy0qtq/+ETQl1QsD0Agwvlgd9GaMK5V6/U5RrldYP2G3nQDOt8+cek/FBps=</X509Certificate>
  18.       </X509Data>
  19.     </KeyInfo>
  20.   </Signature>
  21. </MyElement>
------------------------------------
Client file( unable to validate):I have changed some of information from XML for security reasons.
------------------------------------
Expand|Select|Wrap|Line Numbers
  1. <samlp:Response ID="_9sdsddsaAAsada" Version="2.0" IssueInstant="2011-06-28T15:45:13.424Z" Destination="https://test.abc.com/abc/" Consent="urn:oasis:names:tc:SAML:2.0:consent:unspecified" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol">
  2.   <Issuer xmlns="urn:oasis:names:tc:SAML:2.0:assertion">http://mydomain.com/adfs/services/trust</Issuer>
  3.   <samlp:Status>
  4.     <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
  5.   </samlp:Status>
  6.   <Assertion ID="_7a4" IssueInstant="2011-06-28T15:45:13.424Z" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
  7.     <Issuer>http://mydomain.com/adfs/services/trust</Issuer>
  8.     <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
  9.       <ds:SignedInfo>
  10.         <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
  11.         <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
  12.         <ds:Reference URI="#_testxyz12345">
  13.           <ds:Transforms>
  14.             <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
  15.             <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
  16.           </ds:Transforms>
  17.           <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
  18.           <ds:DigestValue>tpyyynxxyyyYsk55Gh83D5kFsTgE=</ds:DigestValue>
  19.         </ds:Reference>
  20.       </ds:SignedInfo>
  21.       <ds:SignatureValue>1kWJzznFjd4F6A/ij4TdsqfXgsTN0QJ8dfshjsdjfsds njfjsdfsdjfdsfa3OvkUSYJ0iYznPmdOKD8SeTKuJfxOuUVKMoBMO6xHR48ywnRbzWIduP/p+G4Tcw/qT5Ka84aKEpA3nJLHAEEN4HsLVhQWD6jS852kyjPQIBmEGxG3Ya5TwU/vWg6budcVTXQ/vln+DhVhYEnR69CtUSp6eyIJb9rqV+HtUmz6djRN+1MB+80DQC8K4V4vW3YUiNGglZyXmF5g==</ds:SignatureValue>
  22.       <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
  23.         <ds:X509Data>
  24.           <ds:X509Certificate>BDDkqhkiG9w0BAQUFADCBsTELMAkGA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hpodHRwOi8vd3d3LmVudHJ1c3QubmV0L3JwYTAfBgNVHSMEGDAWgBQe8auJBvhJDwEzd+4Ueu4ZfJMoTTAdBgNVHQ4EFgQUNc2mj8QIq+JJ5tR1h9PdyoPdWmowCQYDVR0TBAIwADANBgkqhkiG9w0BAQUFAAOCAQEATp2vdECO6vslLbz/cKkHtSqJOym9/5t4PU68TbbBQDJwac4GRAA5NAjZ8NGaNgO7VYZAAWpd9MPcNcDo4+4kaX0UJXx6BQhfCRQLfUV2HuqNkK76DSuGsi8Q/jHb+hR/lJ0kfjopuTlU7SKT7bRmlqF01bSa09Ifds1ujgiWCBSiKDTvj5SgUW/m/TNHtKUs/KppPsIkFRJFNBiKdL6KJR2Kt6VoxfzYmAKr9WDVVj9gSM4mUpRKF1X3C7ZiHAUgNM3xSCfETK774+1g0zEjQjL04bJXPt8m8MQMmRuhm05Nsl7VumBGcwoosKPthYTrBwawen6aZhqrWstwVWs7Dw==</ds:X509Certificate>
  25.         </ds:X509Data>
  26.       </KeyInfo>
  27.     </ds:Signature>
  28.     <Subject>
  29.       <NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">SWESTKIR</NameID>
  30.       <SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
  31.         <SubjectConfirmationData NotOnOrAfter="2011-06-28T15:50:13.424Z" Recipient="https://testRecipient.test.com/abc/" />
  32.       </SubjectConfirmation>
  33.     </Subject>
  34.     <Conditions NotBefore="2011-06-28T15:45:13.416Z" NotOnOrAfter="2011-06-28T16:45:13.416Z">
  35.       <AudienceRestriction>
  36.         <Audience>https://Audience.test.com/abc/</Audience>
  37.       </AudienceRestriction>
  38.     </Conditions>
  39.     <AttributeStatement>
  40.       <Attribute Name="http://schemas.xmlsoap.org/claims/CommonName" a:OriginalIssuer="http://test.App.com/" xmlns:a="http://schemas.xmlsoap.org/ws/2009/09/identity/claims">
  41.         <AttributeValue>efs</AttributeValue>
  42.       </Attribute>
  43.       <Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname" a:OriginalIssuer="http://test.App.com/" xmlns:a="http://schemas.xmlsoap.org/ws/2009/09/identity/claims">
  44.         <AttributeValue>apple</AttributeValue>
  45.       </Attribute>
  46.       <Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname" a:OriginalIssuer="http://test.App.com/" xmlns:a="http://schemas.xmlsoap.org/ws/2009/09/identity/claims">
  47.         <AttributeValue>cap</AttributeValue>
  48.       </Attribute>
  49.       <Attribute Name="http://schemas.microsoft.com/ws/2008/06/identity/claims/role" a:OriginalIssuer="http://test.App.com/" xmlns:a="http://schemas.xmlsoap.org/ws/2009/09/identity/claims">
  50.         <AttributeValue> </AttributeValue>
  51.       </Attribute>
  52.       <Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress" a:OriginalIssuer="http://test.App.com/" xmlns:a="http://schemas.xmlsoap.org/ws/2009/09/identity/claims">
  53.         <AttributeValue>test@testApp.com</AttributeValue>
  54.       </Attribute>
  55.     </AttributeStatement>
  56.     <AuthnStatement AuthnInstant="2011-06-28T15:45:09.805Z" SessionIndex="_5a4fd-4aba-4660-a136-80rr1b4c378">
  57.       <AuthnContext>
  58.         <AuthnContextClassRef>urn:federation:authentication:windows</AuthnContextClassRef>
  59.       </AuthnContext>
  60.     </AuthnStatement>
  61.   </Assertion>
  62. </samlp:Response>
Here is my code:
------------------------------------
Code to sign a XML:
------------------------------------
Expand|Select|Wrap|Line Numbers
  1. // Sign an XML file and save the signature in a new file.
  2.         public static void SignXmlFile(string FilePath, string SignedFileNamePath, string SubjectName)
  3.         {
  4.             // Load the certificate from the certificate store.
  5.             X509Certificate2 cert = GetCertificateBySubject(SubjectName);
  6.  
  7.             // Create a new XML document.
  8.             XmlDocument doc = new XmlDocument();
  9.  
  10.             // Format the document to ignore white spaces.
  11.             doc.PreserveWhitespace = false;
  12.  
  13.             // Load the passed XML file using it's name.
  14.             doc.Load(FilePath);
  15.  
  16.             // Create a SignedXml object.
  17.             SignedXml signedXml = new SignedXml(doc);
  18.  
  19.             // Add the key to the SignedXml document. 
  20.             signedXml.SigningKey = cert.PrivateKey;
  21.  
  22.             // Create a reference to be signed.
  23.             Reference reference = new Reference();
  24.             reference.Uri = "";
  25.  
  26.             // Add an enveloped transformation to the reference.
  27.             XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
  28.             reference.AddTransform(env);
  29.  
  30.             // Add the reference to the SignedXml object.
  31.             signedXml.AddReference(reference);
  32.  
  33.             // Create a new KeyInfo object.
  34.             KeyInfo keyInfo = new KeyInfo();
  35.  
  36.             // Load the certificate into a KeyInfoX509Data object
  37.             // and add it to the KeyInfo object.
  38.             keyInfo.AddClause(new KeyInfoX509Data(cert));
  39.  
  40.             // Add the KeyInfo object to the SignedXml object.
  41.             signedXml.KeyInfo = keyInfo;
  42.  
  43.             // Compute the signature.
  44.             signedXml.ComputeSignature();
  45.  
  46.             // Get the XML representation of the signature and save
  47.             // it to an XmlElement object.
  48.             XmlElement xmlDigitalSignature = signedXml.GetXml();
  49.  
  50.             // Append the element to the XML document.
  51.             doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
  52.  
  53.             if (doc.FirstChild is XmlDeclaration)
  54.             {
  55.                 doc.RemoveChild(doc.FirstChild);
  56.             }
  57.  
  58.             // Save the signed XML document to a file specified
  59.             // using the passed string.
  60.             using (XmlTextWriter xmltw = new XmlTextWriter(SignedFileNamePath, new UTF8Encoding(false)))
  61.             {
  62.                 doc.WriteTo(xmltw);
  63.                 xmltw.Close();
  64.             }
  65.  
  66.         }
------------------------------------
Code to verify signed XML:
------------------------------------
Expand|Select|Wrap|Line Numbers
  1.   public static bool isValidSignature(String xmlFilePath, String CertificatePath)
  2.         {
  3.             // Load the certificate from the store.
  4.             X509Certificate2 cert = GetCertificateByFile(CertificatePath);
  5.  
  6.             // Create a new XML document.
  7.             XmlDocument xmlDocument = new XmlDocument();
  8.  
  9.             // Load the passed XML file into the document. 
  10.             xmlDocument.Load(xmlFilePath);
  11.  
  12.             // Create a new SignedXml object and pass it
  13.             // the XML document class.
  14.             SignedXml signedXml = new SignedXml(xmlDocument);
  15.  
  16.             // Find the "Signature" node and create a new
  17.             // XmlNodeList object.
  18.             XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");
  19.  
  20.             // Handling Signature and ds:Signature temporarily
  21.             if (nodeList.Count == 0)
  22.             {
  23.                 nodeList = xmlDocument.GetElementsByTagName("ds:Signature");
  24.             }
  25.  
  26.             // Load the signature node.
  27.             signedXml.LoadXml((XmlElement)nodeList[0]);
  28.  
  29.             // Check the signature and return the result.
  30.             return signedXml.CheckSignature(cert, true); 
  31.  
  32.         }
  33.  
This signedXml.CheckSignature(cert, true); gives the exception: “System.Security.Cryptography.CrytographicExceptio n”
with message: “SignatureDescription could not be created for the signature algorithm supplied”
when using the client XML with ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"


------------------------------------
Expand|Select|Wrap|Line Numbers
  1.   public static X509Certificate2 GetCertificateByFile(string certificatePath)
  2.         {
  3.          X509Certificate2 x509 = new X509Certificate2();
  4.          //Create X509Certificate2 object from .cer file. 
  5.          byte[] rawData = ReadFile(certificatePath);
  6.          x509.Import(rawData);
  7.          return x509; 
  8.         }
  9.  
------------------------------------
Expand|Select|Wrap|Line Numbers
  1.         //Reads a file. 
  2.         internal static byte[] ReadFile(string fileName)
  3.         {
  4.             FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  5.             int size = (int)f.Length;
  6.             byte[] data = new byte[size];
  7.             size = f.Read(data, 0, size);
  8.             f.Close();
  9.             return data;
  10.         }
  11.  
------------------------------------


Thanks in advance
Jul 14 '11 #1
0 1913

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jean-Marc Vanel | last post by:
Hello I want to validate against an XML Schema from a DOM Document object without reparsing. If the XML Schema is also from a DOM Document it would be even better. I work with Java. It seems...
1
by: Aaron | last post by:
How and where do I get my application signed so that when downloading from SP2 it will say my app is signed. Thanks, Aaron
0
by: CLarkou | last post by:
My program in Office 2003 has digitally signed databases. My security is medium. I have 1 database (MDE) without any data. Program makes a copy of this empty database (signed) to a new directory...
1
by: Peter Wyss | last post by:
Hello, Does anyone have any Information, Turtorials, FAQ's or Websites about Digitally signed Office Add-Ins written in C#? Thanks for the help, Peter Wyss
1
by: Gaurav Kapoor via .NET 247 | last post by:
Hi, Prob : I want to send an email which is digitally signed over SMTP. Env: I am developing this on .NET and referencing the CDO library (Interop) to send the mail. I havent found any hints on...
5
by: Doru Roman | last post by:
Hi, I am a beginner and I try to understand how can I use C# and Netframework to validate an XML document. Is XSD a solution? How? Thanks, Doru
4
by: Chris | last post by:
How do we create digitally signed MDE files in Access 2007 ? I tried to follow the procedure I was using in other office versions. 1. I installed the Verisign certificate (with private key)....
1
by: Ralf | last post by:
Situation: .NET windows service All works fine until we digitally signed the service exe. After that starting the service often fails (A timeout was reached (30000 milliseconds) while waiting...
2
by: Rymfax | last post by:
Hey all. I need to determine whether or not a Windows Driver is digitally signed using a C# application. Can anyone point me in the right direction for doing this? I know it has something to...
0
by: =?Utf-8?B?amZiZXJtZWpvcA==?= | last post by:
How can I send an email using System.Net.Mail digitally signed? -- Jose
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.