2007-04-24 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / corlib / System.Security.Cryptography / DSACryptoServiceProvider.cs
index a8795da78a56837e27fdd9ab0231d7023edfa6af..7e000fbdfcef3041192da69e35298b139885f963 100644 (file)
@@ -9,23 +9,45 @@
 // (C) 2002
 // Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
 // Portions (C) 2003 Ben Maurer
-// (C) 2004 Novell (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
 using System.IO;
 using System.Globalization;
+using System.Runtime.InteropServices;
 
 using Mono.Security.Cryptography;
 
 namespace System.Security.Cryptography {
 
-#if NET_1_0
-       public class DSACryptoServiceProvider : DSA {
-#else
+#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 = 3;         // from WinCrypt.h
+               private const int PROV_DSS_DH = 13;             // from WinCrypt.h
 
                private KeyPairPersistence store;
                private bool persistKey;
@@ -46,11 +68,20 @@ namespace System.Security.Cryptography {
                // used (or exported). This should save us a lot of time (at
                // least in the unit tests).
 
-               public DSACryptoServiceProvider () : this (1024, null) {}
+               public DSACryptoServiceProvider ()
+                       : this (1024, null)
+               {
+               }
 
-               public DSACryptoServiceProvider (CspParameters parameters) : this (1024, parameters) {}
+               public DSACryptoServiceProvider (CspParameters parameters)
+                       : this (1024, parameters)
+               {
+               }
 
-               public DSACryptoServiceProvider (int dwKeySize) : this (dwKeySize, null) {}
+               public DSACryptoServiceProvider (int dwKeySize)
+                       : this (dwKeySize, null)
+               {
+               }
 
                public DSACryptoServiceProvider (int dwKeySize, CspParameters parameters)
                {
@@ -64,8 +95,8 @@ namespace System.Security.Cryptography {
 
                        persistKey = (parameters != null);
                        if (parameters == null) {
-                               parameters = new CspParameters (PROV_DSS);
-#if ! NET_1_0
+                               parameters = new CspParameters (PROV_DSS_DH);
+#if NET_1_1
                                if (useMachineKeyStore)
                                        parameters.Flags |= CspProviderFlags.UseMachineKeyStore;
 #endif
@@ -96,23 +127,22 @@ 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 (persistKey)
-                                       OnKeyGenerated (dsa, null);
-                       }
+                       set { persistKey = value; }
                }
 
-#if (NET_1_0 || NET_1_1)
-               internal
-#else
+#if NET_2_0
+               [ComVisible (false)]
                public 
+#else
+               internal
 #endif
                bool PublicOnly {
                        get { return dsa.PublicOnly; }
@@ -122,7 +152,7 @@ namespace System.Security.Cryptography {
                        get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }
                }
 
-#if ! NET_1_0
+#if NET_1_1
                private static bool useMachineKeyStore = false;
 
                public static bool UseMachineKeyStore {
@@ -133,8 +163,10 @@ namespace System.Security.Cryptography {
 
                public override DSAParameters ExportParameters (bool includePrivateParameters) 
                {
-                       if ((includePrivateParameters) && (!privateKeyExportable))
-                               throw new CryptographicException ("cannot export private key");
+                       if ((includePrivateParameters) && (!privateKeyExportable)) {
+                               throw new CryptographicException (
+                                       Locale.GetText ("Cannot export private key"));
+                       }
 
                        return dsa.ExportParameters (includePrivateParameters);
                }
@@ -151,7 +183,10 @@ namespace System.Security.Cryptography {
 
                public byte[] SignData (byte[] data)
                {
-                       return dsa.CreateSignature (data);
+                       // right now only SHA1 is supported by FIPS186-2
+                       HashAlgorithm hash = SHA1.Create ();
+                       byte[] toBeSigned = hash.ComputeHash (data);
+                       return dsa.CreateSignature (toBeSigned);
                }
 
                public byte[] SignData (byte[] data, int offset, int count)
@@ -173,8 +208,11 @@ namespace System.Security.Cryptography {
                public byte[] SignHash (byte[] rgbHash, string str)
                {
                        // right now only SHA1 is supported by FIPS186-2
-                       if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0)
-                               throw new Exception (); // not documented
+                       if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0) {
+                               // not documented
+                               throw new CryptographicException (Locale.GetText ("Only SHA1 is supported."));
+                       }
+
                        return dsa.CreateSignature (rgbHash);
                }
 
@@ -191,8 +229,10 @@ namespace System.Security.Cryptography {
                {
                        if (str == null)
                                str = "SHA1"; // default value
-                       if (str != "SHA1")
-                               throw new CryptographicException ();
+                       if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0) {
+                               throw new CryptographicException (Locale.GetText ("Only SHA1 is supported."));
+                       }
+
                        return dsa.VerifySignature (rgbHash, rgbSignature);
                }
 
@@ -228,5 +268,30 @@ namespace System.Security.Cryptography {
                                persisted = true;
                        }
                }
+#if NET_2_0
+               // ICspAsymmetricAlgorithm
+
+               [MonoTODO ("call into KeyPairPersistence to get details")]
+               [ComVisible (false)]
+               public CspKeyContainerInfo CspKeyContainerInfo {
+                       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");
+               }
+
+               [MonoTODO ("call into CryptoConvert (doesn't currently support DSA)")]
+               [ComVisible (false)]
+               public void ImportCspBlob (byte[] rawData)
+               {
+                       if (rawData == null)
+                               throw new ArgumentNullException ("rawData");
+                       throw new NotImplementedException ("CryptoConvert doesn't currently support DSA");
+               }
+#endif
        }
 }