SignedXmlTest.cs 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. //
  2. // SignedXmlTest.cs - NUnit Test Cases for SignedXml
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 2004-2005, 2008 Novell, Inc (http://www.novell.com)
  9. //
  10. using System;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Security.Cryptography;
  14. using System.Security.Cryptography.X509Certificates;
  15. using System.Security.Cryptography.Xml;
  16. using System.Text;
  17. using System.Xml;
  18. using NUnit.Framework;
  19. namespace MonoTests.System.Security.Cryptography.Xml {
  20. public class SignedXmlEx : SignedXml {
  21. // required to test protected GetPublicKey in SignedXml
  22. public AsymmetricAlgorithm PublicGetPublicKey ()
  23. {
  24. return base.GetPublicKey ();
  25. }
  26. }
  27. [TestFixture]
  28. public class SignedXmlTest {
  29. private const string signature = "<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>CTnnhjxUQHJmD+t1MjVXrOW+MCA=</DigestValue></Reference></SignedInfo><SignatureValue>dbFt6Zw3vR+Xh7LbM/vuifyFA7gPh/NlDM2Glz/SJBsveISieuTBpZlk/zavAeuXR/Nu0Ztt4OP4tCOg09a2RNlrTP0dhkeEfL1jTzpnVaLHuQbCiwOWCgbRif7Xt7N12FuiHYb3BltP/YyXS4E12NxlGlqnDiFA1v/mkK5+C1o=</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>hEfTJNa2idz2u+fSYDDG4Lx/xuk4aBbvOPVNqgc1l9Y8t7Pt+ZyF+kkF3uUl8Y0700BFGAsprnhwrWENK+PGdtvM5796ZKxCCa0ooKkofiT4355HqK26hpV8dvj38vq/rkJe1jHZgkTKa+c/0vjcYZOI/RT/IZv9JfXxVWLuLxk=</Modulus><Exponent>EQ==</Exponent></RSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><ObjectListTag xmlns=\"\" /></Object></Signature>";
  30. [Test]
  31. public void StaticValues ()
  32. {
  33. Assert.AreEqual ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", SignedXml.XmlDsigCanonicalizationUrl, "XmlDsigCanonicalizationUrl");
  34. Assert.AreEqual ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", SignedXml.XmlDsigCanonicalizationWithCommentsUrl, "XmlDsigCanonicalizationWithCommentsUrl");
  35. Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#dsa-sha1", SignedXml.XmlDsigDSAUrl, "XmlDsigDSAUrl");
  36. Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#hmac-sha1", SignedXml.XmlDsigHMACSHA1Url, "XmlDsigHMACSHA1Url");
  37. Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#minimal", SignedXml.XmlDsigMinimalCanonicalizationUrl, "XmlDsigMinimalCanonicalizationUrl");
  38. Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#", SignedXml.XmlDsigNamespaceUrl, "XmlDsigNamespaceUrl");
  39. Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", SignedXml.XmlDsigRSASHA1Url, "XmlDsigRSASHA1Url");
  40. Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#sha1", SignedXml.XmlDsigSHA1Url, "XmlDsigSHA1Url");
  41. }
  42. [Test]
  43. public void Constructor_Empty ()
  44. {
  45. XmlDocument doc = new XmlDocument ();
  46. doc.LoadXml (signature);
  47. XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
  48. XmlElement xel = (XmlElement) xnl [0];
  49. SignedXml sx = new SignedXml (doc);
  50. sx.LoadXml (xel);
  51. Assert.IsTrue (sx.CheckSignature (), "CheckSignature");
  52. }
  53. [Test]
  54. public void Constructor_XmlDocument ()
  55. {
  56. XmlDocument doc = new XmlDocument ();
  57. doc.LoadXml (signature);
  58. XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
  59. XmlElement xel = (XmlElement) xnl [0];
  60. SignedXml sx = new SignedXml (doc);
  61. sx.LoadXml (doc.DocumentElement);
  62. Assert.IsTrue (sx.CheckSignature (), "CheckSignature");
  63. }
  64. [Test]
  65. #if NET_2_0
  66. [Ignore ("2.0 throws a NullReferenceException - reported as FDBK25892")]
  67. // http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=02dd9730-d1ad-4170-8c82-36858c55fbe2
  68. #endif
  69. [ExpectedException (typeof (ArgumentNullException))]
  70. public void Constructor_XmlDocument_Null ()
  71. {
  72. XmlDocument doc = null;
  73. SignedXml sx = new SignedXml (doc);
  74. }
  75. [Test]
  76. public void Constructor_XmlElement ()
  77. {
  78. XmlDocument doc = new XmlDocument ();
  79. doc.LoadXml (signature);
  80. XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
  81. XmlElement xel = (XmlElement) xnl [0];
  82. SignedXml sx = new SignedXml (doc.DocumentElement);
  83. sx.LoadXml (xel);
  84. Assert.IsTrue (sx.CheckSignature (), "CheckSignature");
  85. }
  86. [Test]
  87. #if !NET_2_0
  88. [ExpectedException (typeof (CryptographicException))]
  89. #endif
  90. public void Constructor_XmlElement_WithoutLoadXml ()
  91. {
  92. XmlDocument doc = new XmlDocument ();
  93. doc.LoadXml (signature);
  94. XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
  95. XmlElement xel = (XmlElement) xnl [0];
  96. SignedXml sx = new SignedXml (doc.DocumentElement);
  97. Assert.IsTrue (!sx.CheckSignature (), "!CheckSignature");
  98. // SignedXml (XmlElement) != SignedXml () + LoadXml (XmlElement)
  99. }
  100. [Test]
  101. [ExpectedException (typeof (ArgumentNullException))]
  102. public void Constructor_XmlElement_Null ()
  103. {
  104. XmlElement xel = null;
  105. SignedXml sx = new SignedXml (xel);
  106. }
  107. // sample from MSDN (url)
  108. public SignedXml MSDNSample ()
  109. {
  110. // Create example data to sign.
  111. XmlDocument document = new XmlDocument ();
  112. XmlNode node = document.CreateNode (XmlNodeType.Element, "", "MyElement", "samples");
  113. node.InnerText = "This is some text";
  114. document.AppendChild (node);
  115. // Create the SignedXml message.
  116. SignedXml signedXml = new SignedXml ();
  117. // Create a data object to hold the data to sign.
  118. DataObject dataObject = new DataObject ();
  119. dataObject.Data = document.ChildNodes;
  120. dataObject.Id = "MyObjectId";
  121. // Add the data object to the signature.
  122. signedXml.AddObject (dataObject);
  123. // Create a reference to be able to package everything into the
  124. // message.
  125. Reference reference = new Reference ();
  126. reference.Uri = "#MyObjectId";
  127. // Add it to the message.
  128. signedXml.AddReference (reference);
  129. return signedXml;
  130. }
  131. [Test]
  132. [ExpectedException (typeof (CryptographicException))]
  133. public void SignatureMethodMismatch ()
  134. {
  135. SignedXml signedXml = MSDNSample ();
  136. RSA key = RSA.Create ();
  137. signedXml.SigningKey = key;
  138. signedXml.SignedInfo.SignatureMethod = SignedXml.XmlDsigHMACSHA1Url;
  139. // Add a KeyInfo.
  140. KeyInfo keyInfo = new KeyInfo ();
  141. keyInfo.AddClause (new RSAKeyValue (key));
  142. signedXml.KeyInfo = keyInfo;
  143. Assert.IsNotNull (signedXml.SignatureMethod, "SignatureMethod");
  144. // Compute the signature - causes unsupported algorithm by the key.
  145. signedXml.ComputeSignature ();
  146. }
  147. [Test]
  148. public void AsymmetricRSASignature ()
  149. {
  150. SignedXml signedXml = MSDNSample ();
  151. RSA key = RSA.Create ();
  152. signedXml.SigningKey = key;
  153. // Add a KeyInfo.
  154. KeyInfo keyInfo = new KeyInfo ();
  155. keyInfo.AddClause (new RSAKeyValue (key));
  156. signedXml.KeyInfo = keyInfo;
  157. Assert.AreEqual (1, signedXml.KeyInfo.Count, "KeyInfo");
  158. Assert.IsNull (signedXml.SignatureLength, "SignatureLength");
  159. Assert.IsNull (signedXml.SignatureMethod, "SignatureMethod");
  160. Assert.IsNull (signedXml.SignatureValue, "SignatureValue");
  161. Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
  162. // Compute the signature.
  163. signedXml.ComputeSignature ();
  164. Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
  165. Assert.AreEqual (SignedXml.XmlDsigRSASHA1Url, signedXml.SignatureMethod, "SignatureMethod");
  166. Assert.AreEqual (128, signedXml.SignatureValue.Length, "SignatureValue");
  167. Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
  168. // Get the XML representation of the signature.
  169. XmlElement xmlSignature = signedXml.GetXml ();
  170. // LAMESPEC: we must reload the signature or it won't work
  171. // MS framework throw a "malformed element"
  172. SignedXml vrfy = new SignedXml ();
  173. vrfy.LoadXml (xmlSignature);
  174. // assert that we can verify our own signature
  175. Assert.IsTrue (vrfy.CheckSignature (), "RSA-Compute/Verify");
  176. }
  177. [Test]
  178. public void AsymmetricDSASignature ()
  179. {
  180. SignedXml signedXml = MSDNSample ();
  181. DSA key = DSA.Create ();
  182. signedXml.SigningKey = key;
  183. // Add a KeyInfo.
  184. KeyInfo keyInfo = new KeyInfo ();
  185. keyInfo.AddClause (new DSAKeyValue (key));
  186. signedXml.KeyInfo = keyInfo;
  187. Assert.AreEqual (1, signedXml.KeyInfo.Count, "KeyInfo");
  188. Assert.IsNull (signedXml.SignatureLength, "SignatureLength");
  189. Assert.IsNull (signedXml.SignatureMethod, "SignatureMethod");
  190. Assert.IsNull (signedXml.SignatureValue, "SignatureValue");
  191. Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
  192. // Compute the signature.
  193. signedXml.ComputeSignature ();
  194. Assert.IsNull (signedXml.SignatureLength, "SignatureLength");
  195. Assert.AreEqual (SignedXml.XmlDsigDSAUrl, signedXml.SignatureMethod, "SignatureMethod");
  196. Assert.AreEqual (40, signedXml.SignatureValue.Length, "SignatureValue");
  197. Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
  198. // Get the XML representation of the signature.
  199. XmlElement xmlSignature = signedXml.GetXml ();
  200. // LAMESPEC: we must reload the signature or it won't work
  201. // MS framework throw a "malformed element"
  202. SignedXml vrfy = new SignedXml ();
  203. vrfy.LoadXml (xmlSignature);
  204. // assert that we can verify our own signature
  205. Assert.IsTrue (vrfy.CheckSignature (), "DSA-Compute/Verify");
  206. }
  207. [Test]
  208. public void SymmetricHMACSHA1Signature ()
  209. {
  210. SignedXml signedXml = MSDNSample ();
  211. // Compute the signature.
  212. byte[] secretkey = Encoding.Default.GetBytes ("password");
  213. HMACSHA1 hmac = new HMACSHA1 (secretkey);
  214. #if NET_2_0
  215. Assert.AreEqual (0, signedXml.KeyInfo.Count, "KeyInfo");
  216. #else
  217. Assert.IsNull (signedXml.KeyInfo, "KeyInfo");
  218. #endif
  219. Assert.IsNull (signedXml.SignatureLength, "SignatureLength");
  220. Assert.IsNull (signedXml.SignatureMethod, "SignatureMethod");
  221. Assert.IsNull (signedXml.SignatureValue, "SignatureValue");
  222. Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
  223. signedXml.ComputeSignature (hmac);
  224. #if NET_2_0
  225. Assert.AreEqual (0, signedXml.KeyInfo.Count, "KeyInfo");
  226. #else
  227. Assert.IsNull (signedXml.KeyInfo, "KeyInfo");
  228. #endif
  229. Assert.IsNull (signedXml.SignatureLength, "SignatureLength");
  230. Assert.AreEqual (SignedXml.XmlDsigHMACSHA1Url, signedXml.SignatureMethod, "SignatureMethod");
  231. Assert.AreEqual (20, signedXml.SignatureValue.Length, "SignatureValue");
  232. Assert.IsNull (signedXml.SigningKeyName, "SigningKeyName");
  233. // Get the XML representation of the signature.
  234. XmlElement xmlSignature = signedXml.GetXml ();
  235. // LAMESPEC: we must reload the signature or it won't work
  236. // MS framework throw a "malformed element"
  237. SignedXml vrfy = new SignedXml ();
  238. vrfy.LoadXml (xmlSignature);
  239. // assert that we can verify our own signature
  240. Assert.IsTrue (vrfy.CheckSignature (hmac), "HMACSHA1-Compute/Verify");
  241. }
  242. [Test]
  243. [ExpectedException (typeof (CryptographicException))]
  244. public void SymmetricMACTripleDESSignature ()
  245. {
  246. SignedXml signedXml = MSDNSample ();
  247. // Compute the signature.
  248. byte[] secretkey = Encoding.Default.GetBytes ("password");
  249. MACTripleDES hmac = new MACTripleDES (secretkey);
  250. signedXml.ComputeSignature (hmac);
  251. }
  252. // Using empty constructor
  253. // LAMESPEC: The two other constructors don't seems to apply in verifying signatures
  254. [Test]
  255. public void AsymmetricRSAVerify ()
  256. {
  257. 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>";
  258. XmlDocument doc = new XmlDocument ();
  259. doc.LoadXml (value);
  260. SignedXml v1 = new SignedXml ();
  261. v1.LoadXml (doc.DocumentElement);
  262. Assert.IsTrue (v1.CheckSignature (), "RSA-CheckSignature()");
  263. SignedXml v2 = new SignedXml ();
  264. v2.LoadXml (doc.DocumentElement);
  265. AsymmetricAlgorithm key = null;
  266. bool vrfy = v2.CheckSignatureReturningKey (out key);
  267. Assert.IsTrue (vrfy, "RSA-CheckSignatureReturningKey()");
  268. SignedXml v3 = new SignedXml ();
  269. v3.LoadXml (doc.DocumentElement);
  270. Assert.IsTrue (v3.CheckSignature (key), "RSA-CheckSignature(key)");
  271. }
  272. // Using empty constructor
  273. // LAMESPEC: The two other constructors don't seems to apply in verifying signatures
  274. [Test]
  275. public void AsymmetricDSAVerify ()
  276. {
  277. 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>";
  278. XmlDocument doc = new XmlDocument ();
  279. doc.LoadXml (value);
  280. SignedXml v1 = new SignedXml ();
  281. v1.LoadXml (doc.DocumentElement);
  282. Assert.IsTrue (v1.CheckSignature (), "DSA-CheckSignature()");
  283. SignedXml v2 = new SignedXml ();
  284. v2.LoadXml (doc.DocumentElement);
  285. AsymmetricAlgorithm key = null;
  286. bool vrfy = v2.CheckSignatureReturningKey (out key);
  287. Assert.IsTrue (vrfy, "DSA-CheckSignatureReturningKey()");
  288. SignedXml v3 = new SignedXml ();
  289. v3.LoadXml (doc.DocumentElement);
  290. Assert.IsTrue (v3.CheckSignature (key), "DSA-CheckSignature(key)");
  291. }
  292. [Test]
  293. public void SymmetricHMACSHA1Verify ()
  294. {
  295. 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>";
  296. XmlDocument doc = new XmlDocument ();
  297. doc.LoadXml (value);
  298. SignedXml v1 = new SignedXml ();
  299. v1.LoadXml (doc.DocumentElement);
  300. byte[] secretkey = Encoding.Default.GetBytes ("password");
  301. HMACSHA1 hmac = new HMACSHA1 (secretkey);
  302. Assert.IsTrue (v1.CheckSignature (hmac), "HMACSHA1-CheckSignature(key)");
  303. }
  304. [Test]
  305. // adapted from http://bugzilla.ximian.com/show_bug.cgi?id=52084
  306. public void GetIdElement ()
  307. {
  308. XmlDocument doc = new XmlDocument ();
  309. doc.LoadXml (signature);
  310. SignedXml v1 = new SignedXml ();
  311. v1.LoadXml (doc.DocumentElement);
  312. Assert.IsTrue (v1.CheckSignature (), "CheckSignature");
  313. XmlElement xel = v1.GetIdElement (doc, "MyObjectId");
  314. Assert.IsTrue (xel.InnerXml.StartsWith ("<ObjectListTag"), "GetIdElement");
  315. }
  316. [Test]
  317. public void GetPublicKey ()
  318. {
  319. XmlDocument doc = new XmlDocument ();
  320. doc.LoadXml (signature);
  321. SignedXmlEx sxe = new SignedXmlEx ();
  322. sxe.LoadXml (doc.DocumentElement);
  323. AsymmetricAlgorithm aa1 = sxe.PublicGetPublicKey ();
  324. Assert.IsTrue ((aa1 is RSA), "First Public Key is RSA");
  325. AsymmetricAlgorithm aa2 = sxe.PublicGetPublicKey ();
  326. Assert.IsNull (aa2, "Second Public Key is null");
  327. }
  328. #if NET_2_0
  329. [Test]
  330. // [ExpectedException (typeof (ArgumentNullException))]
  331. public void AddObject_Null ()
  332. {
  333. SignedXml sx = new SignedXml ();
  334. // still no ArgumentNullExceptions for this one
  335. sx.AddObject (null);
  336. }
  337. [Test]
  338. [ExpectedException (typeof (ArgumentNullException))]
  339. public void AddReference_Null ()
  340. {
  341. SignedXml sx = new SignedXml ();
  342. sx.AddReference (null);
  343. }
  344. #else
  345. [Test]
  346. public void Add_Null ()
  347. {
  348. SignedXml sx = new SignedXml ();
  349. // no ArgumentNull exceptions for those
  350. sx.AddObject (null);
  351. sx.AddReference (null);
  352. }
  353. #endif
  354. [Test]
  355. [ExpectedException (typeof (CryptographicException))]
  356. public void GetXml_WithoutInfo ()
  357. {
  358. SignedXml sx = new SignedXml ();
  359. XmlElement xel = sx.GetXml ();
  360. }
  361. [Test]
  362. [ExpectedException (typeof (ArgumentNullException))]
  363. public void LoadXml_Null ()
  364. {
  365. SignedXml sx = new SignedXml ();
  366. sx.LoadXml (null);
  367. }
  368. [Test]
  369. public void SigningKeyName ()
  370. {
  371. SignedXmlEx sxe = new SignedXmlEx ();
  372. Assert.IsNull (sxe.SigningKeyName, "SigningKeyName");
  373. sxe.SigningKeyName = "mono";
  374. Assert.AreEqual ("mono", sxe.SigningKeyName, "SigningKeyName");
  375. }
  376. [Test]
  377. public void CheckSignatureEmptySafe ()
  378. {
  379. SignedXml sx;
  380. KeyInfoClause kic;
  381. KeyInfo ki;
  382. // empty keyinfo passes...
  383. sx = new SignedXml ();
  384. sx.KeyInfo = new KeyInfo ();
  385. Assert.IsTrue (!sx.CheckSignature ());
  386. // with empty KeyInfoName
  387. kic = new KeyInfoName ();
  388. ki = new KeyInfo ();
  389. ki.AddClause (kic);
  390. sx.KeyInfo = ki;
  391. Assert.IsTrue (!sx.CheckSignature ());
  392. }
  393. [Test]
  394. #if !NET_2_0
  395. [ExpectedException (typeof (CryptographicException))]
  396. #endif
  397. public void CheckSignatureEmpty ()
  398. {
  399. SignedXml sx = new SignedXml ();
  400. Assert.IsTrue (!sx.CheckSignature ());
  401. }
  402. [Test]
  403. [ExpectedException (typeof (CryptographicException))]
  404. public void ComputeSignatureNoSigningKey ()
  405. {
  406. XmlDocument doc = new XmlDocument ();
  407. doc.LoadXml ("<foo/>");
  408. SignedXml signedXml = new SignedXml (doc);
  409. Reference reference = new Reference ();
  410. reference.Uri = "";
  411. XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform ();
  412. reference.AddTransform (env);
  413. signedXml.AddReference (reference);
  414. signedXml.ComputeSignature ();
  415. }
  416. [Test]
  417. [ExpectedException (typeof (CryptographicException))]
  418. public void ComputeSignatureMissingReferencedObject ()
  419. {
  420. XmlDocument doc = new XmlDocument ();
  421. doc.LoadXml ("<foo/>");
  422. SignedXml signedXml = new SignedXml (doc);
  423. DSA key = DSA.Create ();
  424. signedXml.SigningKey = key;
  425. Reference reference = new Reference ();
  426. reference.Uri = "#bleh";
  427. XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform ();
  428. reference.AddTransform (env);
  429. signedXml.AddReference (reference);
  430. signedXml.ComputeSignature ();
  431. }
  432. [Test]
  433. public void DataReferenceToNonDataObject ()
  434. {
  435. XmlDocument doc = new XmlDocument ();
  436. doc.LoadXml ("<foo Id='id:1'/>");
  437. SignedXml signedXml = new SignedXml (doc);
  438. DSA key = DSA.Create ();
  439. signedXml.SigningKey = key;
  440. Reference reference = new Reference ();
  441. reference.Uri = "#id:1";
  442. XmlDsigC14NTransform t = new XmlDsigC14NTransform ();
  443. reference.AddTransform (t);
  444. signedXml.AddReference (reference);
  445. signedXml.ComputeSignature ();
  446. }
  447. [Test]
  448. public void SignElementWithoutPrefixedNamespace ()
  449. {
  450. string input = "<Action xmlns='urn:foo'>http://tempuri.org/IFoo/Echo</Action>";
  451. string expected = @"<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=""#_1""><Transforms><Transform Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" /></Transforms><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>zdFEZB8rNzvpNG/gFEJBWk/M5Nk=</DigestValue></Reference></SignedInfo><SignatureValue>+OyGVzrHjmKSDWLNyvgx8pjbPfM=</SignatureValue><Object Id=""_1""><Action xmlns=""urn:foo"">http://tempuri.org/IFoo/Echo</Action></Object></Signature>";
  452. byte [] key = Convert.FromBase64String (
  453. "1W5EigVnbnRjGLbg99ElieOmuUgYO+KcwMJtE35SAGI=");
  454. Transform t = new XmlDsigC14NTransform ();
  455. Assert.AreEqual (expected, SignWithHMACSHA1 (input, key, t), "#1");
  456. // The second result should be still identical.
  457. Assert.AreEqual (expected, SignWithHMACSHA1 (input, key, t), "#2");
  458. }
  459. [Test]
  460. public void SignElementWithPrefixedNamespace ()
  461. {
  462. string input = "<a:Action xmlns:a='urn:foo'>http://tempuri.org/IFoo/Echo</a:Action>";
  463. string expected = @"<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=""#_1""><Transforms><Transform Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" /></Transforms><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>6i5FlqkEfJOdUaOMCK7xn0I0HDg=</DigestValue></Reference></SignedInfo><SignatureValue>tASp+e2A0xqcm02jKg5TGqlhKpI=</SignatureValue><Object Id=""_1""><a:Action xmlns:a=""urn:foo"">http://tempuri.org/IFoo/Echo</a:Action></Object></Signature>";
  464. byte [] key = Convert.FromBase64String (
  465. "1W5EigVnbnRjGLbg99ElieOmuUgYO+KcwMJtE35SAGI=");
  466. Assert.AreEqual (SignWithHMACSHA1 (input, key), expected);
  467. }
  468. string SignWithHMACSHA1 (string input, byte [] key)
  469. {
  470. return SignWithHMACSHA1 (input, key, new XmlDsigC14NTransform ());
  471. }
  472. string SignWithHMACSHA1 (string input, byte [] key, Transform transform)
  473. {
  474. XmlDocument doc = new XmlDocument ();
  475. doc.LoadXml (input);
  476. SignedXml sxml = new SignedXml (doc);
  477. HMACSHA1 keyhash = new HMACSHA1 (key);
  478. DataObject d = new DataObject ();
  479. //d.Data = doc.SelectNodes ("//*[local-name()='Body']/*");
  480. d.Data = doc.SelectNodes ("//*[local-name()='Action']");
  481. d.Id = "_1";
  482. sxml.AddObject (d);
  483. Reference r = new Reference ("#_1");
  484. r.AddTransform (transform);
  485. r.DigestMethod = SignedXml.XmlDsigSHA1Url;
  486. sxml.SignedInfo.AddReference (r);
  487. sxml.ComputeSignature (keyhash);
  488. StringWriter sw = new StringWriter ();
  489. XmlWriter w = new XmlTextWriter (sw);
  490. sxml.GetXml ().WriteTo (w);
  491. w.Close ();
  492. return sw.ToString ();
  493. }
  494. [Test]
  495. public void GetIdElement_Null ()
  496. {
  497. SignedXml sign = new SignedXml ();
  498. Assert.IsNull (sign.GetIdElement (null, "value"));
  499. Assert.IsNull (sign.GetIdElement (new XmlDocument (), null));
  500. }
  501. #if NET_2_0
  502. [Test]
  503. [Category ("NotWorking")] // bug #79483
  504. public void DigestValue_CRLF ()
  505. {
  506. XmlDocument doc = CreateSomeXml ("\r\n");
  507. XmlDsigExcC14NTransform transform = new XmlDsigExcC14NTransform ();
  508. transform.LoadInput (doc);
  509. Stream s = (Stream) transform.GetOutput ();
  510. string output = Stream2String (s);
  511. Assert.AreEqual ("<person>&#xD;\n <birthplace>Brussels</birthplace>&#xD;\n</person>", output, "#1");
  512. s.Position = 0;
  513. HashAlgorithm hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA1CryptoServiceProvider");
  514. byte[] digest = hash.ComputeHash (s);
  515. Assert.AreEqual ("IKbfdK2/DMfXyezCf5QggVCXfk8=", Convert.ToBase64String (digest), "#2");
  516. X509Certificate2 cert = new X509Certificate2 (_pkcs12, "mono");
  517. SignedXml signedXml = new SignedXml (doc);
  518. signedXml.SigningKey = cert.PrivateKey;
  519. signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;
  520. Reference reference = new Reference ();
  521. reference.Uri = "";
  522. XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform ();
  523. reference.AddTransform (env);
  524. signedXml.AddReference (reference);
  525. KeyInfo keyInfo = new KeyInfo ();
  526. KeyInfoX509Data x509KeyInfo = new KeyInfoX509Data ();
  527. x509KeyInfo.AddCertificate (new X509Certificate2 (_cert));
  528. x509KeyInfo.AddCertificate (cert);
  529. keyInfo.AddClause (x509KeyInfo);
  530. signedXml.KeyInfo = keyInfo;
  531. signedXml.ComputeSignature ();
  532. digest = reference.DigestValue;
  533. Assert.AreEqual ("e3dsi1xK8FAx1vsug7J203JbEAU=", Convert.ToBase64String (digest), "#3");
  534. Assert.AreEqual ("<SignedInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"
  535. + "<CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\" />"
  536. + "<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" />"
  537. + "<Reference URI=\"\">"
  538. + "<Transforms>"
  539. + "<Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" />"
  540. + "</Transforms>"
  541. + "<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" />"
  542. + "<DigestValue>e3dsi1xK8FAx1vsug7J203JbEAU=</DigestValue>"
  543. + "</Reference>"
  544. + "</SignedInfo>", signedXml.SignedInfo.GetXml ().OuterXml, "#4");
  545. }
  546. [Test]
  547. public void DigestValue_LF ()
  548. {
  549. XmlDocument doc = CreateSomeXml ("\n");
  550. XmlDsigExcC14NTransform transform = new XmlDsigExcC14NTransform ();
  551. transform.LoadInput (doc);
  552. Stream s = (Stream) transform.GetOutput ();
  553. string output = Stream2String (s);
  554. Assert.AreEqual ("<person>\n <birthplace>Brussels</birthplace>\n</person>", output, "#1");
  555. s.Position = 0;
  556. HashAlgorithm hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA1CryptoServiceProvider");
  557. byte[] digest = hash.ComputeHash (s);
  558. Assert.AreEqual ("e3dsi1xK8FAx1vsug7J203JbEAU=", Convert.ToBase64String (digest), "#2");
  559. X509Certificate2 cert = new X509Certificate2 (_pkcs12, "mono");
  560. SignedXml signedXml = new SignedXml (doc);
  561. signedXml.SigningKey = cert.PrivateKey;
  562. signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;
  563. Reference reference = new Reference ();
  564. reference.Uri = "";
  565. XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform ();
  566. reference.AddTransform (env);
  567. signedXml.AddReference (reference);
  568. KeyInfo keyInfo = new KeyInfo ();
  569. KeyInfoX509Data x509KeyInfo = new KeyInfoX509Data ();
  570. x509KeyInfo.AddCertificate (new X509Certificate2 (_cert));
  571. x509KeyInfo.AddCertificate (cert);
  572. keyInfo.AddClause (x509KeyInfo);
  573. signedXml.KeyInfo = keyInfo;
  574. signedXml.ComputeSignature ();
  575. digest = reference.DigestValue;
  576. Assert.AreEqual ("e3dsi1xK8FAx1vsug7J203JbEAU=", Convert.ToBase64String (digest), "#3");
  577. Assert.AreEqual ("<SignedInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"
  578. + "<CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\" />"
  579. + "<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" />"
  580. + "<Reference URI=\"\">"
  581. + "<Transforms>"
  582. + "<Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" />"
  583. + "</Transforms>"
  584. + "<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" />"
  585. + "<DigestValue>e3dsi1xK8FAx1vsug7J203JbEAU=</DigestValue>"
  586. + "</Reference>"
  587. + "</SignedInfo>", signedXml.SignedInfo.GetXml ().OuterXml, "#4");
  588. }
  589. [Test]
  590. [Category ("NotWorking")] // bug #79483
  591. public void SignedXML_CRLF_Invalid ()
  592. {
  593. X509Certificate2 cert = new X509Certificate2 (_pkcs12, "mono");
  594. XmlDocument doc = new XmlDocument ();
  595. doc.LoadXml (string.Format (CultureInfo.InvariantCulture,
  596. "<person>{0}" +
  597. " <birthplace>Brussels</birthplace>{0}" +
  598. "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" +
  599. "<SignedInfo>" +
  600. "<CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\" />" +
  601. "<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" />" +
  602. "<Reference URI=\"\">" +
  603. "<Transforms>" +
  604. "<Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" />" +
  605. "</Transforms>" +
  606. "<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" />" +
  607. "<DigestValue>IKbfdK2/DMfXyezCf5QggVCXfk8=</DigestValue>" +
  608. "</Reference>" +
  609. "</SignedInfo>" +
  610. "<SignatureValue>" +
  611. "JuSd68PyARsZqGKSo5xX5yYHDuu6whHEhoXqxxFmGeEdvkKY2bgroWJ1ZTGHGr" +
  612. "VI7mtG3h0w1ibOKdltm9j4lZaZWo87CAJiJ2syeLbMyIVSw6OyZEsiFF/VqLKK" +
  613. "4T4AO6q7HYsC55zJrOvL1j9IIr8zBnJfvBdKckf0lczYbXc=" +
  614. "</SignatureValue>" +
  615. "<KeyInfo>" +
  616. "<X509Data>" +
  617. "<X509Certificate>" +
  618. "MIIBozCCAQygAwIBAgIQHc+8iURSTUarmg4trmrnGTANBgkqhkiG9w0BAQUFAD" +
  619. "ARMQ8wDQYDVQQDEwZOb3ZlbGwwIBcNMDYwOTIxMDcyNjUxWhgPMjA5MDAxMjEw" +
  620. "ODI2NTFaMA8xDTALBgNVBAMTBE1vbm8wgZ0wDQYJKoZIhvcNAQEBBQADgYsAMI" +
  621. "GHAoGBAJhFB1KHv2WzsHqih9Mvm3KffEOSMv+sh1mPW3sWI/95VOOVqJnhemMM" +
  622. "s82phSbNZeoPHny4btbykbRRaRQv94rtIM6geJR1e2c5mfJWtHSq3EYQarHC68" +
  623. "cAZvCAmQZGa1eQRNRqcTSKX8yfqH0SouIE9ohJtpiluNe+Xgk5fKv3AgERMA0G" +
  624. "CSqGSIb3DQEBBQUAA4GBAE6pqSgK8QKRHSh6YvYs9oRh1n8iREco7QmZCFj7UB" +
  625. "kn/QgJ9mKsT8o12VnYHqBCEwBNaT1ay3z/SR4/Z383zuu4Y6xxjqOqnM6gtwUV" +
  626. "u5/0hvz+ThtuTjItG6Ny5JkLZZQt/XbI5kg920t9jq3vbHBMuX2HxivwQe5sug" +
  627. "jPaTEY" +
  628. "</X509Certificate>" +
  629. "<X509Certificate>" +
  630. "MIIBpTCCAQ6gAwIBAgIQXo6Lr3rrSkW4xmNPRbHMbjANBgkqhkiG9w0BAQUFAD" +
  631. "ARMQ8wDQYDVQQDEwZOb3ZlbGwwIBcNMDYwOTIxMDcxNDE4WhgPMjA5MDAxMjEw" +
  632. "ODE0MThaMBExDzANBgNVBAMTBk1pZ3VlbDCBnTANBgkqhkiG9w0BAQEFAAOBiw" +
  633. "AwgYcCgYEArCkeSZ6U3U3Fm2qSuQsM7xvvsSzZGQLPDUHFQ/BZxA7LiGRfXbmO" +
  634. "yPkkYRYItXdy0yDl/8rAjelaL8jQ4me6Uexyeq+5xEgHn9VbNJny5apGNi4kF1" +
  635. "8DR5DK9Zme9d6icusgW8krv3//5SVE8ao7X5qrIOGS825eCJL73YWbxKkCAREw" +
  636. "DQYJKoZIhvcNAQEFBQADgYEASqBgYTkIJpDO28ZEXnF5Q/G3xDR/MxhdcrCISJ" +
  637. "tDbuGVZzK+xhFhiYD5Q1NiGhD4oDIVJPwKmZH4L3YP96iSh6RdtO27V05ET/X5" +
  638. "yWMKdeIsq6r9jXXv7NaWTmvNfMLKLNgEBCJ00+wN0u4xHUC7yCJc0KNQ3fjDLU" +
  639. "AT1oaVjWI=" +
  640. "</X509Certificate>" +
  641. "</X509Data>" +
  642. "</KeyInfo>" +
  643. "</Signature>" +
  644. "</person>", "\r\n"));
  645. SignedXml signedXml = new SignedXml (doc);
  646. XmlNodeList nodeList = doc.GetElementsByTagName ("Signature");
  647. signedXml.LoadXml ((XmlElement) nodeList [0]);
  648. Assert.IsTrue (!signedXml.CheckSignature (), "#2");
  649. }
  650. [Test]
  651. [Category ("NotWorking")] // bug #79483
  652. public void SignedXML_CRLF_Valid ()
  653. {
  654. X509Certificate2 cert = new X509Certificate2 (_pkcs12, "mono");
  655. XmlDocument doc = CreateSignedXml (cert, SignedXml.XmlDsigExcC14NTransformUrl, "\r\n");
  656. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture, "<person>{0}" +
  657. " <birthplace>Brussels</birthplace>{0}" +
  658. "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" +
  659. "<SignedInfo>" +
  660. "<CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\" />" +
  661. "<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" />" +
  662. "<Reference URI=\"\">" +
  663. "<Transforms>" +
  664. "<Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" />" +
  665. "</Transforms>" +
  666. "<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" />" +
  667. "<DigestValue>e3dsi1xK8FAx1vsug7J203JbEAU=</DigestValue>" +
  668. "</Reference>" +
  669. "</SignedInfo>" +
  670. "<SignatureValue>" +
  671. "X29nbkOR/Xk3KwsEpEvpDOqfI6/NTtiewIxNqKMrPCoM0HLawK5HKsCw3lL07C" +
  672. "8SwqvoXJL9VS05gsSia85YCB8NPDeHuHY3CPGT7DVpgeHFA0oefMnOi8IAqKD2" +
  673. "nx29A222u5OmwbDO0qFqbtsgvIFiP5YJg04cwmnqs+eL+WA=" +
  674. "</SignatureValue>" +
  675. "<KeyInfo>" +
  676. "<X509Data>" +
  677. "<X509Certificate>" +
  678. "MIIBozCCAQygAwIBAgIQHc+8iURSTUarmg4trmrnGTANBgkqhkiG9w0BAQUFAD" +
  679. "ARMQ8wDQYDVQQDEwZOb3ZlbGwwIBcNMDYwOTIxMDcyNjUxWhgPMjA5MDAxMjEw" +
  680. "ODI2NTFaMA8xDTALBgNVBAMTBE1vbm8wgZ0wDQYJKoZIhvcNAQEBBQADgYsAMI" +
  681. "GHAoGBAJhFB1KHv2WzsHqih9Mvm3KffEOSMv+sh1mPW3sWI/95VOOVqJnhemMM" +
  682. "s82phSbNZeoPHny4btbykbRRaRQv94rtIM6geJR1e2c5mfJWtHSq3EYQarHC68" +
  683. "cAZvCAmQZGa1eQRNRqcTSKX8yfqH0SouIE9ohJtpiluNe+Xgk5fKv3AgERMA0G" +
  684. "CSqGSIb3DQEBBQUAA4GBAE6pqSgK8QKRHSh6YvYs9oRh1n8iREco7QmZCFj7UB" +
  685. "kn/QgJ9mKsT8o12VnYHqBCEwBNaT1ay3z/SR4/Z383zuu4Y6xxjqOqnM6gtwUV" +
  686. "u5/0hvz+ThtuTjItG6Ny5JkLZZQt/XbI5kg920t9jq3vbHBMuX2HxivwQe5sug" +
  687. "jPaTEY" +
  688. "</X509Certificate>" +
  689. "<X509Certificate>" +
  690. "MIIBpTCCAQ6gAwIBAgIQXo6Lr3rrSkW4xmNPRbHMbjANBgkqhkiG9w0BAQUFAD" +
  691. "ARMQ8wDQYDVQQDEwZOb3ZlbGwwIBcNMDYwOTIxMDcxNDE4WhgPMjA5MDAxMjEw" +
  692. "ODE0MThaMBExDzANBgNVBAMTBk1pZ3VlbDCBnTANBgkqhkiG9w0BAQEFAAOBiw" +
  693. "AwgYcCgYEArCkeSZ6U3U3Fm2qSuQsM7xvvsSzZGQLPDUHFQ/BZxA7LiGRfXbmO" +
  694. "yPkkYRYItXdy0yDl/8rAjelaL8jQ4me6Uexyeq+5xEgHn9VbNJny5apGNi4kF1" +
  695. "8DR5DK9Zme9d6icusgW8krv3//5SVE8ao7X5qrIOGS825eCJL73YWbxKkCAREw" +
  696. "DQYJKoZIhvcNAQEFBQADgYEASqBgYTkIJpDO28ZEXnF5Q/G3xDR/MxhdcrCISJ" +
  697. "tDbuGVZzK+xhFhiYD5Q1NiGhD4oDIVJPwKmZH4L3YP96iSh6RdtO27V05ET/X5" +
  698. "yWMKdeIsq6r9jXXv7NaWTmvNfMLKLNgEBCJ00+wN0u4xHUC7yCJc0KNQ3fjDLU" +
  699. "AT1oaVjWI=" +
  700. "</X509Certificate>" +
  701. "</X509Data>" +
  702. "</KeyInfo>" +
  703. "</Signature>" +
  704. "</person>", "\r\n"), doc.OuterXml, "#1");
  705. }
  706. [Test]
  707. [Ignore ("This is a bad test case which should basically just check the computed signature value instead of comparing XML document literal string, and thus caused inconsistency between .NET 1.1 and .NET 2.0. Not deleting this test case, to easily find the reason for potentially happening regression in the future (which should not waste time).")]
  708. public void SignedXML_LF_Valid ()
  709. {
  710. X509Certificate2 cert = new X509Certificate2 (_pkcs12, "mono");
  711. XmlDocument doc = CreateSignedXml (cert, SignedXml.XmlDsigExcC14NTransformUrl, "\n");
  712. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture, "<person>{0}" +
  713. " <birthplace>Brussels</birthplace>{0}" +
  714. "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" +
  715. "<SignedInfo>" +
  716. "<CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\" />" +
  717. "<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" />" +
  718. "<Reference URI=\"\">" +
  719. "<Transforms>" +
  720. "<Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" />" +
  721. "</Transforms>" +
  722. "<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" />" +
  723. "<DigestValue>e3dsi1xK8FAx1vsug7J203JbEAU=</DigestValue>" +
  724. "</Reference>" +
  725. "</SignedInfo>" +
  726. "<SignatureValue>" +
  727. "X29nbkOR/Xk3KwsEpEvpDOqfI6/NTtiewIxNqKMrPCoM0HLawK5HKsCw3lL07C" +
  728. "8SwqvoXJL9VS05gsSia85YCB8NPDeHuHY3CPGT7DVpgeHFA0oefMnOi8IAqKD2" +
  729. "nx29A222u5OmwbDO0qFqbtsgvIFiP5YJg04cwmnqs+eL+WA=" +
  730. "</SignatureValue>" +
  731. "<KeyInfo>" +
  732. "<X509Data>" +
  733. "<X509Certificate>" +
  734. "MIIBozCCAQygAwIBAgIQHc+8iURSTUarmg4trmrnGTANBgkqhkiG9w0BAQUFAD" +
  735. "ARMQ8wDQYDVQQDEwZOb3ZlbGwwIBcNMDYwOTIxMDcyNjUxWhgPMjA5MDAxMjEw" +
  736. "ODI2NTFaMA8xDTALBgNVBAMTBE1vbm8wgZ0wDQYJKoZIhvcNAQEBBQADgYsAMI" +
  737. "GHAoGBAJhFB1KHv2WzsHqih9Mvm3KffEOSMv+sh1mPW3sWI/95VOOVqJnhemMM" +
  738. "s82phSbNZeoPHny4btbykbRRaRQv94rtIM6geJR1e2c5mfJWtHSq3EYQarHC68" +
  739. "cAZvCAmQZGa1eQRNRqcTSKX8yfqH0SouIE9ohJtpiluNe+Xgk5fKv3AgERMA0G" +
  740. "CSqGSIb3DQEBBQUAA4GBAE6pqSgK8QKRHSh6YvYs9oRh1n8iREco7QmZCFj7UB" +
  741. "kn/QgJ9mKsT8o12VnYHqBCEwBNaT1ay3z/SR4/Z383zuu4Y6xxjqOqnM6gtwUV" +
  742. "u5/0hvz+ThtuTjItG6Ny5JkLZZQt/XbI5kg920t9jq3vbHBMuX2HxivwQe5sug" +
  743. "jPaTEY" +
  744. "</X509Certificate>" +
  745. "<X509Certificate>" +
  746. "MIIBpTCCAQ6gAwIBAgIQXo6Lr3rrSkW4xmNPRbHMbjANBgkqhkiG9w0BAQUFAD" +
  747. "ARMQ8wDQYDVQQDEwZOb3ZlbGwwIBcNMDYwOTIxMDcxNDE4WhgPMjA5MDAxMjEw" +
  748. "ODE0MThaMBExDzANBgNVBAMTBk1pZ3VlbDCBnTANBgkqhkiG9w0BAQEFAAOBiw" +
  749. "AwgYcCgYEArCkeSZ6U3U3Fm2qSuQsM7xvvsSzZGQLPDUHFQ/BZxA7LiGRfXbmO" +
  750. "yPkkYRYItXdy0yDl/8rAjelaL8jQ4me6Uexyeq+5xEgHn9VbNJny5apGNi4kF1" +
  751. "8DR5DK9Zme9d6icusgW8krv3//5SVE8ao7X5qrIOGS825eCJL73YWbxKkCAREw" +
  752. "DQYJKoZIhvcNAQEFBQADgYEASqBgYTkIJpDO28ZEXnF5Q/G3xDR/MxhdcrCISJ" +
  753. "tDbuGVZzK+xhFhiYD5Q1NiGhD4oDIVJPwKmZH4L3YP96iSh6RdtO27V05ET/X5" +
  754. "yWMKdeIsq6r9jXXv7NaWTmvNfMLKLNgEBCJ00+wN0u4xHUC7yCJc0KNQ3fjDLU" +
  755. "AT1oaVjWI=" +
  756. "</X509Certificate>" +
  757. "</X509Data>" +
  758. "</KeyInfo>" +
  759. "</Signature>" +
  760. "</person>", "\n"), doc.OuterXml, "#1");
  761. }
  762. [Test] // part of bug #79454
  763. public void MultipleX509Certificates ()
  764. {
  765. XmlDocument doc = null;
  766. X509Certificate2 cert = new X509Certificate2 (_pkcs12, "mono");
  767. doc = CreateSignedXml (cert, SignedXml.XmlDsigExcC14NTransformUrl, "\n");
  768. Assert.IsTrue (VerifySignedXml (doc), "#1");
  769. doc = CreateSignedXml (cert, SignedXml.XmlDsigExcC14NWithCommentsTransformUrl, "\n");
  770. Assert.IsTrue (VerifySignedXml (doc), "#2");
  771. doc = CreateSignedXml (cert, SignedXml.XmlDsigCanonicalizationUrl, "\n");
  772. Assert.IsTrue (VerifySignedXml (doc), "#3");
  773. doc = CreateSignedXml (cert, SignedXml.XmlDsigCanonicalizationWithCommentsUrl, "\n");
  774. Assert.IsTrue (VerifySignedXml (doc), "#4");
  775. }
  776. // creates a signed XML document with two certificates in the X509Data
  777. // element, with the second being the one that should be used to verify
  778. // the signature
  779. static XmlDocument CreateSignedXml (X509Certificate2 cert, string canonicalizationMethod, string lineFeed)
  780. {
  781. XmlDocument doc = CreateSomeXml (lineFeed);
  782. SignedXml signedXml = new SignedXml (doc);
  783. signedXml.SigningKey = cert.PrivateKey;
  784. signedXml.SignedInfo.CanonicalizationMethod = canonicalizationMethod;
  785. Reference reference = new Reference ();
  786. reference.Uri = "";
  787. XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform ();
  788. reference.AddTransform (env);
  789. signedXml.AddReference (reference);
  790. KeyInfo keyInfo = new KeyInfo ();
  791. KeyInfoX509Data x509KeyInfo = new KeyInfoX509Data ();
  792. x509KeyInfo.AddCertificate (new X509Certificate2 (_cert));
  793. x509KeyInfo.AddCertificate (cert);
  794. keyInfo.AddClause (x509KeyInfo);
  795. signedXml.KeyInfo = keyInfo;
  796. signedXml.ComputeSignature ();
  797. XmlElement xmlDigitalSignature = signedXml.GetXml ();
  798. doc.DocumentElement.AppendChild (doc.ImportNode (xmlDigitalSignature, true));
  799. return doc;
  800. }
  801. static bool VerifySignedXml (XmlDocument signedDoc)
  802. {
  803. SignedXml signedXml = new SignedXml (signedDoc);
  804. XmlNodeList nodeList = signedDoc.GetElementsByTagName ("Signature");
  805. signedXml.LoadXml ((XmlElement) nodeList [0]);
  806. return signedXml.CheckSignature ();
  807. }
  808. static XmlDocument CreateSomeXml (string lineFeed)
  809. {
  810. StringWriter sw = new StringWriter ();
  811. sw.NewLine = lineFeed;
  812. XmlTextWriter xtw = new XmlTextWriter (sw);
  813. xtw.Formatting = Formatting.Indented;
  814. xtw.WriteStartElement ("person");
  815. xtw.WriteElementString ("birthplace", "Brussels");
  816. xtw.WriteEndElement ();
  817. xtw.Flush ();
  818. XmlDocument doc = new XmlDocument ();
  819. doc.PreserveWhitespace = true;
  820. doc.Load (new StringReader (sw.ToString ()));
  821. return doc;
  822. }
  823. string Stream2String (Stream s)
  824. {
  825. StringBuilder sb = new StringBuilder ();
  826. int b = s.ReadByte ();
  827. while (b != -1) {
  828. sb.Append (Convert.ToChar (b));
  829. b = s.ReadByte ();
  830. }
  831. return sb.ToString ();
  832. }
  833. private static byte [] _cert = new byte [] {
  834. 0x30, 0x82, 0x01, 0xa3, 0x30, 0x82, 0x01, 0x0c, 0xa0, 0x03, 0x02,
  835. 0x01, 0x02, 0x02, 0x10, 0x1d, 0xcf, 0xbc, 0x89, 0x44, 0x52, 0x4d,
  836. 0x46, 0xab, 0x9a, 0x0e, 0x2d, 0xae, 0x6a, 0xe7, 0x19, 0x30, 0x0d,
  837. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05,
  838. 0x05, 0x00, 0x30, 0x11, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
  839. 0x04, 0x03, 0x13, 0x06, 0x4e, 0x6f, 0x76, 0x65, 0x6c, 0x6c, 0x30,
  840. 0x20, 0x17, 0x0d, 0x30, 0x36, 0x30, 0x39, 0x32, 0x31, 0x30, 0x37,
  841. 0x32, 0x36, 0x35, 0x31, 0x5a, 0x18, 0x0f, 0x32, 0x30, 0x39, 0x30,
  842. 0x30, 0x31, 0x32, 0x31, 0x30, 0x38, 0x32, 0x36, 0x35, 0x31, 0x5a,
  843. 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03,
  844. 0x13, 0x04, 0x4d, 0x6f, 0x6e, 0x6f, 0x30, 0x81, 0x9d, 0x30, 0x0d,
  845. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  846. 0x05, 0x00, 0x03, 0x81, 0x8b, 0x00, 0x30, 0x81, 0x87, 0x02, 0x81,
  847. 0x81, 0x00, 0x98, 0x45, 0x07, 0x52, 0x87, 0xbf, 0x65, 0xb3, 0xb0,
  848. 0x7a, 0xa2, 0x87, 0xd3, 0x2f, 0x9b, 0x72, 0x9f, 0x7c, 0x43, 0x92,
  849. 0x32, 0xff, 0xac, 0x87, 0x59, 0x8f, 0x5b, 0x7b, 0x16, 0x23, 0xff,
  850. 0x79, 0x54, 0xe3, 0x95, 0xa8, 0x99, 0xe1, 0x7a, 0x63, 0x0c, 0xb3,
  851. 0xcd, 0xa9, 0x85, 0x26, 0xcd, 0x65, 0xea, 0x0f, 0x1e, 0x7c, 0xb8,
  852. 0x6e, 0xd6, 0xf2, 0x91, 0xb4, 0x51, 0x69, 0x14, 0x2f, 0xf7, 0x8a,
  853. 0xed, 0x20, 0xce, 0xa0, 0x78, 0x94, 0x75, 0x7b, 0x67, 0x39, 0x99,
  854. 0xf2, 0x56, 0xb4, 0x74, 0xaa, 0xdc, 0x46, 0x10, 0x6a, 0xb1, 0xc2,
  855. 0xeb, 0xc7, 0x00, 0x66, 0xf0, 0x80, 0x99, 0x06, 0x46, 0x6b, 0x57,
  856. 0x90, 0x44, 0xd4, 0x6a, 0x71, 0x34, 0x8a, 0x5f, 0xcc, 0x9f, 0xa8,
  857. 0x7d, 0x12, 0xa2, 0xe2, 0x04, 0xf6, 0x88, 0x49, 0xb6, 0x98, 0xa5,
  858. 0xb8, 0xd7, 0xbe, 0x5e, 0x09, 0x39, 0x7c, 0xab, 0xf7, 0x02, 0x01,
  859. 0x11, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  860. 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x81, 0x81, 0x00, 0x4e, 0xa9,
  861. 0xa9, 0x28, 0x0a, 0xf1, 0x02, 0x91, 0x1d, 0x28, 0x7a, 0x62, 0xf6,
  862. 0x2c, 0xf6, 0x84, 0x61, 0xd6, 0x7f, 0x22, 0x44, 0x47, 0x28, 0xed,
  863. 0x09, 0x99, 0x08, 0x58, 0xfb, 0x50, 0x19, 0x27, 0xfd, 0x08, 0x09,
  864. 0xf6, 0x62, 0xac, 0x4f, 0xca, 0x35, 0xd9, 0x59, 0xd8, 0x1e, 0xa0,
  865. 0x42, 0x13, 0x00, 0x4d, 0x69, 0x3d, 0x5a, 0xcb, 0x7c, 0xff, 0x49,
  866. 0x1e, 0x3f, 0x67, 0x7f, 0x37, 0xce, 0xeb, 0xb8, 0x63, 0xac, 0x71,
  867. 0x8e, 0xa3, 0xaa, 0x9c, 0xce, 0xa0, 0xb7, 0x05, 0x15, 0xbb, 0x9f,
  868. 0xf4, 0x86, 0xfc, 0xfe, 0x4e, 0x1b, 0x6e, 0x4e, 0x32, 0x2d, 0x1b,
  869. 0xa3, 0x72, 0xe4, 0x99, 0x0b, 0x65, 0x94, 0x2d, 0xfd, 0x76, 0xc8,
  870. 0xe6, 0x48, 0x3d, 0xdb, 0x4b, 0x7d, 0x8e, 0xad, 0xef, 0x6c, 0x70,
  871. 0x4c, 0xb9, 0x7d, 0x87, 0xc6, 0x2b, 0xf0, 0x41, 0xee, 0x6c, 0xba,
  872. 0x08, 0xcf, 0x69, 0x31, 0x18 };
  873. private static byte [] _pkcs12 = new byte [] {
  874. 0x30, 0x82, 0x05, 0x8d, 0x02, 0x01, 0x03, 0x30, 0x82, 0x05, 0x47,
  875. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01,
  876. 0xa0, 0x82, 0x05, 0x38, 0x04, 0x82, 0x05, 0x34, 0x30, 0x82, 0x05,
  877. 0x30, 0x30, 0x82, 0x02, 0x3f, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  878. 0xf7, 0x0d, 0x01, 0x07, 0x06, 0xa0, 0x82, 0x02, 0x30, 0x30, 0x82,
  879. 0x02, 0x2c, 0x02, 0x01, 0x00, 0x30, 0x82, 0x02, 0x25, 0x06, 0x09,
  880. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30, 0x1c,
  881. 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01,
  882. 0x03, 0x30, 0x0e, 0x04, 0x08, 0x6e, 0x0a, 0x50, 0x20, 0xc3, 0x11,
  883. 0x49, 0x07, 0x02, 0x02, 0x07, 0xd0, 0x80, 0x82, 0x01, 0xf8, 0x74,
  884. 0x40, 0x07, 0x44, 0x6b, 0x80, 0x46, 0xe1, 0x4e, 0x65, 0x5e, 0xf2,
  885. 0xf6, 0x38, 0x90, 0xd1, 0x75, 0x24, 0xd9, 0x72, 0x92, 0x5b, 0x4a,
  886. 0xb9, 0x9e, 0xbd, 0xab, 0xe2, 0xb8, 0x91, 0xc9, 0x48, 0x14, 0x88,
  887. 0x61, 0x7d, 0x06, 0xf9, 0x24, 0x80, 0xb5, 0x36, 0xaf, 0xfe, 0xc0,
  888. 0x59, 0x00, 0x39, 0x3f, 0x78, 0xc0, 0x57, 0xea, 0x1e, 0xcb, 0x29,
  889. 0xa4, 0x5f, 0xba, 0x4b, 0xd9, 0xca, 0x95, 0xab, 0x55, 0x4a, 0x11,
  890. 0x1a, 0xf8, 0xe9, 0xd4, 0xc0, 0x08, 0x55, 0xfb, 0x69, 0x09, 0x0d,
  891. 0x5b, 0xed, 0x02, 0xcc, 0x55, 0xfe, 0x05, 0x2e, 0x45, 0xa7, 0x8d,
  892. 0x63, 0x9a, 0xda, 0x6c, 0xc7, 0xe1, 0xcb, 0x5c, 0xa7, 0xd9, 0x9b,
  893. 0x4a, 0xfb, 0x7d, 0x31, 0xe5, 0x89, 0x3e, 0xf2, 0x32, 0xc9, 0x78,
  894. 0xd0, 0x66, 0x1e, 0x38, 0xc7, 0xbf, 0x41, 0xf9, 0xe7, 0xbd, 0xce,
  895. 0x8b, 0xc3, 0x14, 0x19, 0x4b, 0xfa, 0x3a, 0xa2, 0x1f, 0xb0, 0xd4,
  896. 0xfa, 0x33, 0x39, 0x12, 0xd9, 0x36, 0x7f, 0x7e, 0xf0, 0xc4, 0xdc,
  897. 0xf0, 0xb5, 0x7a, 0x50, 0x2c, 0x99, 0x9d, 0x02, 0x40, 0xec, 0x6a,
  898. 0x23, 0x83, 0x16, 0xec, 0x8f, 0x58, 0x14, 0xa0, 0xa0, 0x9c, 0xa0,
  899. 0xe1, 0xd0, 0x6f, 0x54, 0x1a, 0x10, 0x47, 0x69, 0x6b, 0x55, 0x7f,
  900. 0x67, 0x7d, 0xb8, 0x38, 0xa0, 0x40, 0x99, 0x13, 0xe8, 0x15, 0x73,
  901. 0x8d, 0x18, 0x86, 0x29, 0x74, 0xec, 0x66, 0xa3, 0xb8, 0x14, 0x10,
  902. 0x61, 0xef, 0xa5, 0x79, 0x89, 0x01, 0xaa, 0xf2, 0x1f, 0x0c, 0xdd,
  903. 0x0d, 0x8c, 0xbb, 0x7a, 0x4e, 0x0f, 0x47, 0x91, 0x37, 0xa3, 0x8a,
  904. 0x43, 0x0f, 0xeb, 0xc7, 0x9b, 0x8d, 0xaf, 0x39, 0xdf, 0x23, 0x1c,
  905. 0xa4, 0xf7, 0x66, 0x1c, 0x61, 0x42, 0x24, 0x9a, 0x0a, 0x3a, 0x31,
  906. 0x9c, 0x51, 0xa2, 0x30, 0xbe, 0x85, 0xa6, 0xe8, 0x18, 0xfa, 0x8b,
  907. 0xff, 0xdd, 0xdc, 0x34, 0x46, 0x4f, 0x15, 0xde, 0xdb, 0xc4, 0xeb,
  908. 0x62, 0x3b, 0x7c, 0x25, 0x1a, 0x13, 0x8b, 0xda, 0x3b, 0x59, 0x2a,
  909. 0xb8, 0x50, 0xe3, 0x9f, 0x76, 0xfc, 0xe8, 0x00, 0xfc, 0xf7, 0xba,
  910. 0xd2, 0x45, 0x92, 0x14, 0xb5, 0xe2, 0x93, 0x41, 0x09, 0xea, 0x5b,
  911. 0x5e, 0xda, 0x66, 0x92, 0xd1, 0x93, 0x7a, 0xc0, 0xe1, 0x2f, 0xed,
  912. 0x29, 0x78, 0x80, 0xff, 0x79, 0x0e, 0xda, 0x78, 0x7e, 0x71, 0xa4,
  913. 0x31, 0x2f, 0xe9, 0x48, 0xab, 0xc9, 0x40, 0x7d, 0x63, 0x06, 0xd6,
  914. 0xb5, 0x2b, 0x49, 0xba, 0x43, 0x56, 0x69, 0xc5, 0xc2, 0x85, 0x37,
  915. 0xdb, 0xe7, 0x39, 0x87, 0x8d, 0x14, 0x15, 0x55, 0x76, 0x3f, 0x70,
  916. 0xf6, 0xd7, 0x80, 0x82, 0x48, 0x02, 0x64, 0xe1, 0x73, 0x1a, 0xd9,
  917. 0x35, 0x1a, 0x43, 0xf3, 0xde, 0xd4, 0x00, 0x9d, 0x49, 0x2b, 0xc6,
  918. 0x66, 0x19, 0x3e, 0xb8, 0xcc, 0x43, 0xcc, 0xa8, 0x12, 0xa4, 0xad,
  919. 0xcd, 0xe2, 0xe6, 0xb3, 0xdd, 0x7e, 0x80, 0x50, 0xc0, 0xb4, 0x0c,
  920. 0x4c, 0xd2, 0x31, 0xf3, 0xf8, 0x49, 0x31, 0xbe, 0xf2, 0x7d, 0x60,
  921. 0x38, 0xe0, 0x60, 0xdf, 0x7b, 0x58, 0xe0, 0xf9, 0x6e, 0x68, 0x79,
  922. 0x33, 0xb2, 0x2a, 0x53, 0x4c, 0x5a, 0x9d, 0xb3, 0x81, 0x4b, 0x19,
  923. 0x21, 0xe2, 0x3a, 0x42, 0x07, 0x25, 0x5a, 0xee, 0x1f, 0x5d, 0xa2,
  924. 0xca, 0xf7, 0x2f, 0x3c, 0x9b, 0xb0, 0xbc, 0xe7, 0xaf, 0x8c, 0x2f,
  925. 0x52, 0x43, 0x79, 0x94, 0xb0, 0xee, 0xc4, 0x53, 0x09, 0xc0, 0xc9,
  926. 0x21, 0x39, 0x64, 0x82, 0xc3, 0x54, 0xb8, 0x65, 0xf8, 0xdc, 0xb3,
  927. 0xdf, 0x4d, 0xc4, 0x63, 0x59, 0x14, 0x37, 0xd6, 0xba, 0xa3, 0x98,
  928. 0xda, 0x99, 0x02, 0xdd, 0x7a, 0x87, 0x3e, 0x34, 0xb5, 0x4b, 0x0a,
  929. 0xb4, 0x2d, 0xea, 0x19, 0x24, 0xd1, 0xc2, 0x9f, 0x30, 0x82, 0x02,
  930. 0xe9, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07,
  931. 0x01, 0xa0, 0x82, 0x02, 0xda, 0x04, 0x82, 0x02, 0xd6, 0x30, 0x82,
  932. 0x02, 0xd2, 0x30, 0x82, 0x02, 0xce, 0x06, 0x0b, 0x2a, 0x86, 0x48,
  933. 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x0a, 0x01, 0x02, 0xa0, 0x82, 0x02,
  934. 0xa6, 0x30, 0x82, 0x02, 0xa2, 0x30, 0x1c, 0x06, 0x0a, 0x2a, 0x86,
  935. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x0e, 0x04,
  936. 0x08, 0xe0, 0x21, 0x4f, 0x90, 0x7d, 0x86, 0x72, 0xc7, 0x02, 0x02,
  937. 0x07, 0xd0, 0x04, 0x82, 0x02, 0x80, 0x92, 0xac, 0xe8, 0x52, 0xa6,
  938. 0x3e, 0xed, 0x3d, 0xbc, 0x28, 0x5f, 0xb9, 0x45, 0x76, 0x27, 0x95,
  939. 0xf8, 0x6a, 0xc5, 0x17, 0x97, 0x46, 0x58, 0xe9, 0x15, 0x7c, 0x68,
  940. 0x62, 0x67, 0xb5, 0x2f, 0x1b, 0x64, 0x27, 0x9d, 0xfd, 0x67, 0x66,
  941. 0x42, 0x21, 0x5c, 0xf4, 0x64, 0x37, 0xcc, 0xc0, 0x04, 0x01, 0x91,
  942. 0x6c, 0x6b, 0x84, 0x96, 0xae, 0x04, 0xfe, 0xcc, 0x88, 0x6a, 0x84,
  943. 0xd7, 0x59, 0x28, 0x78, 0xc9, 0xb4, 0xf6, 0x4d, 0x86, 0x8d, 0x59,
  944. 0xc6, 0x74, 0x30, 0xca, 0x2f, 0x0a, 0xa7, 0x66, 0x99, 0xf4, 0x8f,
  945. 0x44, 0x6d, 0x97, 0x3c, 0xd6, 0xdb, 0xd6, 0x31, 0x8c, 0xf7, 0x75,
  946. 0xd9, 0x0b, 0xf5, 0xd2, 0x27, 0x80, 0x81, 0x28, 0x0f, 0x6b, 0x8b,
  947. 0x45, 0x11, 0x08, 0x1d, 0x06, 0x31, 0x4d, 0x98, 0x68, 0xc9, 0x09,
  948. 0x9b, 0x51, 0x84, 0x81, 0x74, 0x76, 0x57, 0x63, 0xb5, 0x38, 0xc8,
  949. 0xe1, 0x96, 0xe4, 0xcd, 0xd4, 0xe8, 0xf8, 0x26, 0x88, 0x88, 0xaa,
  950. 0xdf, 0x1b, 0xc6, 0x37, 0xb8, 0xc4, 0xe1, 0xcb, 0xc0, 0x71, 0x3d,
  951. 0xd6, 0xd7, 0x8b, 0xc6, 0xec, 0x5f, 0x42, 0x86, 0xb0, 0x8d, 0x1c,
  952. 0x49, 0xb9, 0xc6, 0x96, 0x11, 0xa5, 0xd6, 0xd2, 0xc0, 0x18, 0xca,
  953. 0xe7, 0xf6, 0x93, 0xb4, 0xf5, 0x7a, 0xe4, 0xec, 0xa2, 0x90, 0xf8,
  954. 0xef, 0x66, 0x0f, 0xa8, 0x52, 0x0c, 0x3f, 0x85, 0x4a, 0x76, 0x3a,
  955. 0xb8, 0x5a, 0x2d, 0x03, 0x5d, 0x99, 0x70, 0xbb, 0x02, 0x1c, 0x77,
  956. 0x43, 0x12, 0xd9, 0x1f, 0x7c, 0x6f, 0x69, 0x15, 0x17, 0x30, 0x51,
  957. 0x7d, 0x53, 0xc2, 0x06, 0xe0, 0xd2, 0x31, 0x17, 0x2a, 0x98, 0xe3,
  958. 0xe0, 0x20, 0xfb, 0x01, 0xfd, 0xd1, 0x1b, 0x50, 0x00, 0xad, 0x1d,
  959. 0xff, 0xa1, 0xae, 0xd6, 0xac, 0x38, 0x8b, 0x71, 0x28, 0x44, 0x66,
  960. 0x8c, 0xb6, 0x34, 0xc5, 0x86, 0xc9, 0x34, 0xda, 0x6c, 0x2a, 0xef,
  961. 0x69, 0x3c, 0xb7, 0xbd, 0xa5, 0x05, 0x3c, 0x7c, 0xfb, 0x0c, 0x2d,
  962. 0x49, 0x09, 0xdb, 0x91, 0x3b, 0x41, 0x2a, 0xe4, 0xfa, 0x4a, 0xc2,
  963. 0xea, 0x9e, 0x6f, 0xc3, 0x46, 0x2a, 0x77, 0x83, 0x4e, 0x22, 0x01,
  964. 0xfb, 0x0c, 0x2d, 0x5a, 0xcf, 0x8d, 0xa7, 0x55, 0x24, 0x7c, 0xda,
  965. 0x9e, 0xd8, 0xbc, 0xf6, 0x81, 0x63, 0x8a, 0x36, 0xd0, 0x13, 0x74,
  966. 0x30, 0x4d, 0xd8, 0x4e, 0xa6, 0x81, 0x71, 0x71, 0xff, 0x9f, 0xf3,
  967. 0x8d, 0x75, 0xad, 0x6b, 0x93, 0x93, 0x8c, 0xf8, 0x7d, 0xa6, 0x62,
  968. 0x9d, 0xf7, 0x86, 0x6f, 0xcb, 0x5b, 0x6f, 0xe5, 0xee, 0xcd, 0xb0,
  969. 0xb2, 0xfd, 0x96, 0x2c, 0xde, 0xa0, 0xcf, 0x46, 0x8c, 0x66, 0x0e,
  970. 0xf9, 0xa3, 0xdb, 0xfa, 0x8f, 0x1b, 0x54, 0x9d, 0x13, 0x13, 0x6b,
  971. 0x97, 0x43, 0x97, 0x64, 0xec, 0x2a, 0xc5, 0xc0, 0x26, 0xab, 0xea,
  972. 0x37, 0xd6, 0xcb, 0xb9, 0x83, 0x18, 0x53, 0x5a, 0xcd, 0x28, 0xb3,
  973. 0x3b, 0x9c, 0x13, 0xaa, 0x78, 0x6c, 0xcf, 0xe9, 0x75, 0x7c, 0x80,
  974. 0x04, 0x05, 0x52, 0xda, 0x13, 0x41, 0xb0, 0x27, 0x0f, 0x82, 0xa3,
  975. 0x81, 0xd8, 0xf7, 0xdc, 0x61, 0xbb, 0x98, 0x32, 0x5a, 0x88, 0xbf,
  976. 0x49, 0xc1, 0x76, 0x83, 0xcd, 0xc4, 0xb4, 0xca, 0x8d, 0x36, 0x88,
  977. 0xee, 0xdb, 0xc5, 0xf4, 0x13, 0x28, 0x4d, 0xae, 0x7a, 0x31, 0x3e,
  978. 0x77, 0x19, 0xab, 0x11, 0x15, 0x29, 0xd4, 0xcf, 0xb4, 0x73, 0x36,
  979. 0x92, 0x1e, 0x4e, 0x5d, 0x35, 0x57, 0x84, 0x45, 0x9d, 0x05, 0x3c,
  980. 0x44, 0x86, 0x08, 0x0b, 0x90, 0x29, 0xf9, 0xe6, 0x48, 0xaf, 0xf4,
  981. 0x62, 0xd2, 0x4d, 0x32, 0x1a, 0xe9, 0xbf, 0x3a, 0x7b, 0x25, 0x4a,
  982. 0x03, 0xfb, 0x40, 0x1d, 0x71, 0x2c, 0x10, 0x54, 0xdc, 0xbf, 0xf4,
  983. 0x50, 0x85, 0x15, 0x11, 0xb1, 0x2d, 0x03, 0x2c, 0xe4, 0x8a, 0xce,
  984. 0xec, 0x6e, 0x46, 0x06, 0x13, 0x3c, 0x97, 0x8d, 0xdd, 0xf6, 0x1e,
  985. 0x62, 0xb4, 0x8d, 0xfa, 0x2c, 0x86, 0x87, 0x64, 0x5e, 0xec, 0xc8,
  986. 0x84, 0xd1, 0x3d, 0xc5, 0x76, 0x4a, 0x31, 0xd3, 0xdb, 0x34, 0x6e,
  987. 0x8a, 0x49, 0xd6, 0x38, 0xbb, 0x05, 0xe9, 0x4d, 0xf1, 0xde, 0x3e,
  988. 0xa4, 0x47, 0xdd, 0xe8, 0xa8, 0xf1, 0xba, 0x55, 0xce, 0xca, 0x5b,
  989. 0x57, 0xd7, 0xc8, 0x9f, 0x09, 0xa3, 0x8e, 0x58, 0x83, 0x21, 0x0a,
  990. 0x6e, 0xd3, 0x70, 0x9c, 0xb9, 0x7c, 0x52, 0x98, 0x53, 0xcb, 0xda,
  991. 0x9d, 0xaf, 0xb7, 0x4b, 0xf7, 0x48, 0x91, 0x7e, 0x78, 0x20, 0x19,
  992. 0xe3, 0x41, 0x9d, 0xc8, 0x68, 0x11, 0xfb, 0x5f, 0x6b, 0xc8, 0x09,
  993. 0x74, 0xcb, 0x76, 0x08, 0xbc, 0x28, 0x63, 0x57, 0x04, 0xb0, 0x80,
  994. 0xd1, 0x53, 0x60, 0x50, 0x44, 0xba, 0x80, 0x48, 0x5e, 0x0e, 0x9a,
  995. 0xe5, 0x64, 0x26, 0x7a, 0x88, 0xb9, 0xc6, 0x33, 0x31, 0x15, 0x30,
  996. 0x13, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09,
  997. 0x15, 0x31, 0x06, 0x04, 0x04, 0x01, 0x00, 0x00, 0x00, 0x30, 0x3d,
  998. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a,
  999. 0x05, 0x00, 0x04, 0x14, 0x32, 0x55, 0x07, 0xa2, 0x67, 0xf3, 0x76,
  1000. 0x4d, 0x0b, 0x6f, 0xa4, 0xa0, 0x7b, 0xce, 0x2f, 0xc5, 0xff, 0xbe,
  1001. 0x3e, 0x38, 0x04, 0x14, 0x52, 0xf8, 0xb3, 0xeb, 0xc3, 0xda, 0x79,
  1002. 0xfa, 0x75, 0x89, 0x67, 0x33, 0x01, 0xd0, 0xb0, 0x13, 0xfa, 0x11,
  1003. 0x94, 0xac, 0x02, 0x02, 0x07, 0xd0 };
  1004. public SignedXml SignHMAC (string uri, KeyedHashAlgorithm mac, bool ok)
  1005. {
  1006. string input = "<foo/>";
  1007. XmlDocument doc = new XmlDocument ();
  1008. doc.LoadXml (input);
  1009. SignedXml sig = new SignedXml (doc);
  1010. Reference r = new Reference ("");
  1011. r.AddTransform (new XmlDsigEnvelopedSignatureTransform ());
  1012. r.DigestMethod = uri;
  1013. sig.AddReference (r);
  1014. sig.ComputeSignature (mac);
  1015. doc.DocumentElement.AppendChild (doc.ImportNode (sig.GetXml (), true));
  1016. // doc.Save (System.Console.Out);
  1017. sig.LoadXml (doc.DocumentElement ["Signature"]);
  1018. Assert.AreEqual (ok, sig.CheckSignature (mac), "CheckSignature");
  1019. return sig;
  1020. }
  1021. static byte [] hmackey = new byte [0];
  1022. private const string more256 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256";
  1023. private const string more384 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha384";
  1024. private const string more512 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha512";
  1025. private const string moreripe = "http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160";
  1026. [Test]
  1027. public void SignHMAC_SHA256 ()
  1028. {
  1029. SignedXml sign = SignHMAC (EncryptedXml.XmlEncSHA256Url, new HMACSHA256 (hmackey), true);
  1030. Assert.AreEqual (more256, sign.SignatureMethod, "SignatureMethod");
  1031. }
  1032. [Test]
  1033. public void SignHMAC_SHA256_Bad ()
  1034. {
  1035. SignedXml sign = SignHMAC (more256, new HMACSHA256 (hmackey), false);
  1036. Assert.AreEqual (more256, sign.SignatureMethod, "SignatureMethod");
  1037. }
  1038. [Test]
  1039. public void VerifyHMAC_SHA256 ()
  1040. {
  1041. string xml = "<?xml version=\"1.0\" encoding=\"Windows-1252\"?><foo><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/2001/04/xmldsig-more#hmac-sha256\" /><Reference URI=\"\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#sha256\" /><DigestValue>sKG2hDPEHiPrzpd3QA8BZ0eMzMbSEPPMh9QqXgkP7Cs=</DigestValue></Reference></SignedInfo><SignatureValue>Faad3KInJdIpaGcBn5e04Zv080u45fSjAKqrgevdWQw=</SignatureValue></Signature></foo>";
  1042. XmlDocument doc = new XmlDocument ();
  1043. doc.LoadXml (xml);
  1044. SignedXml sign = new SignedXml (doc);
  1045. sign.LoadXml (doc.DocumentElement ["Signature"]);
  1046. // verify MS-generated signature
  1047. Assert.IsTrue (sign.CheckSignature (new HMACSHA256 (hmackey)));
  1048. }
  1049. [Test]
  1050. public void SignHMAC_SHA512 ()
  1051. {
  1052. SignedXml sign = SignHMAC (EncryptedXml.XmlEncSHA512Url, new HMACSHA512 (hmackey), true);
  1053. Assert.AreEqual (more512, sign.SignatureMethod, "SignatureMethod");
  1054. }
  1055. [Test]
  1056. public void SignHMAC_SHA512_Bad ()
  1057. {
  1058. SignedXml sign = SignHMAC (more512, new HMACSHA512 (hmackey), false);
  1059. Assert.AreEqual (more512, sign.SignatureMethod, "SignatureMethod");
  1060. }
  1061. [Test]
  1062. public void VerifyHMAC_SHA512 ()
  1063. {
  1064. string xml = "<?xml version=\"1.0\" encoding=\"Windows-1252\"?><foo><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/2001/04/xmldsig-more#hmac-sha512\" /><Reference URI=\"\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#sha512\" /><DigestValue>2dvMkpTUE8Z76ResJG9pwPpVJNYo7t6s2L02V81xUVJ0oF8yJ7v8CTojjuL76s0iVdBxAOhP80Ambd1YaSkwSw==</DigestValue></Reference></SignedInfo><SignatureValue>wFenihRlm1x5/n0cifdX/TDOYqlbg2oVIbD/gyrAc0Q2hiIUTtwfBIMY5rQhKcErSz6YNoIl8RBQBmww/0rv5g==</SignatureValue></Signature></foo>";
  1065. XmlDocument doc = new XmlDocument ();
  1066. doc.LoadXml (xml);
  1067. SignedXml sign = new SignedXml (doc);
  1068. sign.LoadXml (doc.DocumentElement ["Signature"]);
  1069. // verify MS-generated signature
  1070. Assert.IsTrue (sign.CheckSignature (new HMACSHA512 (hmackey)));
  1071. }
  1072. [Test]
  1073. public void SignHMAC_SHA384 ()
  1074. {
  1075. // works as long as the string can be used by CryptoConfig to create
  1076. // an instance of the required hash algorithm
  1077. SignedXml sign = SignHMAC ("SHA384", new HMACSHA384 (hmackey), true);
  1078. Assert.AreEqual (more384, sign.SignatureMethod, "SignatureMethod");
  1079. }
  1080. [Test]
  1081. public void SignHMAC_SHA384_Bad ()
  1082. {
  1083. // we can't verity the signature if the URI is used
  1084. SignedXml sign = SignHMAC (more384, new HMACSHA384 (hmackey), false);
  1085. Assert.AreEqual (more384, sign.SignatureMethod, "SignatureMethod");
  1086. }
  1087. [Test]
  1088. public void VerifyHMAC_SHA384 ()
  1089. {
  1090. string xml = "<?xml version=\"1.0\" encoding=\"Windows-1252\"?><foo><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/2001/04/xmldsig-more#hmac-sha384\" /><Reference URI=\"\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /></Transforms><DigestMethod Algorithm=\"SHA384\" /><DigestValue>kH9C0LeZocNVXhjfzpz00M5fc3WJf0QU8gxK4I7pAN7HN602yHo8yYDSlG14b5YS</DigestValue></Reference></SignedInfo><SignatureValue>LgydOfhv8nqpFLFPC+xg3ZnjC8D+V3mpzxv6GOdH1HDdw1r+LH/BFM2U7dntxgf0</SignatureValue></Signature></foo>";
  1091. XmlDocument doc = new XmlDocument ();
  1092. doc.LoadXml (xml);
  1093. SignedXml sign = new SignedXml (doc);
  1094. sign.LoadXml (doc.DocumentElement ["Signature"]);
  1095. // verify MS-generated signature
  1096. Assert.IsTrue (sign.CheckSignature (new HMACSHA384 (hmackey)));
  1097. }
  1098. [Test]
  1099. public void SignHMAC_RIPEMD160 ()
  1100. {
  1101. // works as long as the string can be used by CryptoConfig to create
  1102. // an instance of the required hash algorithm
  1103. SignedXml sign = SignHMAC ("RIPEMD160", new HMACRIPEMD160 (hmackey), true);
  1104. Assert.AreEqual (moreripe, sign.SignatureMethod, "SignatureMethod");
  1105. }
  1106. [Test]
  1107. public void SignHMAC_RIPEMD160_Bad ()
  1108. {
  1109. // we can't verity the signature if the URI is used
  1110. SignedXml sign = SignHMAC (moreripe, new HMACRIPEMD160 (hmackey), false);
  1111. Assert.AreEqual (moreripe, sign.SignatureMethod, "SignatureMethod");
  1112. }
  1113. [Test]
  1114. public void VerifyHMAC_RIPEMD160 ()
  1115. {
  1116. string xml = "<?xml version=\"1.0\" encoding=\"Windows-1252\"?><foo><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/2001/04/xmldsig-more#hmac-ripemd160\" /><Reference URI=\"\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /></Transforms><DigestMethod Algorithm=\"RIPEMD160\" /><DigestValue>+uyqtoSDPvgrsXCl1KCimhFpOVw=</DigestValue></Reference></SignedInfo><SignatureValue>8E2m/A5lyU6mBug7uskBqpDHuvs=</SignatureValue></Signature></foo>";
  1117. XmlDocument doc = new XmlDocument ();
  1118. doc.LoadXml (xml);
  1119. SignedXml sign = new SignedXml (doc);
  1120. sign.LoadXml (doc.DocumentElement ["Signature"]);
  1121. // verify MS-generated signature
  1122. Assert.IsTrue (sign.CheckSignature (new HMACRIPEMD160 (hmackey)));
  1123. }
  1124. #endif
  1125. // CVE-2009-0217
  1126. // * a 0-length signature is the worse case - it accepts anything
  1127. // * between 1-7 bits length are considered invalid (not a multiple of 8)
  1128. // * a 8 bits signature would have one chance, out of 256, to be valid
  1129. // * and so on... until we hit (output-length / 2) or 80 bits (see ERRATUM)
  1130. static bool erratum = true; // xmldsig erratum for CVE-2009-0217
  1131. static SignedXml GetSignedXml (string xml)
  1132. {
  1133. XmlDocument doc = new XmlDocument ();
  1134. doc.LoadXml (xml);
  1135. SignedXml sign = new SignedXml (doc);
  1136. sign.LoadXml (doc.DocumentElement);
  1137. return sign;
  1138. }
  1139. static void CheckErratum (SignedXml signed, KeyedHashAlgorithm hmac, string message)
  1140. {
  1141. if (erratum) {
  1142. try {
  1143. signed.CheckSignature (hmac);
  1144. Assert.Fail (message + ": unexcepted success");
  1145. }
  1146. catch (CryptographicException) {
  1147. }
  1148. catch (Exception e) {
  1149. Assert.Fail (message + ": unexcepted " + e.ToString ());
  1150. }
  1151. } else {
  1152. Assert.IsTrue (signed.CheckSignature (hmac), message);
  1153. }
  1154. }
  1155. private void HmacMustBeMultipleOfEightBits (int bits)
  1156. {
  1157. string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
  1158. <Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"">
  1159. <SignedInfo>
  1160. <CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" />
  1161. <SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1"" >
  1162. <HMACOutputLength>{0}</HMACOutputLength>
  1163. </SignatureMethod>
  1164. <Reference URI=""#object"">
  1165. <DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" />
  1166. <DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue>
  1167. </Reference>
  1168. </SignedInfo>
  1169. <SignatureValue>
  1170. gA==
  1171. </SignatureValue>
  1172. <Object Id=""object"">some other text</Object>
  1173. </Signature>
  1174. ";
  1175. SignedXml sign = GetSignedXml (String.Format (xml, bits));
  1176. // only multiple of 8 bits are supported
  1177. sign.CheckSignature (new HMACSHA1 (Encoding.ASCII.GetBytes ("secret")));
  1178. }
  1179. [Test]
  1180. public void HmacMustBeMultipleOfEightBits ()
  1181. {
  1182. for (int i = 1; i < 160; i++) {
  1183. // The .NET framework only supports multiple of 8 bits
  1184. if (i % 8 == 0)
  1185. continue;
  1186. try {
  1187. HmacMustBeMultipleOfEightBits (i);
  1188. Assert.Fail ("Unexpected Success " + i.ToString ());
  1189. }
  1190. catch (CryptographicException) {
  1191. }
  1192. catch (Exception e) {
  1193. Assert.Fail ("Unexpected Exception " + i.ToString () + " : " + e.ToString ());
  1194. }
  1195. }
  1196. }
  1197. [Test]
  1198. [Category ("NotDotNet")] // will fail until a fix is available
  1199. public void VerifyHMAC_ZeroLength ()
  1200. {
  1201. string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
  1202. <Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"">
  1203. <SignedInfo>
  1204. <CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" />
  1205. <SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1"" >
  1206. <HMACOutputLength>0</HMACOutputLength>
  1207. </SignatureMethod>
  1208. <Reference URI=""#object"">
  1209. <DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" />
  1210. <DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue>
  1211. </Reference>
  1212. </SignedInfo>
  1213. <SignatureValue>
  1214. </SignatureValue>
  1215. <Object Id=""object"">some other text</Object>
  1216. </Signature>
  1217. ";
  1218. SignedXml sign = GetSignedXml (xml);
  1219. CheckErratum (sign, new HMACSHA1 (Encoding.ASCII.GetBytes ("no clue")), "1");
  1220. CheckErratum (sign, new HMACSHA1 (Encoding.ASCII.GetBytes ("")), "2");
  1221. CheckErratum (sign, new HMACSHA1 (Encoding.ASCII.GetBytes ("oops")), "3");
  1222. CheckErratum (sign, new HMACSHA1 (Encoding.ASCII.GetBytes ("secret")), "4");
  1223. }
  1224. [Test]
  1225. [Category ("NotDotNet")] // will fail until a fix is available
  1226. public void VerifyHMAC_SmallerThanMinimumLength ()
  1227. {
  1228. // 72 is a multiple of 8 but smaller than the minimum of 80 bits
  1229. string xml = @"<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""><HMACOutputLength>72</HMACOutputLength></SignatureMethod><Reference URI=""#object""><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue></Reference></SignedInfo><SignatureValue>2dimB+P5Aw5K</SignatureValue><Object Id=""object"">some other text</Object></Signature>";
  1230. SignedXml sign = GetSignedXml (xml);
  1231. CheckErratum (sign, new HMACSHA1 (Encoding.ASCII.GetBytes ("secret")), "72");
  1232. }
  1233. [Test]
  1234. public void VerifyHMAC_MinimumLength ()
  1235. {
  1236. // 80 bits is the minimum (and the half-size of HMACSHA1)
  1237. string xml = @"<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""><HMACOutputLength>80</HMACOutputLength></SignatureMethod><Reference URI=""#object""><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue></Reference></SignedInfo><SignatureValue>jVQPtLj61zNYjw==</SignatureValue><Object Id=""object"">some other text</Object></Signature>";
  1238. SignedXml sign = GetSignedXml (xml);
  1239. Assert.IsTrue (sign.CheckSignature (new HMACSHA1 (Encoding.ASCII.GetBytes ("secret"))));
  1240. }
  1241. #if NET_2_0
  1242. [Test]
  1243. [Category ("NotDotNet")] // will fail until a fix is available
  1244. public void VerifyHMAC_SmallerHalfLength ()
  1245. {
  1246. // 80bits is smaller than the half-size of HMACSHA256
  1247. string xml = @"<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/2001/04/xmldsig-more#hmac-sha256""><HMACOutputLength>80</HMACOutputLength></SignatureMethod><Reference URI=""#object""><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue></Reference></SignedInfo><SignatureValue>vPtw7zKVV/JwQg==</SignatureValue><Object Id=""object"">some other text</Object></Signature>";
  1248. SignedXml sign = GetSignedXml (xml);
  1249. CheckErratum (sign, new HMACSHA256 (Encoding.ASCII.GetBytes ("secret")), "80");
  1250. }
  1251. [Test]
  1252. public void VerifyHMAC_HalfLength ()
  1253. {
  1254. // 128 is the half-size of HMACSHA256
  1255. string xml = @"<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/2001/04/xmldsig-more#hmac-sha256""><HMACOutputLength>128</HMACOutputLength></SignatureMethod><Reference URI=""#object""><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue></Reference></SignedInfo><SignatureValue>aegpvkAwOL8gN/CjSnW6qw==</SignatureValue><Object Id=""object"">some other text</Object></Signature>";
  1256. SignedXml sign = GetSignedXml (xml);
  1257. Assert.IsTrue (sign.CheckSignature (new HMACSHA256 (Encoding.ASCII.GetBytes ("secret"))));
  1258. }
  1259. #endif
  1260. [Test]
  1261. public void VerifyHMAC_FullLength ()
  1262. {
  1263. string xml = @"<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=""#object""><DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" /><DigestValue>7/XTsHaBSOnJ/jXD5v0zL6VKYsk=</DigestValue></Reference></SignedInfo><SignatureValue>a0goL9esBUKPqtFYgpp2KST4huk=</SignatureValue><Object Id=""object"">some text</Object></Signature>";
  1264. SignedXml sign = GetSignedXml (xml);
  1265. Assert.IsTrue (sign.CheckSignature (new HMACSHA1 (Encoding.ASCII.GetBytes ("secret"))));
  1266. }
  1267. [Test]
  1268. [ExpectedException (typeof (CryptographicException))]
  1269. public void VerifyHMAC_HMACOutputLength_Signature_Mismatch ()
  1270. {
  1271. string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
  1272. <Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"">
  1273. <SignedInfo>
  1274. <CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" />
  1275. <SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1"" >
  1276. <HMACOutputLength>80</HMACOutputLength>
  1277. </SignatureMethod>
  1278. <Reference URI=""#object"">
  1279. <DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" />
  1280. <DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue>
  1281. </Reference>
  1282. </SignedInfo>
  1283. <SignatureValue>
  1284. </SignatureValue>
  1285. <Object Id=""object"">some other text</Object>
  1286. </Signature>
  1287. ";
  1288. SignedXml sign = GetSignedXml (xml);
  1289. sign.CheckSignature (new HMACSHA1 (Encoding.ASCII.GetBytes ("no clue")));
  1290. }
  1291. [Test]
  1292. [ExpectedException (typeof (FormatException))]
  1293. public void VerifyHMAC_HMACOutputLength_Invalid ()
  1294. {
  1295. string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
  1296. <Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"">
  1297. <SignedInfo>
  1298. <CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" />
  1299. <SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#hmac-sha1"" >
  1300. <HMACOutputLength>I wish this was not a string property</HMACOutputLength>
  1301. </SignatureMethod>
  1302. <Reference URI=""#object"">
  1303. <DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" />
  1304. <DigestValue>nz4GS0NbH2SrWlD/4fX313CoTzc=</DigestValue>
  1305. </Reference>
  1306. </SignedInfo>
  1307. <SignatureValue>
  1308. </SignatureValue>
  1309. <Object Id=""object"">some other text</Object>
  1310. </Signature>
  1311. ";
  1312. SignedXml sign = GetSignedXml (xml);
  1313. sign.CheckSignature (new HMACSHA1 (Encoding.ASCII.GetBytes ("no clue")));
  1314. }
  1315. }
  1316. }