2004-04-25 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[mono.git] / mcs / class / corlib / System.Security.Cryptography / DSACryptoServiceProvider.cs
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)
13 //
14
15 using System;
16 using System.IO;
17 using System.Globalization;
18
19 using Mono.Security.Cryptography;
20
21 namespace System.Security.Cryptography {
22
23 #if NET_1_0
24         public class DSACryptoServiceProvider : DSA {
25 #else
26         public sealed class DSACryptoServiceProvider : DSA {
27 #endif
28                 private const int PROV_DSS = 3;         // from WinCrypt.h
29
30                 private KeyPairPersistence store;
31                 private bool persistKey;
32                 private bool persisted;
33
34                 private bool privateKeyExportable = true;
35                 private bool m_disposed;
36
37                 private DSAManaged dsa;
38
39                 // MS implementation generates a keypair everytime a new DSA 
40                 // object is created (unless an existing key container is 
41                 // specified in the CspParameters).
42                 // However we:
43                 // (a) often use DSA to import an existing keypair.
44                 // (b) take a LOT of time to generate the DSA group
45                 // So we'll generate the keypair only when (and if) it's being
46                 // used (or exported). This should save us a lot of time (at
47                 // least in the unit tests).
48
49                 public DSACryptoServiceProvider () : this (1024, null) {}
50
51                 public DSACryptoServiceProvider (CspParameters parameters) : this (1024, parameters) {}
52
53                 public DSACryptoServiceProvider (int dwKeySize) : this (dwKeySize, null) {}
54
55                 public DSACryptoServiceProvider (int dwKeySize, CspParameters parameters)
56                 {
57                         LegalKeySizesValue = new KeySizes [1];
58                         LegalKeySizesValue [0] = new KeySizes (512, 1024, 64);
59
60                         // will throw an exception is key size isn't supported
61                         KeySize = dwKeySize;
62                         dsa = new DSAManaged (dwKeySize);
63                         dsa.KeyGenerated += new DSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);
64
65                         persistKey = (parameters != null);
66                         if (parameters == null) {
67                                 parameters = new CspParameters (PROV_DSS);
68 #if ! NET_1_0
69                                 if (useMachineKeyStore)
70                                         parameters.Flags |= CspProviderFlags.UseMachineKeyStore;
71 #endif
72                                 store = new KeyPairPersistence (parameters);
73                                 // no need to load - it cannot exists
74                         }
75                         else {
76                                 store = new KeyPairPersistence (parameters);
77                                 store.Load ();
78                                 if (store.KeyValue != null) {
79                                         persisted = true;
80                                         this.FromXmlString (store.KeyValue);
81                                 }
82                         }
83                 }
84
85                 ~DSACryptoServiceProvider ()
86                 {
87                         Dispose (false);
88                 }
89
90                 // DSA isn't used for key exchange
91                 public override string KeyExchangeAlgorithm {
92                         get { return null; }
93                 }
94
95                 public override int KeySize {
96                         get { return dsa.KeySize; }
97                 }
98
99                 public override KeySizes[] LegalKeySizes {
100                         get { return LegalKeySizesValue; }
101                 }
102
103                 public bool PersistKeyInCsp {
104                         get { return persistKey; }
105                         set {
106                                 persistKey = value;
107                                 if (persistKey)
108                                         OnKeyGenerated (dsa);
109                         }
110                 }
111
112 #if (NET_1_0 || NET_1_1)
113                 internal
114 #else
115                 public 
116 #endif
117                 bool PublicOnly {
118                         get { return dsa.PublicOnly; }
119                 }
120
121                 public override string SignatureAlgorithm {
122                         get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }
123                 }
124
125 #if ! NET_1_0
126                 private static bool useMachineKeyStore = false;
127
128                 public static bool UseMachineKeyStore {
129                         get { return useMachineKeyStore; }
130                         set { useMachineKeyStore = value; }
131                 }
132 #endif
133
134                 public override DSAParameters ExportParameters (bool includePrivateParameters) 
135                 {
136                         if ((includePrivateParameters) && (!privateKeyExportable))
137                                 throw new CryptographicException ("cannot export private key");
138
139                         return dsa.ExportParameters (includePrivateParameters);
140                 }
141
142                 public override void ImportParameters (DSAParameters parameters) 
143                 {
144                         dsa.ImportParameters (parameters);
145                 }
146
147                 public override byte[] CreateSignature (byte[] rgbHash)
148                 {
149                         return dsa.CreateSignature (rgbHash);
150                 }
151
152                 public byte[] SignData (byte[] data)
153                 {
154                         return dsa.CreateSignature (data);
155                 }
156
157                 public byte[] SignData (byte[] data, int offset, int count)
158                 {
159                         // right now only SHA1 is supported by FIPS186-2
160                         HashAlgorithm hash = SHA1.Create ();
161                         byte[] toBeSigned = hash.ComputeHash (data, offset, count);
162                         return dsa.CreateSignature (toBeSigned);
163                 }
164
165                 public byte[] SignData (Stream inputStream)
166                 {
167                         // right now only SHA1 is supported by FIPS186-2
168                         HashAlgorithm hash = SHA1.Create ();
169                         byte[] toBeSigned = hash.ComputeHash (inputStream);
170                         return dsa.CreateSignature (toBeSigned);
171                 }
172
173                 public byte[] SignHash (byte[] rgbHash, string str)
174                 {
175                         // right now only SHA1 is supported by FIPS186-2
176                         if (String.Compare (str, "SHA1", true, CultureInfo.InvariantCulture) != 0)
177                                 throw new Exception (); // not documented
178                         return dsa.CreateSignature (rgbHash);
179                 }
180
181                 public bool VerifyData (byte[] rgbData, byte[] rgbSignature)
182                 {
183                         // right now only SHA1 is supported by FIPS186-2
184                         HashAlgorithm hash = SHA1.Create();
185                         byte[] toBeVerified = hash.ComputeHash (rgbData);
186                         return dsa.VerifySignature (toBeVerified, rgbSignature);
187                 }
188
189                 // LAMESPEC: MD5 isn't allowed with DSA
190                 public bool VerifyHash (byte[] rgbHash, string str, byte[] rgbSignature)
191                 {
192                         if (str == null)
193                                 str = "SHA1"; // default value
194                         if (str != "SHA1")
195                                 throw new CryptographicException ();
196                         return dsa.VerifySignature (rgbHash, rgbSignature);
197                 }
198
199                 public override bool VerifySignature (byte[] rgbHash, byte[] rgbSignature)
200                 {
201                         return dsa.VerifySignature (rgbHash, rgbSignature);
202                 }
203
204                 protected override void Dispose (bool disposing) 
205                 {
206                         if (!m_disposed) {
207                                 // the key is persisted and we do not want it persisted
208                                 if ((persisted) && (!persistKey)) {
209                                         store.Remove ();        // delete the container
210                                 }
211                                 if (dsa != null)
212                                         dsa.Clear ();
213                                 // call base class 
214                                 // no need as they all are abstract before us
215                                 m_disposed = true;
216                         }
217                 }
218
219                 // private stuff
220
221                 private void OnKeyGenerated (object sender) 
222                 {
223                         // the key isn't persisted and we want it persisted
224                         if ((persistKey) && (!persisted)) {
225                                 // save the current keypair
226                                 store.KeyValue = this.ToXmlString (!dsa.PublicOnly);
227                                 store.Save ();
228                                 persisted = true;
229                         }
230                 }
231         }
232 }