2 // System.Configuration.RsaProtectedConfigurationProvider.cs
5 // Chris Toshok (toshok@ximian.com)
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections.Specialized;
32 using System.Security.Cryptography;
33 using System.Security.Cryptography.Xml;
35 namespace System.Configuration
37 public sealed class RsaProtectedConfigurationProvider: ProtectedConfigurationProvider
39 string cspProviderName;
40 string keyContainerName;
41 bool useMachineContainer;
44 RSACryptoServiceProvider rsa;
46 RSACryptoServiceProvider GetProvider ()
49 CspParameters c = new CspParameters ();
50 c.ProviderName = cspProviderName;
51 c.KeyContainerName = keyContainerName;
52 if (useMachineContainer)
53 c.Flags |= CspProviderFlags.UseMachineKeyStore;
55 rsa = new RSACryptoServiceProvider (c);
61 public RsaProtectedConfigurationProvider ()
66 public override XmlNode Decrypt (XmlNode encrypted_node)
68 XmlDocument doc = new ConfigurationXmlDocument ();
70 doc.Load (new StringReader (encrypted_node.OuterXml));
72 EncryptedXml ex = new EncryptedXml (doc);
74 ex.AddKeyNameMapping ("Rsa Key", GetProvider ());
76 ex.DecryptDocument ();
78 return doc.DocumentElement;
82 public override XmlNode Encrypt (XmlNode node)
84 XmlDocument doc = new ConfigurationXmlDocument ();
86 doc.Load (new StringReader (node.OuterXml));
88 EncryptedXml ex = new EncryptedXml (doc);
90 ex.AddKeyNameMapping ("Rsa Key", GetProvider ());
92 EncryptedData d = ex.Encrypt (doc.DocumentElement, "Rsa Key");
98 public override void Initialize (string name, NameValueCollection configurationValues)
102 base.Initialize (name, configurationValues);
104 keyContainerName = configurationValues ["keyContainerName"];
105 cspProviderName = configurationValues ["cspProviderName"];
107 flag = configurationValues ["useMachineContainer"];
108 if (flag != null && flag.ToLower() == "true")
109 useMachineContainer = true;
111 flag = configurationValues ["useOAEP"];
112 if (flag != null && flag.ToLower() == "true")
117 public void AddKey (int keySize, bool exportable)
119 throw new NotImplementedException ();
123 public void DeleteKey ()
125 throw new NotImplementedException ();
129 public void ExportKey (string xmlFileName, bool includePrivateParameters)
131 RSACryptoServiceProvider prov = GetProvider ();
132 string xml = prov.ToXmlString (includePrivateParameters);
134 FileStream stream = new FileStream (xmlFileName, FileMode.OpenOrCreate, FileAccess.Write);
135 StreamWriter writer = new StreamWriter (stream);
142 public void ImportKey (string xmlFileName, bool exportable)
144 throw new NotImplementedException ();
147 public string CspProviderName
149 get { return cspProviderName; }
152 public string KeyContainerName {
153 get { return keyContainerName; }
156 public RSAParameters RsaPublicKey {
158 RSACryptoServiceProvider prov = GetProvider ();
159 return prov.ExportParameters (false);
163 public bool UseMachineContainer {
164 get { return useMachineContainer; }
167 public bool UseOAEP {
168 get { return useOAEP; }