EncryptedXmlTest.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // EncryptedXmlTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  8. //
  9. #if !MOBILE
  10. using System;
  11. using System.Collections;
  12. using System.IO;
  13. using System.Security.Cryptography;
  14. using System.Security.Cryptography.X509Certificates;
  15. using System.Security.Cryptography.Xml;
  16. using System.Xml;
  17. using NUnit.Framework;
  18. namespace MonoTests.System.Security.Cryptography.Xml
  19. {
  20. [TestFixture]
  21. public class EncryptedXmlTest
  22. {
  23. [Test]
  24. public void Sample1 ()
  25. {
  26. AssertDecryption1 ("Test/System.Security.Cryptography.Xml/EncryptedXmlSample1.xml");
  27. }
  28. void AssertDecryption1 (string filename)
  29. {
  30. XmlDocument doc = new XmlDocument ();
  31. doc.PreserveWhitespace = true;
  32. doc.Load (filename);
  33. EncryptedXml encxml = new EncryptedXml (doc);
  34. RSACryptoServiceProvider rsa = new X509Certificate2 ("Test/System.Security.Cryptography.Xml/sample.pfx", "mono").PrivateKey as RSACryptoServiceProvider;
  35. XmlNamespaceManager nm = new XmlNamespaceManager (doc.NameTable);
  36. nm.AddNamespace ("s", "http://www.w3.org/2003/05/soap-envelope");
  37. nm.AddNamespace ("o", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
  38. nm.AddNamespace ("e", EncryptedXml.XmlEncNamespaceUrl);
  39. XmlElement el = doc.SelectSingleNode ("/s:Envelope/s:Header/o:Security/e:EncryptedKey", nm) as XmlElement;
  40. EncryptedKey ekey = new EncryptedKey ();
  41. ekey.LoadXml (el);
  42. byte [] key = rsa.Decrypt (ekey.CipherData.CipherValue, true);
  43. Rijndael aes = new RijndaelManaged ();
  44. aes.Key = key;
  45. aes.Mode = CipherMode.CBC;
  46. ArrayList al = new ArrayList ();
  47. foreach (XmlElement ed in doc.SelectNodes ("//e:EncryptedData", nm))
  48. al.Add (ed);
  49. foreach (XmlElement ed in al) {
  50. EncryptedData edata = new EncryptedData ();
  51. edata.LoadXml (ed);
  52. encxml.ReplaceData (ed, encxml.DecryptData (edata, aes));
  53. }
  54. }
  55. [Test]
  56. public void Sample2 ()
  57. {
  58. RijndaelManaged aes = new RijndaelManaged ();
  59. aes.Mode = CipherMode.CBC;
  60. aes.KeySize = 256;
  61. aes.Key = Convert.FromBase64String ("o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640=");
  62. aes.Padding = PaddingMode.Zeros;
  63. XmlDocument doc = new XmlDocument ();
  64. doc.PreserveWhitespace = true;
  65. doc.Load ("Test/System.Security.Cryptography.Xml/EncryptedXmlSample2.xml");
  66. EncryptedXml encxml = new EncryptedXml (doc);
  67. EncryptedData edata = new EncryptedData ();
  68. edata.LoadXml (doc.DocumentElement);
  69. encxml.ReplaceData (doc.DocumentElement, encxml.DecryptData (edata, aes));
  70. }
  71. [Test]
  72. public void Sample3 ()
  73. {
  74. AssertDecryption1 ("Test/System.Security.Cryptography.Xml/EncryptedXmlSample3.xml");
  75. }
  76. [Test]
  77. public void RoundtripSample1 ()
  78. {
  79. StringWriter sw = new StringWriter ();
  80. // Encryption
  81. {
  82. XmlDocument doc = new XmlDocument ();
  83. doc.PreserveWhitespace = true;
  84. doc.LoadXml ("<root> <child>sample</child> </root>");
  85. XmlElement body = doc.DocumentElement;
  86. RijndaelManaged aes = new RijndaelManaged ();
  87. aes.Mode = CipherMode.CBC;
  88. aes.KeySize = 256;
  89. aes.IV = Convert.FromBase64String ("pBUM5P03rZ6AE4ZK5EyBrw==");
  90. aes.Key = Convert.FromBase64String ("o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640=");
  91. aes.Padding = PaddingMode.Zeros;
  92. EncryptedXml exml = new EncryptedXml ();
  93. byte [] encrypted = exml.EncryptData (body, aes, false);
  94. EncryptedData edata = new EncryptedData ();
  95. edata.Type = EncryptedXml.XmlEncElementUrl;
  96. edata.EncryptionMethod = new EncryptionMethod (EncryptedXml.XmlEncAES256Url);
  97. EncryptedKey ekey = new EncryptedKey ();
  98. // omit key encryption, here for testing
  99. byte [] encKeyBytes = aes.Key;
  100. ekey.CipherData = new CipherData (encKeyBytes);
  101. ekey.EncryptionMethod = new EncryptionMethod (EncryptedXml.XmlEncRSA15Url);
  102. DataReference dr = new DataReference ();
  103. dr.Uri = "_0";
  104. ekey.AddReference (dr);
  105. edata.KeyInfo.AddClause (new KeyInfoEncryptedKey (ekey));
  106. edata.KeyInfo = new KeyInfo ();
  107. ekey.KeyInfo.AddClause (new RSAKeyValue (RSA.Create ()));
  108. edata.CipherData.CipherValue = encrypted;
  109. EncryptedXml.ReplaceElement (doc.DocumentElement, edata, false);
  110. doc.Save (new XmlTextWriter (sw));
  111. }
  112. // Decryption
  113. {
  114. RijndaelManaged aes = new RijndaelManaged ();
  115. aes.Mode = CipherMode.CBC;
  116. aes.KeySize = 256;
  117. aes.Key = Convert.FromBase64String (
  118. "o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640=");
  119. aes.Padding = PaddingMode.Zeros;
  120. XmlDocument doc = new XmlDocument ();
  121. doc.PreserveWhitespace = true;
  122. doc.LoadXml (sw.ToString ());
  123. EncryptedXml encxml = new EncryptedXml (doc);
  124. EncryptedData edata = new EncryptedData ();
  125. edata.LoadXml (doc.DocumentElement);
  126. encxml.ReplaceData (doc.DocumentElement, encxml.DecryptData (edata, aes));
  127. }
  128. }
  129. [Test]
  130. [ExpectedException (typeof (ArgumentNullException))]
  131. public void ReplaceData_XmlElementNull ()
  132. {
  133. EncryptedXml ex = new EncryptedXml ();
  134. ex.ReplaceData (null, new byte[0]);
  135. }
  136. [Test]
  137. [ExpectedException (typeof (ArgumentNullException))]
  138. public void ReplaceData_EncryptedDataNull ()
  139. {
  140. EncryptedXml ex = new EncryptedXml ();
  141. XmlDocument doc = new XmlDocument ();
  142. ex.ReplaceData (doc.DocumentElement, null);
  143. }
  144. [Test]
  145. [ExpectedException (typeof (ArgumentNullException))]
  146. public void ReplaceElement_XmlElementNull ()
  147. {
  148. EncryptedXml.ReplaceElement (null, new EncryptedData (), true);
  149. }
  150. [Test]
  151. [ExpectedException (typeof (ArgumentNullException))]
  152. public void ReplaceElement_EncryptedDataNull ()
  153. {
  154. XmlDocument doc = new XmlDocument ();
  155. EncryptedXml.ReplaceElement (doc.DocumentElement, null, false);
  156. }
  157. [Test]
  158. public void GetIdElement_XmlDocumentNull ()
  159. {
  160. EncryptedXml ex = new EncryptedXml ();
  161. Assert.IsNull (ex.GetIdElement (null, "value"));
  162. }
  163. [TestCase (null, TestName = "null")]
  164. [TestCase ("", TestName = "empty")]
  165. public void GetIdElement_WhenElementNameMustBeNonColonizedAndItIsNotProvided_ThrowsArgumentNullException (string elementName)
  166. {
  167. var sut = new EncryptedXml ();
  168. var ex = Assert.Throws<ArgumentNullException> (() => sut.GetIdElement (new XmlDocument (), elementName), "Exception");
  169. Assert.That (ex.ParamName, Is.EqualTo ("name"), "ParamName");
  170. }
  171. [Test]
  172. public void GetIdElement_WhenElementNameMustBeNonColonizedAndItContainsColon_ReturnsNull ()
  173. {
  174. var sut = new EncryptedXml ();
  175. Assert.That (sut.GetIdElement (new XmlDocument (), "t:test"), Is.Null);
  176. }
  177. [Test]
  178. [ExpectedException (typeof (ArgumentNullException))]
  179. public void GetDecryptionKey_EncryptedDataNull ()
  180. {
  181. EncryptedXml ex = new EncryptedXml ();
  182. ex.GetDecryptionKey (null, EncryptedXml.XmlEncAES128Url);
  183. }
  184. [Test]
  185. public void GetDecryptionKey_StringNull ()
  186. {
  187. EncryptedXml ex = new EncryptedXml ();
  188. Assert.IsNull (ex.GetDecryptionKey (new EncryptedData (), null));
  189. }
  190. [Test]
  191. [ExpectedException (typeof (ArgumentNullException))]
  192. public void GetDecryptionIV_EncryptedDataNull ()
  193. {
  194. EncryptedXml ex = new EncryptedXml ();
  195. ex.GetDecryptionIV (null, EncryptedXml.XmlEncAES128Url);
  196. }
  197. [Test]
  198. [ExpectedException (typeof (CryptographicException))]
  199. public void GetDecryptionIV_StringNull ()
  200. {
  201. EncryptedXml ex = new EncryptedXml ();
  202. Assert.IsNull (ex.GetDecryptionIV (new EncryptedData (), null));
  203. }
  204. [Test]
  205. [ExpectedException (typeof (ArgumentNullException))]
  206. public void DecryptKey_KeyNull ()
  207. {
  208. EncryptedXml.DecryptKey (null, Rijndael.Create ());
  209. }
  210. [Test]
  211. [ExpectedException (typeof (ArgumentNullException))]
  212. public void DecryptKey_SymmetricAlgorithmNull ()
  213. {
  214. EncryptedXml.DecryptKey (new byte [16], null);
  215. }
  216. [Test]
  217. [ExpectedException (typeof (ArgumentNullException))]
  218. public void EncryptKey_KeyNull ()
  219. {
  220. EncryptedXml.EncryptKey (null, Rijndael.Create ());
  221. }
  222. [Test]
  223. [ExpectedException (typeof (ArgumentNullException))]
  224. public void EncryptKey_SymmetricAlgorithmNull ()
  225. {
  226. EncryptedXml.EncryptKey (new byte [16], null);
  227. }
  228. [Test]
  229. [ExpectedException (typeof (ArgumentNullException))]
  230. public void DecryptData_EncryptedDataNull ()
  231. {
  232. EncryptedXml ex = new EncryptedXml ();
  233. ex.DecryptData (null, Rijndael.Create ());
  234. }
  235. [Test]
  236. [ExpectedException (typeof (ArgumentNullException))]
  237. public void DecryptData_SymmetricAlgorithmNull ()
  238. {
  239. EncryptedXml ex = new EncryptedXml ();
  240. ex.DecryptData (new EncryptedData (), null);
  241. }
  242. [Test]
  243. [ExpectedException (typeof (ArgumentNullException))]
  244. public void EncryptData_DataNull ()
  245. {
  246. EncryptedXml ex = new EncryptedXml ();
  247. ex.EncryptData (null, Rijndael.Create ());
  248. }
  249. [Test]
  250. [ExpectedException (typeof (ArgumentNullException))]
  251. public void EncryptData_SymmetricAlgorithmNull ()
  252. {
  253. EncryptedXml ex = new EncryptedXml ();
  254. ex.EncryptData (new byte[16], null);
  255. }
  256. [Test]
  257. [ExpectedException (typeof (ArgumentNullException))]
  258. public void EncryptData_XmlElementNull ()
  259. {
  260. EncryptedXml ex = new EncryptedXml ();
  261. ex.EncryptData (null, Rijndael.Create (), true);
  262. }
  263. [Test]
  264. [ExpectedException (typeof (ArgumentNullException))]
  265. public void DecryptEncryptedKey_Null ()
  266. {
  267. EncryptedXml ex = new EncryptedXml ();
  268. ex.DecryptEncryptedKey (null);
  269. }
  270. }
  271. }
  272. #endif