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.
35 using System.Globalization;
36 using System.Runtime.InteropServices;
38 using Mono.Security.Cryptography;
40 namespace System.Security.Cryptography {
44 public sealed class DSACryptoServiceProvider : DSA, ICspAsymmetricAlgorithm {
46 public sealed class DSACryptoServiceProvider : DSA {
48 public class DSACryptoServiceProvider : DSA {
50 private const int PROV_DSS_DH = 13; // from WinCrypt.h
52 private KeyPairPersistence store;
53 private bool persistKey;
54 private bool persisted;
56 private bool privateKeyExportable = true;
57 private bool m_disposed;
59 private DSAManaged dsa;
61 // MS implementation generates a keypair everytime a new DSA
62 // object is created (unless an existing key container is
63 // specified in the CspParameters).
65 // (a) often use DSA to import an existing keypair.
66 // (b) take a LOT of time to generate the DSA group
67 // So we'll generate the keypair only when (and if) it's being
68 // used (or exported). This should save us a lot of time (at
69 // least in the unit tests).
71 public DSACryptoServiceProvider ()
76 public DSACryptoServiceProvider (CspParameters parameters)
77 : this (1024, parameters)
81 public DSACryptoServiceProvider (int dwKeySize)
82 : this (dwKeySize, null)
86 public DSACryptoServiceProvider (int dwKeySize, CspParameters parameters)
88 LegalKeySizesValue = new KeySizes [1];
89 LegalKeySizesValue [0] = new KeySizes (512, 1024, 64);
91 // will throw an exception is key size isn't supported
93 dsa = new DSAManaged (dwKeySize);
94 dsa.KeyGenerated += new DSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);
96 persistKey = (parameters != null);
97 if (parameters == null) {
98 parameters = new CspParameters (PROV_DSS_DH);
100 if (useMachineKeyStore)
101 parameters.Flags |= CspProviderFlags.UseMachineKeyStore;
103 store = new KeyPairPersistence (parameters);
104 // no need to load - it cannot exists
107 store = new KeyPairPersistence (parameters);
109 if (store.KeyValue != null) {
111 this.FromXmlString (store.KeyValue);
116 ~DSACryptoServiceProvider ()
121 // DSA isn't used for key exchange
122 public override string KeyExchangeAlgorithm {
126 public override int KeySize {
127 get { return dsa.KeySize; }
131 public override KeySizes[] LegalKeySizes {
132 get { return LegalKeySizesValue; }
136 public bool PersistKeyInCsp {
137 get { return persistKey; }
138 set { persistKey = value; }
148 get { return dsa.PublicOnly; }
151 public override string SignatureAlgorithm {
152 get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }
156 private static bool useMachineKeyStore = false;
158 public static bool UseMachineKeyStore {
159 get { return useMachineKeyStore; }
160 set { useMachineKeyStore = value; }
164 public override DSAParameters ExportParameters (bool includePrivateParameters)
166 if ((includePrivateParameters) && (!privateKeyExportable)) {
167 throw new CryptographicException (
168 Locale.GetText ("Cannot export private key"));
171 return dsa.ExportParameters (includePrivateParameters);
174 public override void ImportParameters (DSAParameters parameters)
176 dsa.ImportParameters (parameters);
179 public override byte[] CreateSignature (byte[] rgbHash)
181 return dsa.CreateSignature (rgbHash);
184 public byte[] SignData (byte[] data)
186 // right now only SHA1 is supported by FIPS186-2
187 HashAlgorithm hash = SHA1.Create ();
188 byte[] toBeSigned = hash.ComputeHash (data);
189 return dsa.CreateSignature (toBeSigned);
192 public byte[] SignData (byte[] data, int offset, int count)
194 // right now only SHA1 is supported by FIPS186-2
195 HashAlgorithm hash = SHA1.Create ();
196 byte[] toBeSigned = hash.ComputeHash (data, offset, count);
197 return dsa.CreateSignature (toBeSigned);
200 public byte[] SignData (Stream inputStream)
202 // right now only SHA1 is supported by FIPS186-2
203 HashAlgorithm hash = SHA1.Create ();
204 byte[] toBeSigned = hash.ComputeHash (inputStream);
205 return dsa.CreateSignature (toBeSigned);
208 public byte[] SignHash (byte[] rgbHash, string str)
210 // right now only SHA1 is supported by FIPS186-2
211 if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0) {
213 throw new CryptographicException (Locale.GetText ("Only SHA1 is supported."));
216 return dsa.CreateSignature (rgbHash);
219 public bool VerifyData (byte[] rgbData, byte[] rgbSignature)
221 // right now only SHA1 is supported by FIPS186-2
222 HashAlgorithm hash = SHA1.Create();
223 byte[] toBeVerified = hash.ComputeHash (rgbData);
224 return dsa.VerifySignature (toBeVerified, rgbSignature);
227 // LAMESPEC: MD5 isn't allowed with DSA
228 public bool VerifyHash (byte[] rgbHash, string str, byte[] rgbSignature)
231 str = "SHA1"; // default value
232 if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0) {
233 throw new CryptographicException (Locale.GetText ("Only SHA1 is supported."));
236 return dsa.VerifySignature (rgbHash, rgbSignature);
239 public override bool VerifySignature (byte[] rgbHash, byte[] rgbSignature)
241 return dsa.VerifySignature (rgbHash, rgbSignature);
244 protected override void Dispose (bool disposing)
247 // the key is persisted and we do not want it persisted
248 if ((persisted) && (!persistKey)) {
249 store.Remove (); // delete the container
254 // no need as they all are abstract before us
261 private void OnKeyGenerated (object sender, EventArgs e)
263 // the key isn't persisted and we want it persisted
264 if ((persistKey) && (!persisted)) {
265 // save the current keypair
266 store.KeyValue = this.ToXmlString (!dsa.PublicOnly);
272 // ICspAsymmetricAlgorithm
274 [MonoTODO ("call into KeyPairPersistence to get details")]
276 public CspKeyContainerInfo CspKeyContainerInfo {
280 [MonoTODO ("call into CryptoConvert (doesn't currently support DSA)")]
282 public byte[] ExportCspBlob (bool includePrivateParameters)
284 throw new NotImplementedException ("CryptoConvert doesn't currently support DSA");
287 [MonoTODO ("call into CryptoConvert (doesn't currently support DSA)")]
289 public void ImportCspBlob (byte[] rawData)
292 throw new ArgumentNullException ("rawData");
293 throw new NotImplementedException ("CryptoConvert doesn't currently support DSA");