**** Merged from MCS ****
[mono-project.git] / mcs / class / Microsoft.Web.Services / Test / Microsoft.Web.Services.Security / EncryptedKeyTest.cs
blob44fd51bc4a97ff00f044ae14b73cc0252962ac5d
1 //
2 // EncryptedKeyTest.cs - NUnit Test Cases for EncryptedKey
3 //
4 // Author:
5 // Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
10 using NUnit.Framework;
11 using Microsoft.Web.Services.Security;
12 using System;
13 using System.Security.Cryptography;
14 using System.Security.Cryptography.Xml;
15 using System.Xml;
17 namespace MonoTests.MS.Web.Services.Security {
19 [TestFixture]
20 public class EncryptedKeyTest : Assertion {
22 public const string key = "<RSAKeyValue><Modulus>4o+Yqa3y34yOy+55ssgAALoxeVOutACyONctHoLCVxsbHBafJcnxxStlObm1B7aZnR5/ge6YTAPrboUTbpkBJfZ5bRb3jBoYl45tWmM0WDAth1MFO3gxUw6PZXxTK85ef2kPZ2jYPX6a0pA5bMWWuyA4y0sg/gv5RG0GrBn1zLk=</Modulus><Exponent>AQAB</Exponent><P>/DPHYnDUBmnKES9gv89w3UUmcSG7HuM6u1ZW7N0uoG2H6toF6M37RPs2DBEIwqNqr96VSyMhgDcvWXDHyiKhoQ==</P><Q>5fj41n0S3h513mdZF+ZLsg1/qQ1StUgRWGCNPI7/lZfteUOVQJPxKokXLoVRtf1RrsnGyVYIDPQKb+OZ8h2EGQ==</Q><DP>HkcpHMxm6A4zUDTb7Ks+5ZGzt8lQ6bhmCK+o5+719hSwSKW8J+Ly06lFJ9Wzs4pi+JJEYizpjLcTXC2KDt5xgQ==</DP><DQ>QyGxqhMqvdJQgBLVLCfa8ugD2xp7iVW3UoAk2oe3zjhiZyK2X/qPzOXl8XuThbej740RTVai1P0sSss69jVtiQ==</DQ><InverseQ>GAhDRgTDIbUUCZmwVv2tq4J7Iyktwsgpp3Hpa6PHM06tA1XCcOzeTV6H9m2+JCJjakcc8V4p2jl7kBzt+P3txQ==</InverseQ><D>No4HX8xwMF5jQD4DdgZs4b/0C9gXuGZ2g/NkfPVVoK35QDE+T1YqbqT9cgIC0oGoycm/QGVYrO1AstM+k6IqQSrWL8YjonGceUzZN71/VjBMpHEQOdtGTEX4/mMdFjGqNxxrsRgTAH/Wl3LAmraHgbnQcsdBcIy8RpmWZAveowE=</D></RSAKeyValue>";
24 public const string xml = "<xenc:EncryptedKey Type=\"http://www.w3.org/2001/04/xmlenc#EncryptedKey\" xmlns:xenc=\"http://www.w3.org/2001/04/xmlenc#\"><xenc:EncryptionMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#rsa-1_5\" /><KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><wsse:SecurityTokenReference><wsse:KeyIdentifier ValueType=\"wsse:X509v3\">MfB7IuMVrXzv++amAm93Tc0Hbas=</wsse:KeyIdentifier></wsse:SecurityTokenReference></KeyInfo><xenc:CipherData><xenc:CipherValue>1WfgPiTV7bpIOCtyKhmYXeTWvl7rykYoeaAqASh6iHQwhS7M61QHEOHw/wS4iphjBGVXsYcNUIFlpZEAkQalxeqLGMJHKkz5Mhd2Ee4N0DWfHlRz5hR7cnwxMKfo/MfzexPfLRcbDuE5iGrYDAb58XQPN3dHLmhCCK+kQ/4KH+E=</xenc:CipherValue></xenc:CipherData><xenc:ReferenceList><xenc:DataReference URI=\"#EncryptedContent-14dac16d-84e9-42bb-aeba-4030da7986d9\" /></xenc:ReferenceList></xenc:EncryptedKey><xenc:EncryptedData Id=\"EncryptedContent-14dac16d-84e9-42bb-aeba-4030da7986d9\" Type=\"http://www.w3.org/2001/04/xmlenc#Content\" xmlns:xenc=\"http://www.w3.org/2001/04/xmlenc#\"><xenc:EncryptionMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#tripledes-cbc\" /><xenc:CipherData><xenc:CipherValue>A4L9y9NICU8KhOF3ip323wucnpUXbM1De0AOuMaQ2kFRG4wUjw2Wo/u14kQts5uGPNtYWI8/bsBunRr4JVfbMANV+/bPZ3d3v++J+5lrrvvBAaxBu+bCZDwZK45Li/fD</xenc:CipherValue></xenc:CipherData></xenc:EncryptedData>";
26 private AsymmetricEncryptionKey GetKey ()
28 RSA rsa = RSA.Create ();
29 rsa.FromXmlString (key);
30 AsymmetricEncryptionKey aek = new AsymmetricEncryptionKey (rsa);
31 aek.KeyInfo.AddClause (new RSAKeyValue (rsa));
32 return aek;
35 [Test]
36 public void ConstructorAsymmetricEncryptionKey ()
38 EncryptedKey ek = new EncryptedKey (GetKey ());
39 AssertNotNull ("EncryptedKey(AsymmetricEncryptionKey)", ek);
40 // check default
41 AssertEquals ("EncryptionMethod", XmlEncryption.AlgorithmURI.RSA15, ek.EncryptionMethod);
42 AssertEquals ("SessionAlgorithmURI", XmlEncryption.AlgorithmURI.TripleDES, ek.SessionAlgorithmURI);
43 AssertNotNull ("KeyInfo", ek.KeyInfo);
44 Assert ("KeyInfo.Count > 0", (ek.KeyInfo.Count > 0));
45 AssertNotNull ("ReferenceList", ek.ReferenceList);
48 [Test]
49 [ExpectedException (typeof (ArgumentNullException))]
50 public void ConstructorAsymmetricEncryptionKeyNull ()
52 AsymmetricEncryptionKey aek = null;
53 EncryptedKey ek = new EncryptedKey (aek);
56 [Test]
57 [Ignore("IDecryptionKeyProvider")]
58 public void ConstructorXmlElement ()
60 XmlDocument doc = new XmlDocument ();
61 doc.LoadXml (xml);
62 EncryptedKey ek = new EncryptedKey (doc.DocumentElement);
63 AssertNotNull ("EncryptedKey(XmlElement)", ek);
66 [Test]
67 [ExpectedException (typeof (ArgumentNullException))]
68 public void ConstructorXmlElementNull ()
70 XmlElement xel = null;
71 EncryptedKey ek = new EncryptedKey (xel);
74 [Test]
75 public void SessionAlgorithmURI_AES128 ()
77 EncryptedKey ek = new EncryptedKey (GetKey ());
78 ek.SessionAlgorithmURI = XmlEncryption.AlgorithmURI.AES128;
79 AssertEquals ("SessionAlgorithmURI==AES128", XmlEncryption.AlgorithmURI.AES128, ek.SessionAlgorithmURI);
82 [Test]
83 public void SessionAlgorithmURI_AES192 ()
85 EncryptedKey ek = new EncryptedKey (GetKey ());
86 ek.SessionAlgorithmURI = XmlEncryption.AlgorithmURI.AES192;
87 AssertEquals ("SessionAlgorithmURI==AES192", XmlEncryption.AlgorithmURI.AES192, ek.SessionAlgorithmURI);
90 [Test]
91 public void SessionAlgorithmURI_AES256 ()
93 EncryptedKey ek = new EncryptedKey (GetKey ());
94 ek.SessionAlgorithmURI = XmlEncryption.AlgorithmURI.AES256;
95 AssertEquals ("SessionAlgorithmURI==AES256", XmlEncryption.AlgorithmURI.AES256, ek.SessionAlgorithmURI);
98 [Test]
99 public void SessionAlgorithmURI_TripleDES ()
101 EncryptedKey ek = new EncryptedKey (GetKey ());
102 ek.SessionAlgorithmURI = XmlEncryption.AlgorithmURI.TripleDES;
103 AssertEquals ("SessionAlgorithmURI==TripleDES", XmlEncryption.AlgorithmURI.TripleDES, ek.SessionAlgorithmURI);
106 private void UnsupportedAlgorithm (EncryptedKey ek, string algorithm)
108 try {
109 ek.SessionAlgorithmURI = algorithm;
110 Fail ("expected SecurityFault but got none");
112 catch (Exception e) {
113 if (!e.ToString ().StartsWith ("Microsoft.Web.Services.Security.SecurityFault"))
114 Fail ("expected SecurityFault but got " + e.ToString ());
118 [Test]
119 public void SessionAlgorithmURI_Unsupported ()
121 EncryptedKey ek = new EncryptedKey (GetKey ());
122 UnsupportedAlgorithm (ek, XmlEncryption.AlgorithmURI.AES128KeyWrap);
123 UnsupportedAlgorithm (ek, XmlEncryption.AlgorithmURI.AES192KeyWrap);
124 UnsupportedAlgorithm (ek, XmlEncryption.AlgorithmURI.AES256KeyWrap);
125 // strangely DES is defined but unsupported
126 UnsupportedAlgorithm (ek, XmlEncryption.AlgorithmURI.DES);
127 UnsupportedAlgorithm (ek, XmlEncryption.AlgorithmURI.RSA15);
128 UnsupportedAlgorithm (ek, XmlEncryption.AlgorithmURI.RSAOAEP);
129 UnsupportedAlgorithm (ek, XmlEncryption.AlgorithmURI.SHA1);
130 UnsupportedAlgorithm (ek, XmlEncryption.AlgorithmURI.SHA256);
131 UnsupportedAlgorithm (ek, XmlEncryption.AlgorithmURI.SHA512);
132 UnsupportedAlgorithm (ek, XmlEncryption.AlgorithmURI.TripleDESKeyWrap);
135 [Test]
136 [ExpectedException (typeof (ArgumentNullException))]
137 public void SessionAlgorithmURI_Null ()
139 EncryptedKey ek = new EncryptedKey (GetKey ());
140 ek.SessionAlgorithmURI = null;
143 [Test]
144 public void GetXml ()
146 EncryptedKey ek = new EncryptedKey (GetKey ());
147 XmlDocument doc = new XmlDocument ();
148 XmlElement xel = ek.GetXml (doc);
149 // output will always be different (new key each time)
150 AssertNotNull ("GetXml", xel);
151 // TODO: more
154 [Test]
155 [Ignore("IDecryptionKeyProvider")]
156 public void GetLoadXmlRoundtrip ()
158 EncryptedKey ek = new EncryptedKey (GetKey ());
159 XmlDocument doc = new XmlDocument ();
160 XmlElement xel = ek.GetXml (doc);
161 // output will always be different (new key each time)
162 AssertNotNull ("GetXml", xel);
163 ek.LoadXml (xel);
166 [Test]
167 [ExpectedException (typeof (ArgumentNullException))]
168 public void GetXmlNull ()
170 EncryptedKey ek = new EncryptedKey (GetKey ());
171 ek.GetXml (null);
174 [Test]
175 [ExpectedException (typeof (ArgumentNullException))]
176 public void LoadXmlNull ()
178 XmlElement xel = null;
179 EncryptedKey ek = new EncryptedKey (xel);
180 ek.LoadXml (null);