mscorlib(4.0.0.0) API with additions
DSACryptoServiceProvider.cs
1 using System.IO;
5 
7 {
9  [ComVisible(true)]
11  {
12  private int _dwKeySize;
13 
14  private CspParameters _parameters;
15 
16  private bool _randomKeyContainer;
17 
18  [SecurityCritical]
19  private SafeProvHandle _safeProvHandle;
20 
21  [SecurityCritical]
22  private SafeKeyHandle _safeKeyHandle;
23 
24  private SHA1CryptoServiceProvider _sha1;
25 
26  private static volatile CspProviderFlags s_UseMachineKeyStore;
27 
31  [ComVisible(false)]
32  public bool PublicOnly
33  {
34  [SecuritySafeCritical]
35  get
36  {
37  GetKeyPair();
38  byte[] array = Utils._GetKeyParameter(_safeKeyHandle, 2u);
39  return array[0] == 1;
40  }
41  }
42 
45  [ComVisible(false)]
47  {
48  [SecuritySafeCritical]
49  get
50  {
51  GetKeyPair();
52  return new CspKeyContainerInfo(_parameters, _randomKeyContainer);
53  }
54  }
55 
58  public override int KeySize
59  {
60  [SecuritySafeCritical]
61  get
62  {
63  GetKeyPair();
64  byte[] array = Utils._GetKeyParameter(_safeKeyHandle, 1u);
65  _dwKeySize = (array[0] | (array[1] << 8) | (array[2] << 16) | (array[3] << 24));
66  return _dwKeySize;
67  }
68  }
69 
72  public override string KeyExchangeAlgorithm => null;
73 
76  public override string SignatureAlgorithm => "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
77 
81  public static bool UseMachineKeyStore
82  {
83  get
84  {
85  return s_UseMachineKeyStore == CspProviderFlags.UseMachineKeyStore;
86  }
87  set
88  {
89  s_UseMachineKeyStore = (value ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags);
90  }
91  }
92 
96  public bool PersistKeyInCsp
97  {
98  [SecuritySafeCritical]
99  get
100  {
101  if (_safeProvHandle == null)
102  {
103  lock (this)
104  {
105  if (_safeProvHandle == null)
106  {
107  _safeProvHandle = Utils.CreateProvHandle(_parameters, _randomKeyContainer);
108  }
109  }
110  }
111  return Utils.GetPersistKeyInCsp(_safeProvHandle);
112  }
113  [SecuritySafeCritical]
114  set
115  {
116  bool persistKeyInCsp = PersistKeyInCsp;
117  if (value != persistKeyInCsp)
118  {
119  KeyContainerPermission keyContainerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
120  if (!value)
121  {
123  keyContainerPermission.AccessEntries.Add(accessEntry);
124  }
125  else
126  {
128  keyContainerPermission.AccessEntries.Add(accessEntry2);
129  }
130  keyContainerPermission.Demand();
131  Utils.SetPersistKeyInCsp(_safeProvHandle, value);
132  }
133  }
134  }
135 
138  : this(0, new CspParameters(13, null, null, s_UseMachineKeyStore))
139  {
140  }
141 
144  public DSACryptoServiceProvider(int dwKeySize)
145  : this(dwKeySize, new CspParameters(13, null, null, s_UseMachineKeyStore))
146  {
147  }
148 
152  : this(0, parameters)
153  {
154  }
155 
162  [SecuritySafeCritical]
163  public DSACryptoServiceProvider(int dwKeySize, CspParameters parameters)
164  {
165  if (dwKeySize < 0)
166  {
167  throw new ArgumentOutOfRangeException("dwKeySize", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
168  }
169  _parameters = Utils.SaveCspParameters(CspAlgorithmType.Dss, parameters, s_UseMachineKeyStore, ref _randomKeyContainer);
171  {
172  new KeySizes(512, 1024, 64)
173  };
174  _dwKeySize = dwKeySize;
175  _sha1 = new SHA1CryptoServiceProvider();
176  if (!_randomKeyContainer || Environment.GetCompatibilityFlag(CompatibilityFlag.EagerlyGenerateRandomAsymmKeys))
177  {
178  GetKeyPair();
179  }
180  }
181 
182  [SecurityCritical]
183  private void GetKeyPair()
184  {
185  if (_safeKeyHandle == null)
186  {
187  lock (this)
188  {
189  if (_safeKeyHandle == null)
190  {
191  Utils.GetKeyPairHelper(CspAlgorithmType.Dss, _parameters, _randomKeyContainer, _dwKeySize, ref _safeProvHandle, ref _safeKeyHandle);
192  }
193  }
194  }
195  }
196 
197  [SecuritySafeCritical]
198  protected override void Dispose(bool disposing)
199  {
200  base.Dispose(disposing);
201  if (_safeKeyHandle != null && !_safeKeyHandle.IsClosed)
202  {
203  _safeKeyHandle.Dispose();
204  }
205  if (_safeProvHandle != null && !_safeProvHandle.IsClosed)
206  {
207  _safeProvHandle.Dispose();
208  }
209  }
210 
216  [SecuritySafeCritical]
217  public override DSAParameters ExportParameters(bool includePrivateParameters)
218  {
219  GetKeyPair();
220  if (includePrivateParameters)
221  {
222  KeyContainerPermission keyContainerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
224  keyContainerPermission.AccessEntries.Add(accessEntry);
225  keyContainerPermission.Demand();
226  }
227  DSACspObject dSACspObject = new DSACspObject();
228  int blobType = includePrivateParameters ? 7 : 6;
229  Utils._ExportKey(_safeKeyHandle, blobType, dSACspObject);
230  return DSAObjectToStruct(dSACspObject);
231  }
232 
237  [SecuritySafeCritical]
238  [ComVisible(false)]
239  public byte[] ExportCspBlob(bool includePrivateParameters)
240  {
241  GetKeyPair();
242  return Utils.ExportCspBlobHelper(includePrivateParameters, _parameters, _safeKeyHandle);
243  }
244 
248  [SecuritySafeCritical]
249  public override void ImportParameters(DSAParameters parameters)
250  {
251  DSACspObject cspObject = DSAStructToObject(parameters);
252  if (_safeKeyHandle != null && !_safeKeyHandle.IsClosed)
253  {
254  _safeKeyHandle.Dispose();
255  }
256  _safeKeyHandle = SafeKeyHandle.InvalidHandle;
257  if (IsPublic(parameters))
258  {
259  Utils._ImportKey(Utils.StaticDssProvHandle, 8704, CspProviderFlags.NoFlags, cspObject, ref _safeKeyHandle);
260  return;
261  }
262  KeyContainerPermission keyContainerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
264  keyContainerPermission.AccessEntries.Add(accessEntry);
265  keyContainerPermission.Demand();
266  if (_safeProvHandle == null)
267  {
268  _safeProvHandle = Utils.CreateProvHandle(_parameters, _randomKeyContainer);
269  }
270  Utils._ImportKey(_safeProvHandle, 8704, _parameters.Flags, cspObject, ref _safeKeyHandle);
271  }
272 
275  [SecuritySafeCritical]
276  [ComVisible(false)]
277  public void ImportCspBlob(byte[] keyBlob)
278  {
279  Utils.ImportCspBlobHelper(CspAlgorithmType.Dss, keyBlob, IsPublic(keyBlob), ref _parameters, _randomKeyContainer, ref _safeProvHandle, ref _safeKeyHandle);
280  }
281 
285  public byte[] SignData(Stream inputStream)
286  {
287  byte[] rgbHash = _sha1.ComputeHash(inputStream);
288  return SignHash(rgbHash, null);
289  }
290 
294  public byte[] SignData(byte[] buffer)
295  {
296  byte[] rgbHash = _sha1.ComputeHash(buffer);
297  return SignHash(rgbHash, null);
298  }
299 
305  public byte[] SignData(byte[] buffer, int offset, int count)
306  {
307  byte[] rgbHash = _sha1.ComputeHash(buffer, offset, count);
308  return SignHash(rgbHash, null);
309  }
310 
316  public bool VerifyData(byte[] rgbData, byte[] rgbSignature)
317  {
318  byte[] rgbHash = _sha1.ComputeHash(rgbData);
319  return VerifyHash(rgbHash, null, rgbSignature);
320  }
321 
325  public override byte[] CreateSignature(byte[] rgbHash)
326  {
327  return SignHash(rgbHash, null);
328  }
329 
335  public override bool VerifySignature(byte[] rgbHash, byte[] rgbSignature)
336  {
337  return VerifyHash(rgbHash, null, rgbSignature);
338  }
339 
340  protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
341  {
342  if (hashAlgorithm != HashAlgorithmName.SHA1)
343  {
344  throw new CryptographicException(Environment.GetResourceString("Cryptography_UnknownHashAlgorithm", hashAlgorithm.Name));
345  }
346  return _sha1.ComputeHash(data, offset, count);
347  }
348 
349  protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
350  {
351  if (hashAlgorithm != HashAlgorithmName.SHA1)
352  {
353  throw new CryptographicException(Environment.GetResourceString("Cryptography_UnknownHashAlgorithm", hashAlgorithm.Name));
354  }
355  return _sha1.ComputeHash(data);
356  }
357 
364  [SecuritySafeCritical]
365  public byte[] SignHash(byte[] rgbHash, string str)
366  {
367  if (rgbHash == null)
368  {
369  throw new ArgumentNullException("rgbHash");
370  }
371  if (PublicOnly)
372  {
373  throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_NoPrivateKey"));
374  }
375  int calgHash = X509Utils.NameOrOidToAlgId(str, OidGroup.HashAlgorithm);
376  if (rgbHash.Length != _sha1.HashSize / 8)
377  {
378  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidHashSize", "SHA1", _sha1.HashSize / 8));
379  }
380  GetKeyPair();
382  {
383  KeyContainerPermission keyContainerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
385  keyContainerPermission.AccessEntries.Add(accessEntry);
386  keyContainerPermission.Demand();
387  }
388  return Utils.SignValue(_safeKeyHandle, _parameters.KeyNumber, 8704, calgHash, rgbHash);
389  }
390 
399  [SecuritySafeCritical]
400  public bool VerifyHash(byte[] rgbHash, string str, byte[] rgbSignature)
401  {
402  if (rgbHash == null)
403  {
404  throw new ArgumentNullException("rgbHash");
405  }
406  if (rgbSignature == null)
407  {
408  throw new ArgumentNullException("rgbSignature");
409  }
410  int calgHash = X509Utils.NameOrOidToAlgId(str, OidGroup.HashAlgorithm);
411  if (rgbHash.Length != _sha1.HashSize / 8)
412  {
413  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidHashSize", "SHA1", _sha1.HashSize / 8));
414  }
415  GetKeyPair();
416  return Utils.VerifySign(_safeKeyHandle, 8704, calgHash, rgbHash, rgbSignature);
417  }
418 
419  private static DSAParameters DSAObjectToStruct(DSACspObject dsaCspObject)
420  {
421  DSAParameters result = default(DSAParameters);
422  result.P = dsaCspObject.P;
423  result.Q = dsaCspObject.Q;
424  result.G = dsaCspObject.G;
425  result.Y = dsaCspObject.Y;
426  result.J = dsaCspObject.J;
427  result.X = dsaCspObject.X;
428  result.Seed = dsaCspObject.Seed;
429  result.Counter = dsaCspObject.Counter;
430  return result;
431  }
432 
433  private static DSACspObject DSAStructToObject(DSAParameters dsaParams)
434  {
435  DSACspObject dSACspObject = new DSACspObject();
436  dSACspObject.P = dsaParams.P;
437  dSACspObject.Q = dsaParams.Q;
438  dSACspObject.G = dsaParams.G;
439  dSACspObject.Y = dsaParams.Y;
440  dSACspObject.J = dsaParams.J;
441  dSACspObject.X = dsaParams.X;
442  dSACspObject.Seed = dsaParams.Seed;
443  dSACspObject.Counter = dsaParams.Counter;
444  return dSACspObject;
445  }
446 
447  private static bool IsPublic(DSAParameters dsaParams)
448  {
449  return dsaParams.X == null;
450  }
451 
452  private static bool IsPublic(byte[] keyBlob)
453  {
454  if (keyBlob == null)
455  {
456  throw new ArgumentNullException("keyBlob");
457  }
458  if (keyBlob[0] != 6)
459  {
460  return false;
461  }
462  if ((keyBlob[11] != 49 && keyBlob[11] != 51) || keyBlob[10] != 83 || keyBlob[9] != 83 || keyBlob[8] != 68)
463  {
464  return false;
465  }
466  return true;
467  }
468  }
469 }
The exception that is thrown when an error occurs during a cryptographic operation.
bool VerifyHash(byte[] rgbHash, string str, byte[] rgbSignature)
Verifies the specified signature data by comparing it to the signature computed for the specified has...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
override byte [] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
When overridden in a derived class, computes the hash value of a specified portion of a byte array by...
static bool? UseMachineKeyStore
Gets or sets a value indicating whether the key should be persisted in the computer's key store inste...
byte [] J
Specifies the J parameter for the T:System.Security.Cryptography.DSA algorithm.
override byte [] HashData(Stream data, HashAlgorithmName hashAlgorithm)
When overridden in a derived class, computes the hash value of a specified binary stream by using a s...
int KeyNumber
Specifies whether an asymmetric key is created as a signature key or an exchange key.
string Name
Gets the underlying string representation of the algorithm name.
byte [] SignData(byte[] buffer, int offset, int count)
Signs a byte array from the specified start point to the specified end point.
byte [] P
Specifies the P parameter for the T:System.Security.Cryptography.DSA algorithm.
byte [] X
Specifies the X parameter for the T:System.Security.Cryptography.DSA algorithm.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
byte [] Q
Specifies the Q parameter for the T:System.Security.Cryptography.DSA algorithm.
Specifies access rights for specific key containers. This class cannot be inherited.
CspProviderFlags
Specifies flags that modify the behavior of the cryptographic service providers (CSP).
bool PersistKeyInCsp
Gets or sets a value indicating whether the key should be persisted in the cryptographic service prov...
DSACryptoServiceProvider()
Initializes a new instance of the T:System.Security.Cryptography.DSACryptoServiceProvider class.
OidGroup
Identifies Windows cryptographic object identifier (OID) groups.
Definition: OidGroup.cs:4
static HashAlgorithmName SHA1
Gets a hash algorithm name that represents "SHA1".
Controls the ability to access key containers. This class cannot be inherited.
override byte [] CreateSignature(byte[] rgbHash)
Creates the T:System.Security.Cryptography.DSA signature for the specified data.
Defines methods that allow an T:System.Security.Cryptography.AsymmetricAlgorithm class to enumerate k...
override void ImportParameters(DSAParameters parameters)
Imports the specified T:System.Security.Cryptography.DSAParameters.
DSACryptoServiceProvider(int dwKeySize)
Initializes a new instance of the T:System.Security.Cryptography.DSACryptoServiceProvider class with ...
Specifies the name of a cryptographic hash algorithm.
KeySizes [] LegalKeySizesValue
Specifies the key sizes that are supported by the asymmetric algorithm.
byte [] SignData(Stream inputStream)
Computes the hash value of the specified input stream and signs the resulting hash value.
byte [] Seed
Specifies the seed for the T:System.Security.Cryptography.DSA algorithm.
Represents the abstract base class from which all implementations of the Digital Signature Algorithm ...
Definition: DSA.cs:10
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
override int KeySize
Gets the size of the key used by the asymmetric algorithm in bits.
byte [] ExportCspBlob(bool includePrivateParameters)
Exports a blob containing the key information associated with a T:System.Security....
byte [] SignHash(byte[] rgbHash, string str)
Computes the signature for the specified hash value by encrypting it with the private key.
byte [] Y
Specifies the Y parameter for the T:System.Security.Cryptography.DSA algorithm.
bool VerifyData(byte[] rgbData, byte[] rgbSignature)
Verifies the specified signature data by comparing it to the signature computed for the specified dat...
int Add(KeyContainerPermissionAccessEntry accessEntry)
Adds a T:System.Security.Permissions.KeyContainerPermissionAccessEntry object to the collection.
KeyContainerPermissionAccessEntryCollection AccessEntries
Gets the collection of T:System.Security.Permissions.KeyContainerPermissionAccessEntry objects associ...
override DSAParameters ExportParameters(bool includePrivateParameters)
Exports the T:System.Security.Cryptography.DSAParameters.
byte [] SignData(byte[] buffer)
Computes the hash value of the specified byte array and signs the resulting hash value.
Contains parameters that are passed to the cryptographic service provider (CSP) that performs cryptog...
Definition: CspParameters.cs:8
KeyContainerPermissionFlags
Specifies the type of key container access allowed.
override string SignatureAlgorithm
Gets the name of the signature algorithm.
Determines the set of valid key sizes for the symmetric cryptographic algorithms.
Definition: KeySizes.cs:7
byte [] G
Specifies the G parameter for the T:System.Security.Cryptography.DSA algorithm.
void ImportCspBlob(byte[] keyBlob)
Imports a blob that represents DSA key information.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
Defines a wrapper object to access the cryptographic service provider (CSP) implementation of the T:S...
bool PublicOnly
Gets a value that indicates whether the T:System.Security.Cryptography.DSACryptoServiceProvider objec...
DSACryptoServiceProvider(int dwKeySize, CspParameters parameters)
Initializes a new instance of the T:System.Security.Cryptography.DSACryptoServiceProvider class with ...
Provides additional information about a cryptographic key pair. This class cannot be inherited.
override string KeyExchangeAlgorithm
Gets the name of the key exchange algorithm.
CspKeyContainerInfo CspKeyContainerInfo
Gets a T:System.Security.Cryptography.CspKeyContainerInfo object that describes additional informatio...
DSACryptoServiceProvider(CspParameters parameters)
Initializes a new instance of the T:System.Security.Cryptography.DSACryptoServiceProvider class with ...
virtual int HashSize
Gets the size, in bits, of the computed hash code.
int Counter
Specifies the counter for the T:System.Security.Cryptography.DSA algorithm.
override bool VerifySignature(byte[] rgbHash, byte[] rgbSignature)
Verifies the T:System.Security.Cryptography.DSA signature for the specified data.
override void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.Security.Cryptography.AsymmetricAlgorithm class...
CspProviderFlags Flags
Represents the flags for T:System.Security.Cryptography.CspParameters that modify the behavior of the...
bool RandomlyGenerated
Gets a value indicating whether a key container was randomly generated by a managed cryptography clas...
byte [] ComputeHash(Stream inputStream)
Computes the hash value for the specified T:System.IO.Stream object.
Contains the typical parameters for the T:System.Security.Cryptography.DSA algorithm.
Definition: DSAParameters.cs:8
Computes the T:System.Security.Cryptography.SHA1 hash value for the input data using the implementati...
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16