SignedInfoTest.cs 9.6 KB

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