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 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.Globalization;
38 using System.Runtime.InteropServices;
40 using Mono.Security.Cryptography;
42 namespace System.Security.Cryptography {
45 public sealed class DSACryptoServiceProvider : DSA, ICspAsymmetricAlgorithm {
46 private const int PROV_DSS_DH = 13; // from WinCrypt.h
48 private KeyPairPersistence store;
49 private bool persistKey;
50 private bool persisted;
52 private bool privateKeyExportable = true;
53 private bool m_disposed;
55 private DSAManaged dsa;
57 // MS implementation generates a keypair everytime a new DSA
58 // object is created (unless an existing key container is
59 // specified in the CspParameters).
61 // (a) often use DSA to import an existing keypair.
62 // (b) take a LOT of time to generate the DSA group
63 // So we'll generate the keypair only when (and if) it's being
64 // used (or exported). This should save us a lot of time (at
65 // least in the unit tests).
67 public DSACryptoServiceProvider ()
72 public DSACryptoServiceProvider (CspParameters parameters)
73 : this (1024, parameters)
77 public DSACryptoServiceProvider (int dwKeySize)
78 : this (dwKeySize, null)
82 public DSACryptoServiceProvider (int dwKeySize, CspParameters parameters)
84 LegalKeySizesValue = new KeySizes [1];
85 LegalKeySizesValue [0] = new KeySizes (512, 1024, 64);
87 // will throw an exception is key size isn't supported
89 dsa = new DSAManaged (dwKeySize);
90 dsa.KeyGenerated += new DSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);
92 persistKey = (parameters != null);
93 if (parameters == null) {
94 parameters = new CspParameters (PROV_DSS_DH);
95 if (useMachineKeyStore)
96 parameters.Flags |= CspProviderFlags.UseMachineKeyStore;
97 store = new KeyPairPersistence (parameters);
98 // no need to load - it cannot exists
101 store = new KeyPairPersistence (parameters);
103 if (store.KeyValue != null) {
105 this.FromXmlString (store.KeyValue);
110 ~DSACryptoServiceProvider ()
115 // DSA isn't used for key exchange
116 public override string KeyExchangeAlgorithm {
120 public override int KeySize {
121 get { return dsa.KeySize; }
124 public bool PersistKeyInCsp {
125 get { return persistKey; }
126 set { persistKey = value; }
130 public bool PublicOnly {
131 get { return dsa.PublicOnly; }
134 public override string SignatureAlgorithm {
135 get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }
138 private static bool useMachineKeyStore;
140 public static bool UseMachineKeyStore {
141 get { return useMachineKeyStore; }
142 set { useMachineKeyStore = value; }
145 public override DSAParameters ExportParameters (bool includePrivateParameters)
147 if ((includePrivateParameters) && (!privateKeyExportable)) {
148 throw new CryptographicException (
149 Locale.GetText ("Cannot export private key"));
152 return dsa.ExportParameters (includePrivateParameters);
155 public override void ImportParameters (DSAParameters parameters)
157 dsa.ImportParameters (parameters);
160 public override byte[] CreateSignature (byte[] rgbHash)
162 return dsa.CreateSignature (rgbHash);
165 public byte[] SignData (byte[] buffer)
167 // right now only SHA1 is supported by FIPS186-2
168 HashAlgorithm hash = SHA1.Create ();
169 byte[] toBeSigned = hash.ComputeHash (buffer);
170 return dsa.CreateSignature (toBeSigned);
173 public byte[] SignData (byte[] buffer, int offset, int count)
175 // right now only SHA1 is supported by FIPS186-2
176 HashAlgorithm hash = SHA1.Create ();
177 byte[] toBeSigned = hash.ComputeHash (buffer, offset, count);
178 return dsa.CreateSignature (toBeSigned);
181 public byte[] SignData (Stream inputStream)
183 // right now only SHA1 is supported by FIPS186-2
184 HashAlgorithm hash = SHA1.Create ();
185 byte[] toBeSigned = hash.ComputeHash (inputStream);
186 return dsa.CreateSignature (toBeSigned);
189 public byte[] SignHash (byte[] rgbHash, string str)
191 // right now only SHA1 is supported by FIPS186-2
192 if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0) {
194 throw new CryptographicException (Locale.GetText ("Only SHA1 is supported."));
197 return dsa.CreateSignature (rgbHash);
200 public bool VerifyData (byte[] rgbData, byte[] rgbSignature)
202 // right now only SHA1 is supported by FIPS186-2
203 HashAlgorithm hash = SHA1.Create();
204 byte[] toBeVerified = hash.ComputeHash (rgbData);
205 return dsa.VerifySignature (toBeVerified, rgbSignature);
208 // LAMESPEC: MD5 isn't allowed with DSA
209 public bool VerifyHash (byte[] rgbHash, string str, byte[] rgbSignature)
212 str = "SHA1"; // default value
213 if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0) {
214 throw new CryptographicException (Locale.GetText ("Only SHA1 is supported."));
217 return dsa.VerifySignature (rgbHash, rgbSignature);
220 public override bool VerifySignature (byte[] rgbHash, byte[] rgbSignature)
222 return dsa.VerifySignature (rgbHash, rgbSignature);
225 protected override void Dispose (bool disposing)
228 // the key is persisted and we do not want it persisted
229 if ((persisted) && (!persistKey)) {
230 store.Remove (); // delete the container
235 // no need as they all are abstract before us
242 private void OnKeyGenerated (object sender, EventArgs e)
244 // the key isn't persisted and we want it persisted
245 if ((persistKey) && (!persisted)) {
246 // save the current keypair
247 store.KeyValue = this.ToXmlString (!dsa.PublicOnly);
252 // ICspAsymmetricAlgorithm
254 [MonoTODO ("call into KeyPairPersistence to get details")]
256 public CspKeyContainerInfo CspKeyContainerInfo {
261 public byte[] ExportCspBlob (bool includePrivateParameters)
264 if (includePrivateParameters)
265 blob = CryptoConvert.ToCapiPrivateKeyBlob (this);
267 blob = CryptoConvert.ToCapiPublicKeyBlob (this);
272 public void ImportCspBlob (byte[] keyBlob)
275 throw new ArgumentNullException ("keyBlob");
276 DSA dsa = CryptoConvert.FromCapiKeyBlobDSA (keyBlob);
277 if (dsa is DSACryptoServiceProvider) {
278 DSAParameters dsap = dsa.ExportParameters (!(dsa as DSACryptoServiceProvider).PublicOnly);
279 ImportParameters (dsap);
281 // we can't know from DSA if the private key is available
284 DSAParameters dsap = dsa.ExportParameters (true);
285 ImportParameters (dsap);
289 DSAParameters dsap = dsa.ExportParameters (false);
290 ImportParameters (dsap);