2 // SignedInfoTest.cs - NUnit Test Cases for SignedInfo
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 using System.Security.Cryptography;
13 using System.Security.Cryptography.Xml;
16 using NUnit.Framework;
18 namespace MonoTests.System.Security.Cryptography.Xml {
21 public class SignedInfoTest : Assertion {
23 protected SignedInfo info;
26 protected void SetUp ()
28 info = new SignedInfo ();
34 AssertEquals ("CanonicalizationMethod", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod);
35 AssertNull ("Id", info.Id);
36 AssertNotNull ("References", info.References);
37 AssertEquals ("References.Count", 0, info.References.Count);
38 AssertNull ("SignatureLength", info.SignatureLength);
39 AssertNull ("SignatureMethod", info.SignatureMethod);
40 AssertEquals ("ToString()", "System.Security.Cryptography.Xml.SignedInfo", info.ToString ());
44 [ExpectedException (typeof (CryptographicException))]
45 public void EmptyException ()
47 string xml = info.GetXml ().OuterXml;
51 public void Properties ()
53 info.CanonicalizationMethod = "http://www.go-mono.com/";
54 AssertEquals ("CanonicalizationMethod", "http://www.go-mono.com/", info.CanonicalizationMethod);
56 AssertEquals ("Id", "Mono::", info.Id);
60 public void References ()
62 Reference r1 = new Reference ();
63 r1.Uri = "http://www.go-mono.com/";
64 r1.AddTransform (new XmlDsigBase64Transform ());
65 info.AddReference (r1);
66 AssertEquals ("References.Count 1", 1, info.References.Count);
68 Reference r2 = new Reference ("http://www.motus.com/");
69 r2.AddTransform (new XmlDsigBase64Transform ());
70 info.AddReference (r2);
71 AssertEquals ("References.Count 2", 2, info.References.Count);
73 info.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
79 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>";
80 XmlDocument doc = new XmlDocument ();
82 info.LoadXml (doc.DocumentElement);
83 AssertEquals ("LoadXml", xml, (info.GetXml ().OuterXml));
84 AssertEquals ("LoadXml-C14N", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod);
85 AssertEquals ("LoadXml-Algo", "http://www.w3.org/2000/09/xmldsig#rsa-sha1", info.SignatureMethod);
86 AssertEquals ("LoadXml-Ref1", 1, info.References.Count);
89 // there are many (documented) not supported methods in SignedInfo
92 [ExpectedException (typeof (NotSupportedException))]
93 public void NotSupportedCount ()
99 [ExpectedException (typeof (NotSupportedException))]
100 public void NotSupportedIsReadOnly ()
102 bool b = info.IsReadOnly;
106 [ExpectedException (typeof (NotSupportedException))]
107 public void NotSupportedIsSynchronized ()
109 bool b = info.IsSynchronized;
113 [ExpectedException (typeof (NotSupportedException))]
114 public void NotSupportedSyncRoot ()
116 object o = info.SyncRoot;
120 [ExpectedException (typeof (NotSupportedException))]
121 public void NotSupportedCopyTo ()
123 info.CopyTo (null, 0);
126 // from phaos testcase
127 const string xmlForGetXml = @"<player bats=""left"" id=""10012"" throws=""right"">
128 <!-- Here's a comment -->
129 <name>Alfonso Soriano</name>
130 <position>2B</position>
131 <team>New York Yankees</team>
132 <dsig:Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"" xmlns:dsig=""http://www.w3.org/2000/09/xmldsig#"">"
133 + @"<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""/>"
134 + @"<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>";
137 public void GetXmlWithoutSetProperty ()
139 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>";
140 XmlDocument doc = new XmlDocument ();
141 doc.LoadXml (xmlForGetXml);
142 SignedInfo sig = new SignedInfo ();
143 sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
144 XmlElement el = sig.GetXml ();
145 AssertEquals ("#GetXmlWOSetProperty.document", doc, el.OwnerDocument);
146 AssertEquals ("#GetXmlWOSetProperty.outerxml", result, el.OuterXml);
151 // urn:foo is'nt accepted when calling GetXml
152 [ExpectedException (typeof (CryptographicException))]
153 [Category ("NotWorking")]
155 public void GetXmlWithSetProperty ()
157 XmlDocument doc = new XmlDocument ();
158 doc.LoadXml (xmlForGetXml);
159 SignedInfo sig = new SignedInfo ();
160 sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
161 sig.CanonicalizationMethod = "urn:foo";
162 XmlElement el = sig.GetXml ();
163 Assert ("#GetXmlWithSetProperty.document", doc != el.OwnerDocument);
166 [Test] // never fails
167 public void EmptyReferenceWithoutSetProperty ()
169 XmlDocument doc = new XmlDocument ();
170 doc.LoadXml (xmlForGetXml);
171 XmlNode n = doc.SelectSingleNode ("//*[local-name()='Reference']");
172 n.ParentNode.RemoveChild (n);
174 SignedInfo sig = new SignedInfo ();
175 sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
176 XmlElement el = sig.GetXml ();
180 [ExpectedException (typeof (CryptographicException))]
181 public void EmptyReferenceWithSetProperty ()
183 XmlDocument doc = new XmlDocument ();
184 doc.LoadXml (xmlForGetXml);
185 XmlNode n = doc.SelectSingleNode ("//*[local-name()='Reference']");
186 n.ParentNode.RemoveChild (n);
188 SignedInfo sig = new SignedInfo ();
189 sig.LoadXml ((XmlElement) doc.SelectSingleNode ("//*[local-name()='SignedInfo']"));
190 sig.CanonicalizationMethod = "urn:foo";
191 XmlElement el = sig.GetXml ();