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, 2009 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 {
23 protected SignedInfo info;
26 protected void SetUp ()
28 info = new SignedInfo ();
34 Assert.AreEqual ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod, "CanonicalizationMethod");
35 Assert.IsNull (info.Id, "Id");
36 Assert.IsNotNull (info.References, "References");
37 Assert.AreEqual (0, info.References.Count, "References.Count");
38 Assert.IsNull (info.SignatureLength, "SignatureLength");
39 Assert.IsNull (info.SignatureMethod, "SignatureMethod");
40 Assert.AreEqual ("System.Security.Cryptography.Xml.SignedInfo", info.ToString (), "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 Assert.AreEqual ("http://www.go-mono.com/", info.CanonicalizationMethod, "CanonicalizationMethod");
56 Assert.AreEqual ("Mono::", info.Id, "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 Assert.AreEqual (1, info.References.Count, "References.Count 1");
68 Reference r2 = new Reference ("http://www.motus.com/");
69 r2.AddTransform (new XmlDsigBase64Transform ());
70 info.AddReference (r2);
71 Assert.AreEqual (2, info.References.Count, "References.Count 2");
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 Assert.AreEqual (xml, (info.GetXml ().OuterXml), "LoadXml");
84 Assert.AreEqual ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", info.CanonicalizationMethod, "LoadXml-C14N");
85 Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", info.SignatureMethod, "LoadXml-Algo");
86 Assert.AreEqual (1, info.References.Count, "LoadXml-Ref1");
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 Assert.AreEqual (doc, el.OwnerDocument, "#GetXmlWOSetProperty.document");
146 Assert.AreEqual (result, el.OuterXml, "#GetXmlWOSetProperty.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.IsTrue (doc != el.OwnerDocument, "#GetXmlWithSetProperty.document");
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 ();
195 public void SignatureLength ()
197 // we can set the length before the algorithm
198 SignedInfo si = new SignedInfo ();
199 si.SignatureLength = "128";
200 Assert.AreEqual ("128", si.SignatureLength, "SignatureLength-1");
201 Assert.IsNull (si.SignatureMethod, "SignatureMethod-1");
204 si.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
205 si.SignatureLength = "0";
206 Assert.AreEqual ("0", si.SignatureLength, "SignatureLength-2");
207 Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", si.SignatureMethod, "SignatureMethod-2");
209 // mixup length and method
210 si.SignatureLength = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
211 si.SignatureMethod = "0";
212 Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", si.SignatureLength, "SignatureLength-3");
213 Assert.AreEqual ("0", si.SignatureMethod, "SignatureMethod-3");