2 // System.Security.Cryptography.DSACryptoServiceProvider.cs
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Sebastien Pouliot <sebastien@ximian.com>
7 // Ben Maurer (bmaurer@users.sf.net)
10 // Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
11 // Portions (C) 2003 Ben Maurer
12 // (C) 2004 Novell (http://www.novell.com)
17 using System.Globalization;
19 using Mono.Security.Cryptography;
21 namespace System.Security.Cryptography {
24 public class DSACryptoServiceProvider : DSA {
26 public sealed class DSACryptoServiceProvider : DSA {
28 private const int PROV_DSS = 3; // from WinCrypt.h
30 private KeyPairPersistence store;
31 private bool persistKey;
32 private bool persisted;
34 private bool privateKeyExportable = true;
35 private bool m_disposed;
37 private DSAManaged dsa;
39 // MS implementation generates a keypair everytime a new DSA
40 // object is created (unless an existing key container is
41 // specified in the CspParameters).
43 // (a) often use DSA to import an existing keypair.
44 // (b) take a LOT of time to generate the DSA group
45 // So we'll generate the keypair only when (and if) it's being
46 // used (or exported). This should save us a lot of time (at
47 // least in the unit tests).
49 public DSACryptoServiceProvider () : this (1024, null) {}
51 public DSACryptoServiceProvider (CspParameters parameters) : this (1024, parameters) {}
53 public DSACryptoServiceProvider (int dwKeySize) : this (dwKeySize, null) {}
55 public DSACryptoServiceProvider (int dwKeySize, CspParameters parameters)
57 LegalKeySizesValue = new KeySizes [1];
58 LegalKeySizesValue [0] = new KeySizes (512, 1024, 64);
60 // will throw an exception is key size isn't supported
62 dsa = new DSAManaged (dwKeySize);
63 dsa.KeyGenerated += new DSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);
65 persistKey = (parameters != null);
66 if (parameters == null) {
67 parameters = new CspParameters (PROV_DSS);
69 if (useMachineKeyStore)
70 parameters.Flags |= CspProviderFlags.UseMachineKeyStore;
72 store = new KeyPairPersistence (parameters);
73 // no need to load - it cannot exists
76 store = new KeyPairPersistence (parameters);
78 if (store.KeyValue != null) {
80 this.FromXmlString (store.KeyValue);
85 ~DSACryptoServiceProvider ()
90 // DSA isn't used for key exchange
91 public override string KeyExchangeAlgorithm {
95 public override int KeySize {
96 get { return dsa.KeySize; }
99 public override KeySizes[] LegalKeySizes {
100 get { return LegalKeySizesValue; }
103 public bool PersistKeyInCsp {
104 get { return persistKey; }
108 OnKeyGenerated (dsa);
112 #if (NET_1_0 || NET_1_1)
118 get { return dsa.PublicOnly; }
121 public override string SignatureAlgorithm {
122 get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }
126 private static bool useMachineKeyStore = false;
128 public static bool UseMachineKeyStore {
129 get { return useMachineKeyStore; }
130 set { useMachineKeyStore = value; }
134 public override DSAParameters ExportParameters (bool includePrivateParameters)
136 if ((includePrivateParameters) && (!privateKeyExportable))
137 throw new CryptographicException ("cannot export private key");
139 return dsa.ExportParameters (includePrivateParameters);
142 public override void ImportParameters (DSAParameters parameters)
144 dsa.ImportParameters (parameters);
147 public override byte[] CreateSignature (byte[] rgbHash)
149 return dsa.CreateSignature (rgbHash);
152 public byte[] SignData (byte[] data)
154 return dsa.CreateSignature (data);
157 public byte[] SignData (byte[] data, int offset, int count)
159 // right now only SHA1 is supported by FIPS186-2
160 HashAlgorithm hash = SHA1.Create ();
161 byte[] toBeSigned = hash.ComputeHash (data, offset, count);
162 return dsa.CreateSignature (toBeSigned);
165 public byte[] SignData (Stream inputStream)
167 // right now only SHA1 is supported by FIPS186-2
168 HashAlgorithm hash = SHA1.Create ();
169 byte[] toBeSigned = hash.ComputeHash (inputStream);
170 return dsa.CreateSignature (toBeSigned);
173 public byte[] SignHash (byte[] rgbHash, string str)
175 // right now only SHA1 is supported by FIPS186-2
176 if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0)
177 throw new Exception (); // not documented
178 return dsa.CreateSignature (rgbHash);
181 public bool VerifyData (byte[] rgbData, byte[] rgbSignature)
183 // right now only SHA1 is supported by FIPS186-2
184 HashAlgorithm hash = SHA1.Create();
185 byte[] toBeVerified = hash.ComputeHash (rgbData);
186 return dsa.VerifySignature (toBeVerified, rgbSignature);
189 // LAMESPEC: MD5 isn't allowed with DSA
190 public bool VerifyHash (byte[] rgbHash, string str, byte[] rgbSignature)
193 str = "SHA1"; // default value
195 throw new CryptographicException ();
196 return dsa.VerifySignature (rgbHash, rgbSignature);
199 public override bool VerifySignature (byte[] rgbHash, byte[] rgbSignature)
201 return dsa.VerifySignature (rgbHash, rgbSignature);
204 protected override void Dispose (bool disposing)
207 // the key is persisted and we do not want it persisted
208 if ((persisted) && (!persistKey)) {
209 store.Remove (); // delete the container
214 // no need as they all are abstract before us
221 private void OnKeyGenerated (object sender)
223 // the key isn't persisted and we want it persisted
224 if ((persistKey) && (!persisted)) {
225 // save the current keypair
226 store.KeyValue = this.ToXmlString (!dsa.PublicOnly);