Merge pull request #642 from Ventero/CleanCopyLocal
[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 using System.Xml;
30 using System.IO;
31 using System.Collections.Specialized;
32 using System.Security.Cryptography;
33 using System.Security.Cryptography.Xml;
34
35 namespace System.Configuration
36 {
37         public sealed class RsaProtectedConfigurationProvider: ProtectedConfigurationProvider
38         {
39                 string cspProviderName;
40                 string keyContainerName;
41                 bool useMachineContainer;
42                 bool useOAEP;
43
44                 RSACryptoServiceProvider rsa;
45
46                 RSACryptoServiceProvider GetProvider ()
47                 {
48                         if (rsa == null) {
49                                 CspParameters c = new CspParameters ();
50                                 c.ProviderName = cspProviderName;
51                                 c.KeyContainerName = keyContainerName;
52                                 if (useMachineContainer)
53                                         c.Flags |= CspProviderFlags.UseMachineKeyStore;
54
55                                 rsa = new RSACryptoServiceProvider (c);
56                         }
57
58                         return rsa;
59                 }
60
61                 public RsaProtectedConfigurationProvider ()
62                 {
63                 }
64
65                 [MonoTODO]
66                 public override XmlNode Decrypt (XmlNode encrypted_node)
67                 {
68                         XmlDocument doc = new ConfigurationXmlDocument ();
69                         
70                         doc.Load (new StringReader (encrypted_node.OuterXml));
71
72                         EncryptedXml ex = new EncryptedXml (doc);
73
74                         ex.AddKeyNameMapping ("Rsa Key", GetProvider ());
75
76                         ex.DecryptDocument ();
77                         
78                         return doc.DocumentElement;
79                 }
80
81                 [MonoTODO]
82                 public override XmlNode Encrypt (XmlNode node)
83                 {
84                         XmlDocument doc = new ConfigurationXmlDocument ();
85                         
86                         doc.Load (new StringReader (node.OuterXml));
87
88                         EncryptedXml ex = new EncryptedXml (doc);
89
90                         ex.AddKeyNameMapping ("Rsa Key", GetProvider ());
91
92                         EncryptedData d = ex.Encrypt (doc.DocumentElement, "Rsa Key");
93
94                         return d.GetXml();
95                 }
96
97                 [MonoTODO]
98                 public override void Initialize (string name, NameValueCollection configurationValues)
99                 {
100                         string flag;
101
102                         base.Initialize (name, configurationValues);
103
104                         keyContainerName = configurationValues ["keyContainerName"];
105                         cspProviderName = configurationValues ["cspProviderName"];
106
107                         flag = configurationValues ["useMachineContainer"];
108                         if (flag != null && flag.ToLower() == "true")
109                                 useMachineContainer = true;
110
111                         flag = configurationValues ["useOAEP"];
112                         if (flag != null && flag.ToLower() == "true")
113                                 useOAEP = true;
114                 }
115
116                 [MonoTODO]
117                 public void AddKey (int keySize, bool exportable)
118                 {
119                         throw new NotImplementedException ();
120                 }
121
122                 [MonoTODO]
123                 public void DeleteKey ()
124                 {
125                         throw new NotImplementedException ();
126                 }
127
128                 [MonoTODO]
129                 public void ExportKey (string xmlFileName, bool includePrivateParameters)
130                 {
131                         RSACryptoServiceProvider prov = GetProvider ();
132                         string xml = prov.ToXmlString (includePrivateParameters);
133
134                         FileStream stream = new FileStream (xmlFileName, FileMode.OpenOrCreate, FileAccess.Write);
135                         StreamWriter writer = new StreamWriter (stream);
136
137                         writer.Write (xml);
138                         writer.Close ();
139                 }
140
141                 [MonoTODO]
142                 public void ImportKey (string xmlFileName, bool exportable)
143                 {
144                         throw new NotImplementedException ();
145                 }
146
147                 public string CspProviderName
148                 {
149                         get { return cspProviderName; }
150                 }
151
152                 public string KeyContainerName {
153                         get { return keyContainerName; }
154                 }
155
156                 public RSAParameters RsaPublicKey {
157                         get {
158                                 RSACryptoServiceProvider prov = GetProvider ();
159                                 return prov.ExportParameters (false);
160                         }
161                 }
162
163                 public bool UseMachineContainer {
164                         get { return useMachineContainer; }
165                 }
166
167                 public bool UseOAEP {
168                         get { return useOAEP; }
169                 }
170         }
171 }
172