(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System.Security.Cryptography / DSACryptoServiceProvider.cs
blob7c29964a34351647c1a5f9d1e0c7baa825f0512a
1 //
2 // System.Security.Cryptography.DSACryptoServiceProvider.cs
3 //
4 // Authors:
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Sebastien Pouliot <sebastien@ximian.com>
7 // Ben Maurer (bmaurer@users.sf.net)
8 //
9 // (C) 2002
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)
16 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
25 //
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
28 //
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System;
39 using System.IO;
40 using System.Globalization;
42 using Mono.Security.Cryptography;
44 namespace System.Security.Cryptography {
46 #if NET_2_0
47 public sealed class DSACryptoServiceProvider : DSA, ICspAsymmetricAlgorithm {
48 #elif NET_1_1
49 public sealed class DSACryptoServiceProvider : DSA {
50 #else
51 public class DSACryptoServiceProvider : DSA {
52 #endif
53 private const int PROV_DSS_DH = 13; // from WinCrypt.h
55 private KeyPairPersistence store;
56 private bool persistKey;
57 private bool persisted;
59 private bool privateKeyExportable = true;
60 private bool m_disposed;
62 private DSAManaged dsa;
64 // MS implementation generates a keypair everytime a new DSA
65 // object is created (unless an existing key container is
66 // specified in the CspParameters).
67 // However we:
68 // (a) often use DSA to import an existing keypair.
69 // (b) take a LOT of time to generate the DSA group
70 // So we'll generate the keypair only when (and if) it's being
71 // used (or exported). This should save us a lot of time (at
72 // least in the unit tests).
74 public DSACryptoServiceProvider ()
75 : this (1024, null)
79 public DSACryptoServiceProvider (CspParameters parameters)
80 : this (1024, parameters)
84 public DSACryptoServiceProvider (int dwKeySize)
85 : this (dwKeySize, null)
89 public DSACryptoServiceProvider (int dwKeySize, CspParameters parameters)
91 LegalKeySizesValue = new KeySizes [1];
92 LegalKeySizesValue [0] = new KeySizes (512, 1024, 64);
94 // will throw an exception is key size isn't supported
95 KeySize = dwKeySize;
96 dsa = new DSAManaged (dwKeySize);
97 dsa.KeyGenerated += new DSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);
99 persistKey = (parameters != null);
100 if (parameters == null) {
101 parameters = new CspParameters (PROV_DSS_DH);
102 #if NET_1_1
103 if (useMachineKeyStore)
104 parameters.Flags |= CspProviderFlags.UseMachineKeyStore;
105 #endif
106 store = new KeyPairPersistence (parameters);
107 // no need to load - it cannot exists
109 else {
110 store = new KeyPairPersistence (parameters);
111 store.Load ();
112 if (store.KeyValue != null) {
113 persisted = true;
114 this.FromXmlString (store.KeyValue);
119 ~DSACryptoServiceProvider ()
121 Dispose (false);
124 // DSA isn't used for key exchange
125 public override string KeyExchangeAlgorithm {
126 get { return null; }
129 public override int KeySize {
130 get { return dsa.KeySize; }
133 #if !NET_2_0
134 public override KeySizes[] LegalKeySizes {
135 get { return LegalKeySizesValue; }
137 #endif
139 public bool PersistKeyInCsp {
140 get { return persistKey; }
141 set { persistKey = value; }
144 #if NET_2_0
145 public
146 #else
147 internal
148 #endif
149 bool PublicOnly {
150 get { return dsa.PublicOnly; }
153 public override string SignatureAlgorithm {
154 get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }
157 #if NET_1_1
158 private static bool useMachineKeyStore = false;
160 public static bool UseMachineKeyStore {
161 get { return useMachineKeyStore; }
162 set { useMachineKeyStore = value; }
164 #endif
166 public override DSAParameters ExportParameters (bool includePrivateParameters)
168 if ((includePrivateParameters) && (!privateKeyExportable)) {
169 throw new CryptographicException (
170 Locale.GetText ("Cannot export private key"));
173 return dsa.ExportParameters (includePrivateParameters);
176 public override void ImportParameters (DSAParameters parameters)
178 dsa.ImportParameters (parameters);
181 public override byte[] CreateSignature (byte[] rgbHash)
183 return dsa.CreateSignature (rgbHash);
186 public byte[] SignData (byte[] data)
188 // right now only SHA1 is supported by FIPS186-2
189 HashAlgorithm hash = SHA1.Create ();
190 byte[] toBeSigned = hash.ComputeHash (data);
191 return dsa.CreateSignature (toBeSigned);
194 public byte[] SignData (byte[] data, int offset, int count)
196 // right now only SHA1 is supported by FIPS186-2
197 HashAlgorithm hash = SHA1.Create ();
198 byte[] toBeSigned = hash.ComputeHash (data, offset, count);
199 return dsa.CreateSignature (toBeSigned);
202 public byte[] SignData (Stream inputStream)
204 // right now only SHA1 is supported by FIPS186-2
205 HashAlgorithm hash = SHA1.Create ();
206 byte[] toBeSigned = hash.ComputeHash (inputStream);
207 return dsa.CreateSignature (toBeSigned);
210 public byte[] SignHash (byte[] rgbHash, string str)
212 // right now only SHA1 is supported by FIPS186-2
213 if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0) {
214 // not documented
215 throw new CryptographicException (Locale.GetText ("Only SHA1 is supported."));
218 return dsa.CreateSignature (rgbHash);
221 public bool VerifyData (byte[] rgbData, byte[] rgbSignature)
223 // right now only SHA1 is supported by FIPS186-2
224 HashAlgorithm hash = SHA1.Create();
225 byte[] toBeVerified = hash.ComputeHash (rgbData);
226 return dsa.VerifySignature (toBeVerified, rgbSignature);
229 // LAMESPEC: MD5 isn't allowed with DSA
230 public bool VerifyHash (byte[] rgbHash, string str, byte[] rgbSignature)
232 if (str == null)
233 str = "SHA1"; // default value
234 if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0) {
235 throw new CryptographicException (Locale.GetText ("Only SHA1 is supported."));
238 return dsa.VerifySignature (rgbHash, rgbSignature);
241 public override bool VerifySignature (byte[] rgbHash, byte[] rgbSignature)
243 return dsa.VerifySignature (rgbHash, rgbSignature);
246 protected override void Dispose (bool disposing)
248 if (!m_disposed) {
249 // the key is persisted and we do not want it persisted
250 if ((persisted) && (!persistKey)) {
251 store.Remove (); // delete the container
253 if (dsa != null)
254 dsa.Clear ();
255 // call base class
256 // no need as they all are abstract before us
257 m_disposed = true;
261 // private stuff
263 private void OnKeyGenerated (object sender, EventArgs e)
265 // the key isn't persisted and we want it persisted
266 if ((persistKey) && (!persisted)) {
267 // save the current keypair
268 store.KeyValue = this.ToXmlString (!dsa.PublicOnly);
269 store.Save ();
270 persisted = true;
273 #if NET_2_0
274 // ICspAsymmetricAlgorithm
276 [MonoTODO ("call into KeyPairPersistence to get details")]
277 public CspKeyContainerInfo CspKeyContainerInfo {
278 get { return null; }
281 [MonoTODO ("call into CryptoConvert (doesn't currently support DSA)")]
282 public byte[] ExportCspBlob (bool includePrivateParameters)
284 return null;
287 [MonoTODO ("call into CryptoConvert (doesn't currently support DSA)")]
288 public void ImportCspBlob (byte[] rawData)
291 #endif