5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
13 using System.Collections;
15 using System.Security.Cryptography;
16 using System.Security.Cryptography.X509Certificates;
17 using System.Security.Cryptography.Xml;
20 using NUnit.Framework;
22 namespace MonoTests.System.Security.Cryptography.Xml
25 public class EncryptedXmlTest
28 public void Sample1 ()
30 AssertDecryption1 ("Test/System.Security.Cryptography.Xml/EncryptedXmlSample1.xml");
33 void AssertDecryption1 (string filename)
35 XmlDocument doc = new XmlDocument ();
36 doc.PreserveWhitespace = true;
38 EncryptedXml encxml = new EncryptedXml (doc);
39 RSACryptoServiceProvider rsa = new X509Certificate2 ("Test/System.Security.Cryptography.Xml/sample.pfx", "mono").PrivateKey as RSACryptoServiceProvider;
40 XmlNamespaceManager nm = new XmlNamespaceManager (doc.NameTable);
41 nm.AddNamespace ("s", "http://www.w3.org/2003/05/soap-envelope");
42 nm.AddNamespace ("o", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
43 nm.AddNamespace ("e", EncryptedXml.XmlEncNamespaceUrl);
44 XmlElement el = doc.SelectSingleNode ("/s:Envelope/s:Header/o:Security/e:EncryptedKey", nm) as XmlElement;
45 EncryptedKey ekey = new EncryptedKey ();
47 byte [] key = rsa.Decrypt (ekey.CipherData.CipherValue, true);
48 Rijndael aes = new RijndaelManaged ();
50 aes.Mode = CipherMode.CBC;
51 ArrayList al = new ArrayList ();
52 foreach (XmlElement ed in doc.SelectNodes ("//e:EncryptedData", nm))
54 foreach (XmlElement ed in al) {
55 EncryptedData edata = new EncryptedData ();
57 encxml.ReplaceData (ed, encxml.DecryptData (edata, aes));
62 public void Sample2 ()
64 RijndaelManaged aes = new RijndaelManaged ();
65 aes.Mode = CipherMode.CBC;
67 aes.Key = Convert.FromBase64String ("o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640=");
68 aes.Padding = PaddingMode.Zeros;
70 XmlDocument doc = new XmlDocument ();
71 doc.PreserveWhitespace = true;
72 doc.Load ("Test/System.Security.Cryptography.Xml/EncryptedXmlSample2.xml");
73 EncryptedXml encxml = new EncryptedXml (doc);
74 EncryptedData edata = new EncryptedData ();
75 edata.LoadXml (doc.DocumentElement);
76 encxml.ReplaceData (doc.DocumentElement, encxml.DecryptData (edata, aes));
80 public void Sample3 ()
82 AssertDecryption1 ("Test/System.Security.Cryptography.Xml/EncryptedXmlSample3.xml");
86 public void RoundtripSample1 ()
88 StringWriter sw = new StringWriter ();
92 XmlDocument doc = new XmlDocument ();
93 doc.PreserveWhitespace = true;
94 doc.LoadXml ("<root> <child>sample</child> </root>");
96 XmlElement body = doc.DocumentElement;
98 RijndaelManaged aes = new RijndaelManaged ();
99 aes.Mode = CipherMode.CBC;
101 aes.IV = Convert.FromBase64String ("pBUM5P03rZ6AE4ZK5EyBrw==");
102 aes.Key = Convert.FromBase64String ("o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640=");
103 aes.Padding = PaddingMode.Zeros;
105 EncryptedXml exml = new EncryptedXml ();
106 byte [] encrypted = exml.EncryptData (body, aes, false);
107 EncryptedData edata = new EncryptedData ();
108 edata.Type = EncryptedXml.XmlEncElementUrl;
109 edata.EncryptionMethod = new EncryptionMethod (EncryptedXml.XmlEncAES256Url);
110 EncryptedKey ekey = new EncryptedKey ();
111 // omit key encryption, here for testing
112 byte [] encKeyBytes = aes.Key;
113 ekey.CipherData = new CipherData (encKeyBytes);
114 ekey.EncryptionMethod = new EncryptionMethod (EncryptedXml.XmlEncRSA15Url);
115 DataReference dr = new DataReference ();
117 ekey.AddReference (dr);
118 edata.KeyInfo.AddClause (new KeyInfoEncryptedKey (ekey));
119 edata.KeyInfo = new KeyInfo ();
120 ekey.KeyInfo.AddClause (new RSAKeyValue (RSA.Create ()));
121 edata.CipherData.CipherValue = encrypted;
122 EncryptedXml.ReplaceElement (doc.DocumentElement, edata, false);
123 doc.Save (new XmlTextWriter (sw));
128 RijndaelManaged aes = new RijndaelManaged ();
129 aes.Mode = CipherMode.CBC;
131 aes.Key = Convert.FromBase64String (
132 "o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640=");
133 aes.Padding = PaddingMode.Zeros;
135 XmlDocument doc = new XmlDocument ();
136 doc.PreserveWhitespace = true;
137 doc.LoadXml (sw.ToString ());
138 EncryptedXml encxml = new EncryptedXml (doc);
139 EncryptedData edata = new EncryptedData ();
140 edata.LoadXml (doc.DocumentElement);
141 encxml.ReplaceData (doc.DocumentElement, encxml.DecryptData (edata, aes));
146 [ExpectedException (typeof (ArgumentNullException))]
147 public void ReplaceData_XmlElementNull ()
149 EncryptedXml ex = new EncryptedXml ();
150 ex.ReplaceData (null, new byte[0]);
154 [ExpectedException (typeof (ArgumentNullException))]
155 public void ReplaceData_EncryptedDataNull ()
157 EncryptedXml ex = new EncryptedXml ();
158 XmlDocument doc = new XmlDocument ();
159 ex.ReplaceData (doc.DocumentElement, null);
163 [ExpectedException (typeof (ArgumentNullException))]
164 public void ReplaceElement_XmlElementNull ()
166 EncryptedXml.ReplaceElement (null, new EncryptedData (), true);
170 [ExpectedException (typeof (ArgumentNullException))]
171 public void ReplaceElement_EncryptedDataNull ()
173 XmlDocument doc = new XmlDocument ();
174 EncryptedXml.ReplaceElement (doc.DocumentElement, null, false);
178 public void GetIdElement_XmlDocumentNull ()
180 EncryptedXml ex = new EncryptedXml ();
181 Assert.IsNull (ex.GetIdElement (null, "value"));
185 public void GetIdElement_StringNull ()
187 EncryptedXml ex = new EncryptedXml ();
188 Assert.IsNull (ex.GetIdElement (new XmlDocument (), null));
192 [ExpectedException (typeof (ArgumentNullException))]
193 public void GetDecryptionKey_EncryptedDataNull ()
195 EncryptedXml ex = new EncryptedXml ();
196 ex.GetDecryptionKey (null, EncryptedXml.XmlEncAES128Url);
200 public void GetDecryptionKey_StringNull ()
202 EncryptedXml ex = new EncryptedXml ();
203 Assert.IsNull (ex.GetDecryptionKey (new EncryptedData (), null));
207 [ExpectedException (typeof (ArgumentNullException))]
208 public void GetDecryptionIV_EncryptedDataNull ()
210 EncryptedXml ex = new EncryptedXml ();
211 ex.GetDecryptionIV (null, EncryptedXml.XmlEncAES128Url);
215 [ExpectedException (typeof (CryptographicException))]
216 public void GetDecryptionIV_StringNull ()
218 EncryptedXml ex = new EncryptedXml ();
219 Assert.IsNull (ex.GetDecryptionIV (new EncryptedData (), null));
223 [ExpectedException (typeof (ArgumentNullException))]
224 public void DecryptKey_KeyNull ()
226 EncryptedXml.DecryptKey (null, Rijndael.Create ());
230 [ExpectedException (typeof (ArgumentNullException))]
231 public void DecryptKey_SymmetricAlgorithmNull ()
233 EncryptedXml.DecryptKey (new byte [16], null);
237 [ExpectedException (typeof (ArgumentNullException))]
238 public void EncryptKey_KeyNull ()
240 EncryptedXml.EncryptKey (null, Rijndael.Create ());
244 [ExpectedException (typeof (ArgumentNullException))]
245 public void EncryptKey_SymmetricAlgorithmNull ()
247 EncryptedXml.EncryptKey (new byte [16], null);
251 [ExpectedException (typeof (ArgumentNullException))]
252 public void DecryptData_EncryptedDataNull ()
254 EncryptedXml ex = new EncryptedXml ();
255 ex.DecryptData (null, Rijndael.Create ());
259 [ExpectedException (typeof (ArgumentNullException))]
260 public void DecryptData_SymmetricAlgorithmNull ()
262 EncryptedXml ex = new EncryptedXml ();
263 ex.DecryptData (new EncryptedData (), null);
267 [ExpectedException (typeof (ArgumentNullException))]
268 public void EncryptData_DataNull ()
270 EncryptedXml ex = new EncryptedXml ();
271 ex.EncryptData (null, Rijndael.Create ());
275 [ExpectedException (typeof (ArgumentNullException))]
276 public void EncryptData_SymmetricAlgorithmNull ()
278 EncryptedXml ex = new EncryptedXml ();
279 ex.EncryptData (new byte[16], null);
283 [ExpectedException (typeof (ArgumentNullException))]
284 public void EncryptData_XmlElementNull ()
286 EncryptedXml ex = new EncryptedXml ();
287 ex.EncryptData (null, Rijndael.Create (), true);
291 [ExpectedException (typeof (ArgumentNullException))]
292 public void DecryptEncryptedKey_Null ()
294 EncryptedXml ex = new EncryptedXml ();
295 ex.DecryptEncryptedKey (null);