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