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 {
43 public sealed class DSACryptoServiceProvider : DSA, ICspAsymmetricAlgorithm {
44 private const int PROV_DSS_DH = 13; // from WinCrypt.h
46 private KeyPairPersistence store;
47 private bool persistKey;
48 private bool persisted;
50 private bool privateKeyExportable = true;
51 private bool m_disposed;
53 private DSAManaged dsa;
55 // MS implementation generates a keypair everytime a new DSA
56 // object is created (unless an existing key container is
57 // specified in the CspParameters).
59 // (a) often use DSA to import an existing keypair.
60 // (b) take a LOT of time to generate the DSA group
61 // So we'll generate the keypair only when (and if) it's being
62 // used (or exported). This should save us a lot of time (at
63 // least in the unit tests).
65 public DSACryptoServiceProvider ()
70 public DSACryptoServiceProvider (CspParameters parameters)
71 : this (1024, parameters)
75 public DSACryptoServiceProvider (int dwKeySize)
76 : this (dwKeySize, null)
80 public DSACryptoServiceProvider (int dwKeySize, CspParameters parameters)
82 LegalKeySizesValue = new KeySizes [1];
83 LegalKeySizesValue [0] = new KeySizes (512, 1024, 64);
85 // will throw an exception is key size isn't supported
87 dsa = new DSAManaged (dwKeySize);
88 dsa.KeyGenerated += new DSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);
90 persistKey = (parameters != null);
91 if (parameters == null) {
92 parameters = new CspParameters (PROV_DSS_DH);
93 if (useMachineKeyStore)
94 parameters.Flags |= CspProviderFlags.UseMachineKeyStore;
95 store = new KeyPairPersistence (parameters);
96 // no need to load - it cannot exists
99 store = new KeyPairPersistence (parameters);
101 if (store.KeyValue != null) {
103 this.FromXmlString (store.KeyValue);
108 ~DSACryptoServiceProvider ()
113 // DSA isn't used for key exchange
114 public override string KeyExchangeAlgorithm {
118 public override int KeySize {
119 get { return dsa.KeySize; }
122 public bool PersistKeyInCsp {
123 get { return persistKey; }
124 set { persistKey = value; }
128 public bool PublicOnly {
129 get { return dsa.PublicOnly; }
132 public override string SignatureAlgorithm {
133 get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }
136 private static bool useMachineKeyStore;
138 public static bool UseMachineKeyStore {
139 get { return useMachineKeyStore; }
140 set { useMachineKeyStore = value; }
143 public override DSAParameters ExportParameters (bool includePrivateParameters)
145 if ((includePrivateParameters) && (!privateKeyExportable)) {
146 throw new CryptographicException (
147 Locale.GetText ("Cannot export private key"));
150 return dsa.ExportParameters (includePrivateParameters);
153 public override void ImportParameters (DSAParameters parameters)
155 dsa.ImportParameters (parameters);
158 public override byte[] CreateSignature (byte[] rgbHash)
160 return dsa.CreateSignature (rgbHash);
163 public byte[] SignData (byte[] buffer)
165 // right now only SHA1 is supported by FIPS186-2
166 HashAlgorithm hash = SHA1.Create ();
167 byte[] toBeSigned = hash.ComputeHash (buffer);
168 return dsa.CreateSignature (toBeSigned);
171 public byte[] SignData (byte[] buffer, int offset, int count)
173 // right now only SHA1 is supported by FIPS186-2
174 HashAlgorithm hash = SHA1.Create ();
175 byte[] toBeSigned = hash.ComputeHash (buffer, offset, count);
176 return dsa.CreateSignature (toBeSigned);
179 public byte[] SignData (Stream inputStream)
181 // right now only SHA1 is supported by FIPS186-2
182 HashAlgorithm hash = SHA1.Create ();
183 byte[] toBeSigned = hash.ComputeHash (inputStream);
184 return dsa.CreateSignature (toBeSigned);
187 public byte[] SignHash (byte[] rgbHash, string str)
189 // right now only SHA1 is supported by FIPS186-2
190 if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0) {
192 throw new CryptographicException (Locale.GetText ("Only SHA1 is supported."));
195 return dsa.CreateSignature (rgbHash);
198 public bool VerifyData (byte[] rgbData, byte[] rgbSignature)
200 // right now only SHA1 is supported by FIPS186-2
201 HashAlgorithm hash = SHA1.Create();
202 byte[] toBeVerified = hash.ComputeHash (rgbData);
203 return dsa.VerifySignature (toBeVerified, rgbSignature);
206 // LAMESPEC: MD5 isn't allowed with DSA
207 public bool VerifyHash (byte[] rgbHash, string str, byte[] rgbSignature)
210 str = "SHA1"; // default value
211 if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0) {
212 throw new CryptographicException (Locale.GetText ("Only SHA1 is supported."));
215 return dsa.VerifySignature (rgbHash, rgbSignature);
218 public override bool VerifySignature (byte[] rgbHash, byte[] rgbSignature)
220 return dsa.VerifySignature (rgbHash, rgbSignature);
223 protected override void Dispose (bool disposing)
226 // the key is persisted and we do not want it persisted
227 if ((persisted) && (!persistKey)) {
228 store.Remove (); // delete the container
233 // no need as they all are abstract before us
240 private void OnKeyGenerated (object sender, EventArgs e)
242 // the key isn't persisted and we want it persisted
243 if ((persistKey) && (!persisted)) {
244 // save the current keypair
245 store.KeyValue = this.ToXmlString (!dsa.PublicOnly);
250 // ICspAsymmetricAlgorithm
252 [MonoTODO ("call into KeyPairPersistence to get details")]
254 public CspKeyContainerInfo CspKeyContainerInfo {
259 public byte[] ExportCspBlob (bool includePrivateParameters)
262 if (includePrivateParameters)
263 blob = CryptoConvert.ToCapiPrivateKeyBlob (this);
265 blob = CryptoConvert.ToCapiPublicKeyBlob (this);
270 public void ImportCspBlob (byte[] keyBlob)
273 throw new ArgumentNullException ("keyBlob");
274 DSA dsa = CryptoConvert.FromCapiKeyBlobDSA (keyBlob);
275 if (dsa is DSACryptoServiceProvider) {
276 DSAParameters dsap = dsa.ExportParameters (!(dsa as DSACryptoServiceProvider).PublicOnly);
277 ImportParameters (dsap);
279 // we can't know from DSA if the private key is available
282 DSAParameters dsap = dsa.ExportParameters (true);
283 ImportParameters (dsap);
287 DSAParameters dsap = dsa.ExportParameters (false);
288 ImportParameters (dsap);