SignedXmlTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // SignedXmlTest.cs - NUnit Test Cases for SignedXml
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Security.Cryptography;
  12. using System.Security.Cryptography.Xml;
  13. using System.Text;
  14. using System.Xml;
  15. namespace MonoTests.System.Security.Cryptography.Xml {
  16. public class SignedXmlTest : TestCase {
  17. public SignedXmlTest () : base ("System.Security.Cryptography.Xml.SignedXml testsuite") {}
  18. public SignedXmlTest (string name) : base (name) {}
  19. protected override void SetUp () {}
  20. protected override void TearDown () {}
  21. public static ITest Suite {
  22. get {
  23. return new TestSuite (typeof (SignedXmlTest));
  24. }
  25. }
  26. public void TestStatic ()
  27. {
  28. AssertEquals ("XmlDsigCanonicalizationUrl", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", SignedXml.XmlDsigCanonicalizationUrl);
  29. AssertEquals ("XmlDsigCanonicalizationWithCommentsUrl", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", SignedXml.XmlDsigCanonicalizationWithCommentsUrl);
  30. AssertEquals ("XmlDsigDSAUrl", "http://www.w3.org/2000/09/xmldsig#dsa-sha1", SignedXml.XmlDsigDSAUrl);
  31. AssertEquals ("XmlDsigHMACSHA1Url", "http://www.w3.org/2000/09/xmldsig#hmac-sha1", SignedXml.XmlDsigHMACSHA1Url);
  32. AssertEquals ("XmlDsigMinimalCanonicalizationUrl", "http://www.w3.org/2000/09/xmldsig#minimal", SignedXml.XmlDsigMinimalCanonicalizationUrl);
  33. AssertEquals ("XmlDsigNamespaceUrl", "http://www.w3.org/2000/09/xmldsig#", SignedXml.XmlDsigNamespaceUrl);
  34. AssertEquals ("XmlDsigRSASHA1Url", "http://www.w3.org/2000/09/xmldsig#rsa-sha1", SignedXml.XmlDsigRSASHA1Url);
  35. AssertEquals ("XmlDsigSHA1Url", "http://www.w3.org/2000/09/xmldsig#sha1", SignedXml.XmlDsigSHA1Url);
  36. }
  37. // sample from MSDN (url)
  38. public SignedXml MSDNSample ()
  39. {
  40. // Create example data to sign.
  41. XmlDocument document = new XmlDocument ();
  42. XmlNode node = document.CreateNode (XmlNodeType.Element, "", "MyElement", "samples");
  43. node.InnerText = "This is some text";
  44. document.AppendChild (node);
  45. // Create the SignedXml message.
  46. SignedXml signedXml = new SignedXml ();
  47. // Create a data object to hold the data to sign.
  48. DataObject dataObject = new DataObject ();
  49. dataObject.Data = document.ChildNodes;
  50. dataObject.Id = "MyObjectId";
  51. // Add the data object to the signature.
  52. signedXml.AddObject (dataObject);
  53. // Create a reference to be able to package everything into the
  54. // message.
  55. Reference reference = new Reference ();
  56. reference.Uri = "#MyObjectId";
  57. // Add it to the message.
  58. signedXml.AddReference (reference);
  59. return signedXml;
  60. }
  61. public void TestAsymmetricRSASignature ()
  62. {
  63. SignedXml signedXml = MSDNSample ();
  64. RSA key = RSA.Create ();
  65. signedXml.SigningKey = key;
  66. // Add a KeyInfo.
  67. KeyInfo keyInfo = new KeyInfo ();
  68. keyInfo.AddClause (new RSAKeyValue (key));
  69. signedXml.KeyInfo = keyInfo;
  70. // Compute the signature.
  71. signedXml.ComputeSignature ();
  72. // Get the XML representation of the signature.
  73. XmlElement xmlSignature = signedXml.GetXml ();
  74. // LAMESPEC: we must reload the signature or it won't work
  75. // MS framework throw a "malformed element"
  76. SignedXml vrfy = new SignedXml ();
  77. vrfy.LoadXml (xmlSignature);
  78. // assert that we can verify our own signature
  79. Assert ("RSA-Compute/Verify", vrfy.CheckSignature ());
  80. }
  81. public void TestAsymmetricDSASignature ()
  82. {
  83. SignedXml signedXml = MSDNSample ();
  84. DSA key = DSA.Create ();
  85. signedXml.SigningKey = key;
  86. // Add a KeyInfo.
  87. KeyInfo keyInfo = new KeyInfo ();
  88. keyInfo.AddClause (new DSAKeyValue (key));
  89. signedXml.KeyInfo = keyInfo;
  90. // Compute the signature.
  91. signedXml.ComputeSignature ();
  92. // Get the XML representation of the signature.
  93. XmlElement xmlSignature = signedXml.GetXml ();
  94. // LAMESPEC: we must reload the signature or it won't work
  95. // MS framework throw a "malformed element"
  96. SignedXml vrfy = new SignedXml ();
  97. vrfy.LoadXml (xmlSignature);
  98. // assert that we can verify our own signature
  99. Assert ("DSA-Compute/Verify", vrfy.CheckSignature ());
  100. }
  101. public void TestSymmetricHMACSHA1Signature ()
  102. {
  103. SignedXml signedXml = MSDNSample ();
  104. // Compute the signature.
  105. byte[] secretkey = Encoding.Default.GetBytes ("password");
  106. HMACSHA1 hmac = new HMACSHA1 (secretkey);
  107. signedXml.ComputeSignature (hmac);
  108. // Get the XML representation of the signature.
  109. XmlElement xmlSignature = signedXml.GetXml ();
  110. // LAMESPEC: we must reload the signature or it won't work
  111. // MS framework throw a "malformed element"
  112. SignedXml vrfy = new SignedXml ();
  113. vrfy.LoadXml (xmlSignature);
  114. // assert that we can verify our own signature
  115. Assert ("HMACSHA1-Compute/Verify", vrfy.CheckSignature (hmac));
  116. }
  117. public void TestSymmetricMACTripleDESSignature ()
  118. {
  119. SignedXml signedXml = MSDNSample ();
  120. // Compute the signature.
  121. byte[] secretkey = Encoding.Default.GetBytes ("password");
  122. MACTripleDES hmac = new MACTripleDES (secretkey);
  123. try {
  124. signedXml.ComputeSignature (hmac);
  125. Fail ("Expected CryptographicException but none");
  126. }
  127. catch (CryptographicException) {
  128. // this is expected
  129. }
  130. catch (Exception e) {
  131. Fail ("Expected CryptographicException but got: " + e.ToString ());
  132. }
  133. }
  134. // Using empty constructor
  135. // LAMESPEC: The two other constructors don't seems to apply in verifying signatures
  136. public void TestAsymmetricRSAVerify ()
  137. {
  138. string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>A6XuE8Cy9iOffRXaW9b0+dUcMUJQnlmwLsiqtQnADbCtZXnXAaeJ6nGnQ4Mm0IGi0AJc7/2CoJReXl7iW4hltmFguG1e3nl0VxCyCTHKGOCo1u8R3K+B1rTaenFbSxs42EM7/D9KETsPlzfYfis36yM3PqatiCUOsoMsAiMGzlc=</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>tI8QYIpbG/m6JLyvP+S3X8mzcaAIayxomyTimSh9UCpEucRnGvLw0P73uStNpiF7wltTZA1HEsv+Ha39dY/0j/Wiy3RAodGDRNuKQao1wu34aNybZ673brbsbHFUfw/o7nlKD2xO84fbajBZmKtBBDy63NHt+QL+grSrREPfCTM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>";
  139. XmlDocument doc = new XmlDocument ();
  140. doc.LoadXml (value);
  141. SignedXml v1 = new SignedXml ();
  142. v1.LoadXml (doc.DocumentElement);
  143. Assert ("RSA-CheckSignature()", v1.CheckSignature ());
  144. SignedXml v2 = new SignedXml ();
  145. v2.LoadXml (doc.DocumentElement);
  146. AsymmetricAlgorithm key = null;
  147. bool vrfy = v2.CheckSignatureReturningKey (out key);
  148. Assert ("RSA-CheckSignatureReturningKey()", vrfy);
  149. SignedXml v3 = new SignedXml ();
  150. v3.LoadXml (doc.DocumentElement);
  151. Assert ("RSA-CheckSignature(key)", v3.CheckSignature (key));
  152. }
  153. // Using empty constructor
  154. // LAMESPEC: The two other constructors don't seems to apply in verifying signatures
  155. public void TestAsymmetricDSAVerify ()
  156. {
  157. string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#dsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>BYz/qRGjGsN1yMFPxWa3awUZm1y4I/IxOQroMxkOteRGgk1HIwhRYw==</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue><P>iglVaZ+LsSL8Y0aDXmFMBwva3xHqIypr3l/LtqBH9ziV2Sh1M4JVasAiKqytWIWt/s/Uk8Ckf2tO2Ww1vsNi1NL+Kg9T7FE52sn380/rF0miwGkZeidzm74OWhykb3J+wCTXaIwOzAWI1yN7FoeoN7wzF12jjlSXAXeqPMlViqk=</P><Q>u4sowiJMHilNRojtdmIuQY2YnB8=</Q><G>SdnN7d+wn1n+HH4Hr8MIryIRYgcXdbZ5TH7jAnuWc1koqRc1AZfcYAZ6RDf+orx6Lzn055FTFiN+1NHQfGUtXJCWW0zz0FVV1NJux7WRj8vGTldjJ5ef0oCenkpwDjcIxWsZgVobve4GPoyN1sAc1scnkJB59oupibklmF4y72A=</G><Y>XejzS8Z51yfl0zbYnxSYYbHqreSLjNCoGPB/KjM1TOyV5sMjz0StKtGrFWryTWc7EgvFY7kUth4e04VKf9HbK8z/FifHTXj8+Tszbjzw8GfInnBwLN+vJgbpnjtypmiI5Bm2nLiRbfkdAHP+OrKtr/EauM9GQfYuaxm3/Vj8B84=</Y><J>vGwGg9wqwwWP9xsoPoXu6kHArJtadiNKe9azBiUx5Ob883gd5wlKfEcGuKkBmBySGbgwxyOsIBovd9Kk48hF01ymfQzAAuHR0EdJECSsTsTTKVTLQNBU32O+PRbLYpv4E8kt6rNL83JLJCBY</J><Seed>sqzn8J6fd2gtEyq6YOqiUSHgPE8=</Seed><PgenCounter>sQ==</PgenCounter></DSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>";
  158. XmlDocument doc = new XmlDocument ();
  159. doc.LoadXml (value);
  160. SignedXml v1 = new SignedXml ();
  161. v1.LoadXml (doc.DocumentElement);
  162. Assert ("DSA-CheckSignature()", v1.CheckSignature ());
  163. SignedXml v2 = new SignedXml ();
  164. v2.LoadXml (doc.DocumentElement);
  165. AsymmetricAlgorithm key = null;
  166. bool vrfy = v2.CheckSignatureReturningKey (out key);
  167. Assert ("DSA-CheckSignatureReturningKey()", vrfy);
  168. SignedXml v3 = new SignedXml ();
  169. v3.LoadXml (doc.DocumentElement);
  170. Assert ("DSA-CheckSignature(key)", v3.CheckSignature (key));
  171. }
  172. public void TestSymmetricHMACSHA1Verify ()
  173. {
  174. string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#hmac-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>e2RxYr5yGbvTqZLCFcgA2RAC0yE=</SignatureValue><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>";
  175. XmlDocument doc = new XmlDocument ();
  176. doc.LoadXml (value);
  177. SignedXml v1 = new SignedXml ();
  178. v1.LoadXml (doc.DocumentElement);
  179. byte[] secretkey = Encoding.Default.GetBytes ("password");
  180. HMACSHA1 hmac = new HMACSHA1 (secretkey);
  181. Assert ("HMACSHA1-CheckSignature(key)", v1.CheckSignature (hmac));
  182. }
  183. }
  184. }