Merge pull request #644 from knocte/connstrings
[mono.git] / mcs / class / System.Configuration / System.Configuration / RsaProtectedConfigurationProvider.cs
index ee445ecfc0684f5b071a7ccdd776673bd0460f0a..bbb0eea08b302223ce6347cb7ad559d2f2fe5f2d 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
 using System.Xml;
+using System.IO;
 using System.Collections.Specialized;
 using System.Security.Cryptography;
+using System.Security.Cryptography.Xml;
 
 namespace System.Configuration
 {
        public sealed class RsaProtectedConfigurationProvider: ProtectedConfigurationProvider
        {
+               string cspProviderName;
                string keyContainerName;
                bool useMachineContainer;
+               bool useOAEP;
+
+               RSACryptoServiceProvider rsa;
+
+               RSACryptoServiceProvider GetProvider ()
+               {
+                       if (rsa == null) {
+                               CspParameters c = new CspParameters ();
+                               c.ProviderName = cspProviderName;
+                               c.KeyContainerName = keyContainerName;
+                               if (useMachineContainer)
+                                       c.Flags |= CspProviderFlags.UseMachineKeyStore;
+
+                               rsa = new RSACryptoServiceProvider (c);
+                       }
+
+                       return rsa;
+               }
 
                public RsaProtectedConfigurationProvider ()
                {
@@ -45,13 +65,33 @@ namespace System.Configuration
                [MonoTODO]
                public override XmlNode Decrypt (XmlNode encrypted_node)
                {
-                       throw new NotImplementedException ();
+                       XmlDocument doc = new ConfigurationXmlDocument ();
+                       
+                       doc.Load (new StringReader (encrypted_node.OuterXml));
+
+                       EncryptedXml ex = new EncryptedXml (doc);
+
+                       ex.AddKeyNameMapping ("Rsa Key", GetProvider ());
+
+                       ex.DecryptDocument ();
+                       
+                       return doc.DocumentElement;
                }
 
                [MonoTODO]
                public override XmlNode Encrypt (XmlNode node)
                {
-                       throw new NotImplementedException ();
+                       XmlDocument doc = new ConfigurationXmlDocument ();
+                       
+                       doc.Load (new StringReader (node.OuterXml));
+
+                       EncryptedXml ex = new EncryptedXml (doc);
+
+                       ex.AddKeyNameMapping ("Rsa Key", GetProvider ());
+
+                       EncryptedData d = ex.Encrypt (doc.DocumentElement, "Rsa Key");
+
+                       return d.GetXml();
                }
 
                [MonoTODO]
@@ -62,10 +102,15 @@ namespace System.Configuration
                        base.Initialize (name, configurationValues);
 
                        keyContainerName = configurationValues ["keyContainerName"];
+                       cspProviderName = configurationValues ["cspProviderName"];
 
                        flag = configurationValues ["useMachineContainer"];
                        if (flag != null && flag.ToLower() == "true")
                                useMachineContainer = true;
+
+                       flag = configurationValues ["useOAEP"];
+                       if (flag != null && flag.ToLower() == "true")
+                               useOAEP = true;
                }
 
                [MonoTODO]
@@ -83,7 +128,14 @@ namespace System.Configuration
                [MonoTODO]
                public void ExportKey (string xmlFileName, bool includePrivateParameters)
                {
-                       throw new NotImplementedException ();
+                       RSACryptoServiceProvider prov = GetProvider ();
+                       string xml = prov.ToXmlString (includePrivateParameters);
+
+                       FileStream stream = new FileStream (xmlFileName, FileMode.OpenOrCreate, FileAccess.Write);
+                       StreamWriter writer = new StreamWriter (stream);
+
+                       writer.Write (xml);
+                       writer.Close ();
                }
 
                [MonoTODO]
@@ -92,29 +144,29 @@ namespace System.Configuration
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                public string CspProviderName
                {
-                       get { throw new NotImplementedException (); }
+                       get { return cspProviderName; }
                }
 
                public string KeyContainerName {
                        get { return keyContainerName; }
                }
 
-               [MonoTODO]
                public RSAParameters RsaPublicKey {
-                       get { throw new NotImplementedException (); }
+                       get {
+                               RSACryptoServiceProvider prov = GetProvider ();
+                               return prov.ExportParameters (false);
+                       }
                }
 
                public bool UseMachineContainer {
                        get { return useMachineContainer; }
                }
 
-               [MonoTODO]
                public bool UseOAEP {
-                       get { throw new NotImplementedException (); }
+                       get { return useOAEP; }
                }
        }
 }
-#endif
+