2005-04-12 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / Microsoft.Web.Services / Test / Microsoft.Web.Services.Security / EncryptedKeyTest.cs
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 //
9
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;
16
17 namespace MonoTests.MS.Web.Services.Security {
18
19         [TestFixture]
20         public class EncryptedKeyTest : Assertion {
21
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>";
23
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>";
25
26                 private AsymmetricEncryptionKey GetKey () 
27                 {
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;
33                 }
34
35                 [Test]
36                 public void ConstructorAsymmetricEncryptionKey () 
37                 {
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);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (ArgumentNullException))] 
50                 public void ConstructorAsymmetricEncryptionKeyNull () 
51                 {
52                         AsymmetricEncryptionKey aek = null;
53                         EncryptedKey ek = new EncryptedKey (aek);
54                 }
55
56                 [Test]
57                 [Ignore("IDecryptionKeyProvider")]
58                 public void ConstructorXmlElement () 
59                 {
60                         XmlDocument doc = new XmlDocument ();
61                         doc.LoadXml (xml);
62                         EncryptedKey ek = new EncryptedKey (doc.DocumentElement);
63                         AssertNotNull ("EncryptedKey(XmlElement)", ek);
64                 }
65
66                 [Test]
67                 [ExpectedException (typeof (ArgumentNullException))] 
68                 public void ConstructorXmlElementNull () 
69                 {
70                         XmlElement xel = null;
71                         EncryptedKey ek = new EncryptedKey (xel);
72                 }
73
74                 [Test]
75                 public void SessionAlgorithmURI_AES128 () 
76                 {
77                         EncryptedKey ek = new EncryptedKey (GetKey ());
78                         ek.SessionAlgorithmURI = XmlEncryption.AlgorithmURI.AES128;
79                         AssertEquals ("SessionAlgorithmURI==AES128", XmlEncryption.AlgorithmURI.AES128, ek.SessionAlgorithmURI);
80                 }
81
82                 [Test]
83                 public void SessionAlgorithmURI_AES192 () 
84                 {
85                         EncryptedKey ek = new EncryptedKey (GetKey ());
86                         ek.SessionAlgorithmURI = XmlEncryption.AlgorithmURI.AES192;
87                         AssertEquals ("SessionAlgorithmURI==AES192", XmlEncryption.AlgorithmURI.AES192, ek.SessionAlgorithmURI);
88                 }
89
90                 [Test]
91                 public void SessionAlgorithmURI_AES256 () 
92                 {
93                         EncryptedKey ek = new EncryptedKey (GetKey ());
94                         ek.SessionAlgorithmURI = XmlEncryption.AlgorithmURI.AES256;
95                         AssertEquals ("SessionAlgorithmURI==AES256", XmlEncryption.AlgorithmURI.AES256, ek.SessionAlgorithmURI);
96                 }
97
98                 [Test]
99                 public void SessionAlgorithmURI_TripleDES () 
100                 {
101                         EncryptedKey ek = new EncryptedKey (GetKey ());
102                         ek.SessionAlgorithmURI = XmlEncryption.AlgorithmURI.TripleDES;
103                         AssertEquals ("SessionAlgorithmURI==TripleDES", XmlEncryption.AlgorithmURI.TripleDES, ek.SessionAlgorithmURI);
104                 }
105
106                 private void UnsupportedAlgorithm (EncryptedKey ek, string algorithm) 
107                 {
108                         try {
109                                 ek.SessionAlgorithmURI = algorithm;
110                                 Fail ("expected SecurityFault but got none");
111                         }
112                         catch (Exception e) {
113                                 if (!e.ToString ().StartsWith ("Microsoft.Web.Services.Security.SecurityFault"))
114                                         Fail ("expected SecurityFault but got " + e.ToString ());
115                         }
116                 }
117
118                 [Test]
119                 public void SessionAlgorithmURI_Unsupported () 
120                 {
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);
133                 }
134
135                 [Test]
136                 [ExpectedException (typeof (ArgumentNullException))] 
137                 public void SessionAlgorithmURI_Null () 
138                 {
139                         EncryptedKey ek = new EncryptedKey (GetKey ());
140                         ek.SessionAlgorithmURI = null;
141                 }
142
143                 [Test]
144                 public void GetXml () 
145                 {
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
152                 }
153
154                 [Test]
155                 [Ignore("IDecryptionKeyProvider")]
156                 public void GetLoadXmlRoundtrip () 
157                 {
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);
164                 }
165
166                 [Test]
167                 [ExpectedException (typeof (ArgumentNullException))] 
168                 public void GetXmlNull () 
169                 {
170                         EncryptedKey ek = new EncryptedKey (GetKey ());
171                         ek.GetXml (null);
172                 }
173
174                 [Test]
175                 [ExpectedException (typeof (ArgumentNullException))] 
176                 public void LoadXmlNull () 
177                 {
178                         XmlElement xel = null;
179                         EncryptedKey ek = new EncryptedKey (xel);
180                         ek.LoadXml (null);
181                 }
182         }
183 }