[docs] Import of Microsoft BCL Documentation.
[mono.git] / mcs / class / corlib / System.Security.Cryptography / DSACryptoServiceProvider.cs
index 7e000fbdfcef3041192da69e35298b139885f963..3d72d23a7bdd8f080f86f4fea135004791588510 100644 (file)
@@ -39,14 +39,8 @@ using Mono.Security.Cryptography;
 
 namespace System.Security.Cryptography {
 
-#if NET_2_0
        [ComVisible (true)]
        public sealed class DSACryptoServiceProvider : DSA, ICspAsymmetricAlgorithm {
-#elif NET_1_1
-       public sealed class DSACryptoServiceProvider : DSA {
-#else
-       public class DSACryptoServiceProvider : DSA {
-#endif
                private const int PROV_DSS_DH = 13;             // from WinCrypt.h
 
                private KeyPairPersistence store;
@@ -69,7 +63,7 @@ namespace System.Security.Cryptography {
                // least in the unit tests).
 
                public DSACryptoServiceProvider ()
-                       : this (1024, null)
+                       : this (1024)
                {
                }
 
@@ -79,11 +73,19 @@ namespace System.Security.Cryptography {
                }
 
                public DSACryptoServiceProvider (int dwKeySize)
-                       : this (dwKeySize, null)
                {
+                       Common (dwKeySize, false);
                }
 
                public DSACryptoServiceProvider (int dwKeySize, CspParameters parameters)
+               {
+                       bool has_parameters = parameters != null;
+                       Common (dwKeySize, has_parameters);
+                       if (has_parameters)
+                               Common (parameters);
+               }
+
+               void Common (int dwKeySize, bool parameters) 
                {
                        LegalKeySizesValue = new KeySizes [1];
                        LegalKeySizesValue [0] = new KeySizes (512, 1024, 64);
@@ -93,23 +95,24 @@ namespace System.Security.Cryptography {
                        dsa = new DSAManaged (dwKeySize);
                        dsa.KeyGenerated += new DSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);
 
-                       persistKey = (parameters != null);
-                       if (parameters == null) {
-                               parameters = new CspParameters (PROV_DSS_DH);
-#if NET_1_1
-                               if (useMachineKeyStore)
-                                       parameters.Flags |= CspProviderFlags.UseMachineKeyStore;
-#endif
-                               store = new KeyPairPersistence (parameters);
-                               // no need to load - it cannot exists
-                       }
-                       else {
-                               store = new KeyPairPersistence (parameters);
-                               store.Load ();
-                               if (store.KeyValue != null) {
-                                       persisted = true;
-                                       this.FromXmlString (store.KeyValue);
-                               }
+                       persistKey = parameters;
+                       if (parameters)
+                               return;
+
+                       var p = new CspParameters (PROV_DSS_DH);
+                       if (useMachineKeyStore)
+                               p.Flags |= CspProviderFlags.UseMachineKeyStore;
+                       store = new KeyPairPersistence (p);
+                       // no need to load - it cannot exists
+               }
+
+               void Common (CspParameters parameters)
+               {
+                       store = new KeyPairPersistence (parameters);
+                       store.Load ();
+                       if (store.KeyValue != null) {
+                               persisted = true;
+                               this.FromXmlString (store.KeyValue);
                        }
                }
 
@@ -127,24 +130,13 @@ namespace System.Security.Cryptography {
                        get { return dsa.KeySize; }
                }
 
-#if !NET_2_0
-               public override KeySizes[] LegalKeySizes {
-                       get { return LegalKeySizesValue; }
-               }
-#endif
-
                public bool PersistKeyInCsp {
                        get { return persistKey; }
                        set { persistKey = value; }
                }
 
-#if NET_2_0
                [ComVisible (false)]
-               public 
-#else
-               internal
-#endif
-               bool PublicOnly {
+               public bool PublicOnly {
                        get { return dsa.PublicOnly; }
                }
 
@@ -152,14 +144,12 @@ namespace System.Security.Cryptography {
                        get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }
                }
 
-#if NET_1_1
-               private static bool useMachineKeyStore = false;
+               private static bool useMachineKeyStore;
 
                public static bool UseMachineKeyStore {
                        get { return useMachineKeyStore; }
                        set { useMachineKeyStore = value; }
                }
-#endif
 
                public override DSAParameters ExportParameters (bool includePrivateParameters) 
                {
@@ -181,19 +171,19 @@ namespace System.Security.Cryptography {
                        return dsa.CreateSignature (rgbHash);
                }
 
-               public byte[] SignData (byte[] data)
+               public byte[] SignData (byte[] buffer)
                {
                        // right now only SHA1 is supported by FIPS186-2
                        HashAlgorithm hash = SHA1.Create ();
-                       byte[] toBeSigned = hash.ComputeHash (data);
+                       byte[] toBeSigned = hash.ComputeHash (buffer);
                        return dsa.CreateSignature (toBeSigned);
                }
 
-               public byte[] SignData (byte[] data, int offset, int count)
+               public byte[] SignData (byte[] buffer, int offset, int count)
                {
                        // right now only SHA1 is supported by FIPS186-2
                        HashAlgorithm hash = SHA1.Create ();
-                       byte[] toBeSigned = hash.ComputeHash (data, offset, count);
+                       byte[] toBeSigned = hash.ComputeHash (buffer, offset, count);
                        return dsa.CreateSignature (toBeSigned);
                }
 
@@ -268,7 +258,6 @@ namespace System.Security.Cryptography {
                                persisted = true;
                        }
                }
-#if NET_2_0
                // ICspAsymmetricAlgorithm
 
                [MonoTODO ("call into KeyPairPersistence to get details")]
@@ -277,21 +266,39 @@ namespace System.Security.Cryptography {
                        get { return null; }
                }
 
-               [MonoTODO ("call into CryptoConvert (doesn't currently support DSA)")]
                [ComVisible (false)]
                public byte[] ExportCspBlob (bool includePrivateParameters)
                {
-                       throw new NotImplementedException ("CryptoConvert doesn't currently support DSA");
+                       byte[] blob = null;
+                       if (includePrivateParameters)
+                               blob = CryptoConvert.ToCapiPrivateKeyBlob (this);
+                       else
+                               blob = CryptoConvert.ToCapiPublicKeyBlob (this);
+                       return blob;
                }
 
-               [MonoTODO ("call into CryptoConvert (doesn't currently support DSA)")]
                [ComVisible (false)]
-               public void ImportCspBlob (byte[] rawData)
+               public void ImportCspBlob (byte[] keyBlob)
                {
-                       if (rawData == null)
-                               throw new ArgumentNullException ("rawData");
-                       throw new NotImplementedException ("CryptoConvert doesn't currently support DSA");
+                       if (keyBlob == null)
+                               throw new ArgumentNullException ("keyBlob");
+                       DSA dsa = CryptoConvert.FromCapiKeyBlobDSA (keyBlob);
+                       if (dsa is DSACryptoServiceProvider) {
+                               DSAParameters dsap = dsa.ExportParameters (!(dsa as DSACryptoServiceProvider).PublicOnly);
+                               ImportParameters (dsap);
+                       } else {
+                               // we can't know from DSA if the private key is available
+                               try {
+                                       // so we try it...
+                                       DSAParameters dsap = dsa.ExportParameters (true);
+                                       ImportParameters (dsap);
+                               }
+                               catch {
+                                       // and fall back
+                                       DSAParameters dsap = dsa.ExportParameters (false);
+                                       ImportParameters (dsap);
+                               }
+                       }
                }
-#endif
        }
 }