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.
32 using System.Collections.Specialized;
33 using System.Security.Cryptography;
34 using System.Security.Cryptography.Xml;
36 namespace System.Configuration
38 public sealed class RsaProtectedConfigurationProvider: ProtectedConfigurationProvider
40 string cspProviderName;
41 string keyContainerName;
42 bool useMachineContainer;
45 RSACryptoServiceProvider rsa;
47 RSACryptoServiceProvider GetProvider ()
50 CspParameters c = new CspParameters ();
51 c.ProviderName = cspProviderName;
52 c.KeyContainerName = keyContainerName;
53 if (useMachineContainer)
54 c.Flags |= CspProviderFlags.UseMachineKeyStore;
56 rsa = new RSACryptoServiceProvider (c);
62 public RsaProtectedConfigurationProvider ()
67 public override XmlNode Decrypt (XmlNode encrypted_node)
69 XmlDocument doc = new ConfigurationXmlDocument ();
71 doc.Load (new StringReader (encrypted_node.OuterXml));
73 EncryptedXml ex = new EncryptedXml (doc);
75 ex.AddKeyNameMapping ("Rsa Key", GetProvider ());
77 ex.DecryptDocument ();
79 return doc.DocumentElement;
83 public override XmlNode Encrypt (XmlNode node)
85 XmlDocument doc = new ConfigurationXmlDocument ();
87 doc.Load (new StringReader (node.OuterXml));
89 EncryptedXml ex = new EncryptedXml (doc);
91 ex.AddKeyNameMapping ("Rsa Key", GetProvider ());
93 EncryptedData d = ex.Encrypt (doc.DocumentElement, "Rsa Key");
99 public override void Initialize (string name, NameValueCollection configurationValues)
103 base.Initialize (name, configurationValues);
105 keyContainerName = configurationValues ["keyContainerName"];
106 cspProviderName = configurationValues ["cspProviderName"];
108 flag = configurationValues ["useMachineContainer"];
109 if (flag != null && flag.ToLower() == "true")
110 useMachineContainer = true;
112 flag = configurationValues ["useOAEP"];
113 if (flag != null && flag.ToLower() == "true")
118 public void AddKey (int keySize, bool exportable)
120 throw new NotImplementedException ();
124 public void DeleteKey ()
126 throw new NotImplementedException ();
130 public void ExportKey (string xmlFileName, bool includePrivateParameters)
132 RSACryptoServiceProvider prov = GetProvider ();
133 string xml = prov.ToXmlString (includePrivateParameters);
135 FileStream stream = new FileStream (xmlFileName, FileMode.OpenOrCreate, FileAccess.Write);
136 StreamWriter writer = new StreamWriter (stream);
143 public void ImportKey (string xmlFileName, bool exportable)
145 throw new NotImplementedException ();
148 public string CspProviderName
150 get { return cspProviderName; }
153 public string KeyContainerName {
154 get { return keyContainerName; }
157 public RSAParameters RsaPublicKey {
159 RSACryptoServiceProvider prov = GetProvider ();
160 return prov.ExportParameters (false);
164 public bool UseMachineContainer {
165 get { return useMachineContainer; }
168 public bool UseOAEP {
169 get { return useOAEP; }