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 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 {
45 public sealed class DSACryptoServiceProvider : DSA {
47 public class DSACryptoServiceProvider : DSA {
49 private const int PROV_DSS_DH = 13; // from WinCrypt.h
51 private KeyPairPersistence store;
52 private bool persistKey;
53 private bool persisted;
55 private bool privateKeyExportable = true;
56 private bool m_disposed;
58 private DSAManaged dsa;
60 // MS implementation generates a keypair everytime a new DSA
61 // object is created (unless an existing key container is
62 // specified in the CspParameters).
64 // (a) often use DSA to import an existing keypair.
65 // (b) take a LOT of time to generate the DSA group
66 // So we'll generate the keypair only when (and if) it's being
67 // used (or exported). This should save us a lot of time (at
68 // least in the unit tests).
70 public DSACryptoServiceProvider ()
75 public DSACryptoServiceProvider (CspParameters parameters)
76 : this (1024, parameters)
80 public DSACryptoServiceProvider (int dwKeySize)
81 : this (dwKeySize, null)
85 public DSACryptoServiceProvider (int dwKeySize, CspParameters parameters)
87 LegalKeySizesValue = new KeySizes [1];
88 LegalKeySizesValue [0] = new KeySizes (512, 1024, 64);
90 // will throw an exception is key size isn't supported
92 dsa = new DSAManaged (dwKeySize);
93 dsa.KeyGenerated += new DSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);
95 persistKey = (parameters != null);
96 if (parameters == null) {
97 parameters = new CspParameters (PROV_DSS_DH);
99 if (useMachineKeyStore)
100 parameters.Flags |= CspProviderFlags.UseMachineKeyStore;
102 store = new KeyPairPersistence (parameters);
103 // no need to load - it cannot exists
106 store = new KeyPairPersistence (parameters);
108 if (store.KeyValue != null) {
110 this.FromXmlString (store.KeyValue);
115 ~DSACryptoServiceProvider ()
120 // DSA isn't used for key exchange
121 public override string KeyExchangeAlgorithm {
125 public override int KeySize {
126 get { return dsa.KeySize; }
130 public override KeySizes[] LegalKeySizes {
131 get { return LegalKeySizesValue; }
135 public bool PersistKeyInCsp {
136 get { return persistKey; }
137 set { persistKey = value; }
147 get { return dsa.PublicOnly; }
150 public override string SignatureAlgorithm {
151 get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }
155 private static bool useMachineKeyStore = false;
157 public static bool UseMachineKeyStore {
158 get { return useMachineKeyStore; }
159 set { useMachineKeyStore = value; }
163 public override DSAParameters ExportParameters (bool includePrivateParameters)
165 if ((includePrivateParameters) && (!privateKeyExportable)) {
166 throw new CryptographicException (
167 Locale.GetText ("Cannot export private key"));
170 return dsa.ExportParameters (includePrivateParameters);
173 public override void ImportParameters (DSAParameters parameters)
175 dsa.ImportParameters (parameters);
178 public override byte[] CreateSignature (byte[] rgbHash)
180 return dsa.CreateSignature (rgbHash);
183 public byte[] SignData (byte[] data)
185 // right now only SHA1 is supported by FIPS186-2
186 HashAlgorithm hash = SHA1.Create ();
187 byte[] toBeSigned = hash.ComputeHash (data);
188 return dsa.CreateSignature (toBeSigned);
191 public byte[] SignData (byte[] data, int offset, int count)
193 // right now only SHA1 is supported by FIPS186-2
194 HashAlgorithm hash = SHA1.Create ();
195 byte[] toBeSigned = hash.ComputeHash (data, offset, count);
196 return dsa.CreateSignature (toBeSigned);
199 public byte[] SignData (Stream inputStream)
201 // right now only SHA1 is supported by FIPS186-2
202 HashAlgorithm hash = SHA1.Create ();
203 byte[] toBeSigned = hash.ComputeHash (inputStream);
204 return dsa.CreateSignature (toBeSigned);
207 public byte[] SignHash (byte[] rgbHash, string str)
209 // right now only SHA1 is supported by FIPS186-2
210 if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0) {
212 throw new CryptographicException (Locale.GetText ("Only SHA1 is supported."));
215 return dsa.CreateSignature (rgbHash);
218 public bool VerifyData (byte[] rgbData, byte[] rgbSignature)
220 // right now only SHA1 is supported by FIPS186-2
221 HashAlgorithm hash = SHA1.Create();
222 byte[] toBeVerified = hash.ComputeHash (rgbData);
223 return dsa.VerifySignature (toBeVerified, rgbSignature);
226 // LAMESPEC: MD5 isn't allowed with DSA
227 public bool VerifyHash (byte[] rgbHash, string str, byte[] rgbSignature)
230 str = "SHA1"; // default value
231 if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0) {
232 throw new CryptographicException (Locale.GetText ("Only SHA1 is supported."));
235 return dsa.VerifySignature (rgbHash, rgbSignature);
238 public override bool VerifySignature (byte[] rgbHash, byte[] rgbSignature)
240 return dsa.VerifySignature (rgbHash, rgbSignature);
243 protected override void Dispose (bool disposing)
246 // the key is persisted and we do not want it persisted
247 if ((persisted) && (!persistKey)) {
248 store.Remove (); // delete the container
253 // no need as they all are abstract before us
260 private void OnKeyGenerated (object sender, EventArgs e)
262 // the key isn't persisted and we want it persisted
263 if ((persistKey) && (!persisted)) {
264 // save the current keypair
265 store.KeyValue = this.ToXmlString (!dsa.PublicOnly);
271 // ICspAsymmetricAlgorithm
273 [MonoTODO ("call into KeyPairPersistence to get details")]
275 public CspKeyContainerInfo CspKeyContainerInfo {
279 [MonoTODO ("call into CryptoConvert (doesn't currently support DSA)")]
281 public byte[] ExportCspBlob (bool includePrivateParameters)
283 throw new NotImplementedException ("CryptoConvert doesn't currently support DSA");
286 [MonoTODO ("call into CryptoConvert (doesn't currently support DSA)")]
288 public void ImportCspBlob (byte[] rawData)
291 throw new ArgumentNullException ("rawData");
292 throw new NotImplementedException ("CryptoConvert doesn't currently support DSA");