SignedInfoTest.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // SignedInfoTest.cs - NUnit Test Cases for SignedInfo
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 2005, 2009 Novell, Inc (http://www.novell.com)
  9. //
  10. using System;
  11. using System.Security.Cryptography;
  12. using System.Security.Cryptography.Xml;
  13. using System.Xml;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Security.Cryptography.Xml {
  16. [TestFixture]
  17. public class SignedInfoTest {
  18. protected SignedInfo info;
  19. [SetUp]
  20. protected void SetUp ()
  21. {
  22. info = new SignedInfo ();
  23. }
  24. [Test]
  25. public void Empty ()
  26. {
  27. Assert.AreEqual ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod, "CanonicalizationMethod");
  28. Assert.IsNull (info.Id, "Id");
  29. Assert.IsNotNull (info.References, "References");
  30. Assert.AreEqual (0, info.References.Count, "References.Count");
  31. Assert.IsNull (info.SignatureLength, "SignatureLength");
  32. Assert.IsNull (info.SignatureMethod, "SignatureMethod");
  33. Assert.AreEqual ("System.Security.Cryptography.Xml.SignedInfo", info.ToString (), "ToString()");
  34. }
  35. [Test]
  36. [ExpectedException (typeof (CryptographicException))]
  37. public void EmptyException ()
  38. {
  39. string xml = info.GetXml ().OuterXml;
  40. }
  41. [Test]
  42. public void Properties ()
  43. {
  44. info.CanonicalizationMethod = "http://www.go-mono.com/";
  45. Assert.AreEqual ("http://www.go-mono.com/", info.CanonicalizationMethod, "CanonicalizationMethod");
  46. info.Id = "Mono::";
  47. Assert.AreEqual ("Mono::", info.Id, "Id");
  48. }
  49. [Test]
  50. public void References ()
  51. {
  52. Reference r1 = new Reference ();
  53. r1.Uri = "http://www.go-mono.com/";
  54. r1.AddTransform (new XmlDsigBase64Transform ());
  55. info.AddReference (r1);
  56. Assert.AreEqual (1, info.References.Count, "References.Count 1");
  57. Reference r2 = new Reference ("http://www.motus.com/");
  58. r2.AddTransform (new XmlDsigBase64Transform ());
  59. info.AddReference (r2);
  60. Assert.AreEqual (2, info.References.Count, "References.Count 2");
  61. info.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
  62. }
  63. [Test]
  64. public void Load ()
  65. {
  66. string xml = "<SignedInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><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>";
  67. XmlDocument doc = new XmlDocument ();
  68. doc.LoadXml (xml);
  69. info.LoadXml (doc.DocumentElement);
  70. Assert.AreEqual (xml, (info.GetXml ().OuterXml), "LoadXml");
  71. Assert.AreEqual ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod, "LoadXml-C14N");
  72. Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", info.SignatureMethod, "LoadXml-Algo");
  73. Assert.AreEqual (1, info.References.Count, "LoadXml-Ref1");
  74. }
  75. // there are many (documented) not supported methods in SignedInfo
  76. [Test]
  77. [ExpectedException (typeof (NotSupportedException))]
  78. public void NotSupportedCount ()
  79. {
  80. int n = info.Count;
  81. }
  82. [Test]
  83. [ExpectedException (typeof (NotSupportedException))]
  84. public void NotSupportedIsReadOnly ()
  85. {
  86. bool b = info.IsReadOnly;
  87. }
  88. [Test]
  89. [ExpectedException (typeof (NotSupportedException))]
  90. public void NotSupportedIsSynchronized ()
  91. {
  92. bool b = info.IsSynchronized;
  93. }
  94. [Test]
  95. [ExpectedException (typeof (NotSupportedException))]
  96. public void NotSupportedSyncRoot ()
  97. {
  98. object o = info.SyncRoot;
  99. }
  100. [Test]
  101. [ExpectedException (typeof (NotSupportedException))]
  102. public void NotSupportedCopyTo ()
  103. {
  104. info.CopyTo (null, 0);
  105. }
  106. // from phaos testcase
  107. const string xmlForGetXml = @"<player bats=""left"" id=""10012"" throws=""right"">
  108. <!-- Here&apos;s a comment -->
  109. <name>Alfonso Soriano</name>
  110. <position>2B</position>
  111. <team>New York Yankees</team>
  112. <dsig:Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"" xmlns:dsig=""http://www.w3.org/2000/09/xmldsig#"">"
  113. + @"<dsig:SignedInfo><dsig:CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-withcomments-20010315""/><dsig:SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#rsa-sha1""/>"
  114. + @"<dsig:Reference URI=""""><dsig:Transforms><dsig:Transform Algorithm=""http://www.w3.org/2000/09/xmldsig#enveloped-signature""/></dsig:Transforms><dsig:DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1""/><dsig:DigestValue>nDF2V/bzRd0VE3EwShWtsBzTEDc=</dsig:DigestValue></dsig:Reference></dsig:SignedInfo><dsig:SignatureValue>fbye4Xm//RPUTsLd1dwJPo0gPZYX6gVYCEB/gz2348EARNk/nCCch1fFfpuqAGMKg4ayVC0yWkUyE5V4QB33jaGlh9wuNQSjxs6TIvFwSsT+0ioDgVgFv0gVeasbyNL4rFEHuAWL8QKwDT9L6b2wUvJC90DmpBs9GMR2jTZIWlM=</dsig:SignatureValue><dsig:KeyInfo><dsig:X509Data><dsig:X509Certificate>MIIC0DCCAjmgAwIBAgIDD0JBMA0GCSqGSIb3DQEBBAUAMHwxCzAJBgNVBAYTAlVTMREwDwYDVQQIEwhOZXcgWW9yazERMA8GA1UEBxMITmV3IFlvcmsxGTAXBgNVBAoTEFBoYW9zIFRlY2hub2xvZ3kxFDASBgNVBAsTC0VuZ2luZWVyaW5nMRYwFAYDVQQDEw1UZXN0IENBIChSU0EpMB4XDTAyMDQyOTE5MTY0MFoXDTEyMDQyNjE5MTY0MFowgYAxCzAJBgNVBAYTAlVTMREwDwYDVQQIEwhOZXcgWW9yazERMA8GA1UEBxMITmV3IFlvcmsxGTAXBgNVBAoTEFBoYW9zIFRlY2hub2xvZ3kxFDASBgNVBAsTC0VuZ2luZWVyaW5nMRowGAYDVQQDExFUZXN0IENsaWVudCAoUlNBKTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAgIb6nAB9oS/AI5jIj6WymvQhRxiMlE07G4abmMliYi5zWzvaFE2tnU+RZIBgtoXcgDEIU/vsLQut7nzCn9mHxC8JEaV4D4U91j64AyZakShqJw7qjJfqUxxPL0yJv2oFiouPDjGuJ9JPi0NrsZq+yfWfM54s4b9SNkcOIVMybZUCAwEAAaNbMFkwDAYDVR0TAQH/BAIwADAPBgNVHQ8BAf8EBQMDB9gAMBkGA1UdEQQSMBCBDnRlY2hAcGhhb3MuY29tMB0GA1UdDgQWBBQT58rBCxPmVLeZaYGRqVROnQlFbzANBgkqhkiG9w0BAQQFAAOBgQCxbCovFST25t+ryN1RipqozxJQcguKfeCwbfgBNobzcRvoW0kSIf7zi4mtQajDM0NfslFF51/dex5Rn64HmFFshSwSvQQMyf5Cfaqv2XQ60OXq6nAFG6WbHoge6RqfIez2MWDLoSB6plsjKtMmL3mcybBhROtX5GGuLx1NtfhNFQ==</dsig:X509Certificate><dsig:X509IssuerSerial><dsig:X509IssuerName>CN=Test CA (RSA),OU=Engineering,O=Phaos Technology,L=New York,ST=New York,C=US</dsig:X509IssuerName><dsig:X509SerialNumber>1000001</dsig:X509SerialNumber></dsig:X509IssuerSerial><dsig:X509SubjectName>CN=Test Client (RSA),OU=Engineering,O=Phaos Technology,L=New York,ST=New York,C=US</dsig:X509SubjectName><dsig:X509SKI>E+fKwQsT5lS3mWmBkalUTp0JRW8=</dsig:X509SKI></dsig:X509Data></dsig:KeyInfo></dsig:Signature></player>";
  115. [Test]
  116. public void GetXmlWithoutSetProperty ()
  117. {
  118. string result = @"<dsig:SignedInfo xmlns:dsig=""http://www.w3.org/2000/09/xmldsig#""><dsig:CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-withcomments-20010315"" /><dsig:SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#rsa-sha1"" /><dsig:Reference URI=""""><dsig:Transforms><dsig:Transform Algorithm=""http://www.w3.org/2000/09/xmldsig#enveloped-signature"" /></dsig:Transforms><dsig:DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><dsig:DigestValue>nDF2V/bzRd0VE3EwShWtsBzTEDc=</dsig:DigestValue></dsig:Reference></dsig:SignedInfo>";
  119. XmlDocument doc = new XmlDocument ();
  120. doc.LoadXml (xmlForGetXml);
  121. SignedInfo sig = new SignedInfo ();
  122. sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
  123. XmlElement el = sig.GetXml ();
  124. Assert.AreEqual (doc, el.OwnerDocument, "#GetXmlWOSetProperty.document");
  125. Assert.AreEqual (result, el.OuterXml, "#GetXmlWOSetProperty.outerxml");
  126. }
  127. [Test]
  128. // urn:foo is'nt accepted when calling GetXml
  129. [ExpectedException (typeof (CryptographicException))]
  130. [Category ("NotWorking")]
  131. public void GetXmlWithSetProperty ()
  132. {
  133. XmlDocument doc = new XmlDocument ();
  134. doc.LoadXml (xmlForGetXml);
  135. SignedInfo sig = new SignedInfo ();
  136. sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
  137. sig.CanonicalizationMethod = "urn:foo";
  138. XmlElement el = sig.GetXml ();
  139. Assert.IsTrue (doc != el.OwnerDocument, "#GetXmlWithSetProperty.document");
  140. }
  141. [Test] // never fails
  142. public void EmptyReferenceWithoutSetProperty ()
  143. {
  144. XmlDocument doc = new XmlDocument ();
  145. doc.LoadXml (xmlForGetXml);
  146. XmlNode n = doc.SelectSingleNode ("//*[local-name()='Reference']");
  147. n.ParentNode.RemoveChild (n);
  148. SignedInfo sig = new SignedInfo ();
  149. sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
  150. XmlElement el = sig.GetXml ();
  151. }
  152. [Test]
  153. [ExpectedException (typeof (CryptographicException))]
  154. public void EmptyReferenceWithSetProperty ()
  155. {
  156. XmlDocument doc = new XmlDocument ();
  157. doc.LoadXml (xmlForGetXml);
  158. XmlNode n = doc.SelectSingleNode ("//*[local-name()='Reference']");
  159. n.ParentNode.RemoveChild (n);
  160. SignedInfo sig = new SignedInfo ();
  161. sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
  162. sig.CanonicalizationMethod = "urn:foo";
  163. XmlElement el = sig.GetXml ();
  164. }
  165. [Test]
  166. public void SignatureLength ()
  167. {
  168. // we can set the length before the algorithm
  169. SignedInfo si = new SignedInfo ();
  170. si.SignatureLength = "128";
  171. Assert.AreEqual ("128", si.SignatureLength, "SignatureLength-1");
  172. Assert.IsNull (si.SignatureMethod, "SignatureMethod-1");
  173. // zero
  174. si.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
  175. si.SignatureLength = "0";
  176. Assert.AreEqual ("0", si.SignatureLength, "SignatureLength-2");
  177. Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", si.SignatureMethod, "SignatureMethod-2");
  178. // mixup length and method
  179. si.SignatureLength = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
  180. si.SignatureMethod = "0";
  181. Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", si.SignatureLength, "SignatureLength-3");
  182. Assert.AreEqual ("0", si.SignatureMethod, "SignatureMethod-3");
  183. }
  184. }
  185. }