Added tests for Task.WhenAll w/ empty list
[mono.git] / mcs / class / System.Configuration / System.Configuration / RsaProtectedConfigurationProvider.cs
1 //
2 // System.Configuration.RsaProtectedConfigurationProvider.cs
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30 using System.Xml;
31 using System.IO;
32 using System.Collections.Specialized;
33 using System.Security.Cryptography;
34 using System.Security.Cryptography.Xml;
35
36 namespace System.Configuration
37 {
38         public sealed class RsaProtectedConfigurationProvider: ProtectedConfigurationProvider
39         {
40                 string cspProviderName;
41                 string keyContainerName;
42                 bool useMachineContainer;
43                 bool useOAEP;
44
45                 RSACryptoServiceProvider rsa;
46
47                 RSACryptoServiceProvider GetProvider ()
48                 {
49                         if (rsa == null) {
50                                 CspParameters c = new CspParameters ();
51                                 c.ProviderName = cspProviderName;
52                                 c.KeyContainerName = keyContainerName;
53                                 if (useMachineContainer)
54                                         c.Flags |= CspProviderFlags.UseMachineKeyStore;
55
56                                 rsa = new RSACryptoServiceProvider (c);
57                         }
58
59                         return rsa;
60                 }
61
62                 public RsaProtectedConfigurationProvider ()
63                 {
64                 }
65
66                 [MonoTODO]
67                 public override XmlNode Decrypt (XmlNode encrypted_node)
68                 {
69                         XmlDocument doc = new ConfigurationXmlDocument ();
70                         
71                         doc.Load (new StringReader (encrypted_node.OuterXml));
72
73                         EncryptedXml ex = new EncryptedXml (doc);
74
75                         ex.AddKeyNameMapping ("Rsa Key", GetProvider ());
76
77                         ex.DecryptDocument ();
78                         
79                         return doc.DocumentElement;
80                 }
81
82                 [MonoTODO]
83                 public override XmlNode Encrypt (XmlNode node)
84                 {
85                         XmlDocument doc = new ConfigurationXmlDocument ();
86                         
87                         doc.Load (new StringReader (node.OuterXml));
88
89                         EncryptedXml ex = new EncryptedXml (doc);
90
91                         ex.AddKeyNameMapping ("Rsa Key", GetProvider ());
92
93                         EncryptedData d = ex.Encrypt (doc.DocumentElement, "Rsa Key");
94
95                         return d.GetXml();
96                 }
97
98                 [MonoTODO]
99                 public override void Initialize (string name, NameValueCollection configurationValues)
100                 {
101                         string flag;
102
103                         base.Initialize (name, configurationValues);
104
105                         keyContainerName = configurationValues ["keyContainerName"];
106                         cspProviderName = configurationValues ["cspProviderName"];
107
108                         flag = configurationValues ["useMachineContainer"];
109                         if (flag != null && flag.ToLower() == "true")
110                                 useMachineContainer = true;
111
112                         flag = configurationValues ["useOAEP"];
113                         if (flag != null && flag.ToLower() == "true")
114                                 useOAEP = true;
115                 }
116
117                 [MonoTODO]
118                 public void AddKey (int keySize, bool exportable)
119                 {
120                         throw new NotImplementedException ();
121                 }
122
123                 [MonoTODO]
124                 public void DeleteKey ()
125                 {
126                         throw new NotImplementedException ();
127                 }
128
129                 [MonoTODO]
130                 public void ExportKey (string xmlFileName, bool includePrivateParameters)
131                 {
132                         RSACryptoServiceProvider prov = GetProvider ();
133                         string xml = prov.ToXmlString (includePrivateParameters);
134
135                         FileStream stream = new FileStream (xmlFileName, FileMode.OpenOrCreate, FileAccess.Write);
136                         StreamWriter writer = new StreamWriter (stream);
137
138                         writer.Write (xml);
139                         writer.Close ();
140                 }
141
142                 [MonoTODO]
143                 public void ImportKey (string xmlFileName, bool exportable)
144                 {
145                         throw new NotImplementedException ();
146                 }
147
148                 public string CspProviderName
149                 {
150                         get { return cspProviderName; }
151                 }
152
153                 public string KeyContainerName {
154                         get { return keyContainerName; }
155                 }
156
157                 public RSAParameters RsaPublicKey {
158                         get {
159                                 RSACryptoServiceProvider prov = GetProvider ();
160                                 return prov.ExportParameters (false);
161                         }
162                 }
163
164                 public bool UseMachineContainer {
165                         get { return useMachineContainer; }
166                 }
167
168                 public bool UseOAEP {
169                         get { return useOAEP; }
170                 }
171         }
172 }
173 #endif