Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DSACryptoServiceProvider.cs
Go to the documentation of this file.
2using System.IO;
5
7
9{
10 private int _keySize;
11
12 private readonly CspParameters _parameters;
13
14 private readonly bool _randomKeyContainer;
15
17
19
20 private readonly SHA1 _sha1;
21
22 private static volatile CspProviderFlags s_useMachineKeyStore;
23
24 private bool _disposed;
25
27 {
28 get
29 {
30 if (_safeProvHandle == null)
31 {
32 lock (_parameters)
33 {
34 if (_safeProvHandle == null)
35 {
37 _safeProvHandle = safeProvHandle;
38 }
39 }
40 return _safeProvHandle;
41 }
42 return _safeProvHandle;
43 }
44 set
45 {
46 lock (_parameters)
47 {
48 SafeProvHandle safeProvHandle = _safeProvHandle;
49 if (value != safeProvHandle)
50 {
51 if (safeProvHandle != null)
52 {
53 SafeKeyHandle safeKeyHandle = _safeKeyHandle;
54 _safeKeyHandle = null;
55 safeKeyHandle?.Dispose();
56 safeProvHandle.Dispose();
57 }
59 }
60 }
61 }
62 }
63
65 {
66 get
67 {
68 if (_safeKeyHandle == null)
69 {
70 lock (_parameters)
71 {
72 if (_safeKeyHandle == null)
73 {
75 _safeKeyHandle = keyPairHelper;
76 }
77 }
78 }
79 return _safeKeyHandle;
80 }
81 set
82 {
83 lock (_parameters)
84 {
85 SafeKeyHandle safeKeyHandle = _safeKeyHandle;
86 if (value != safeKeyHandle)
87 {
89 safeKeyHandle?.Dispose();
90 }
91 }
92 }
93 }
94
95 [SupportedOSPlatform("windows")]
97 {
98 get
99 {
100 SafeKeyHandle safeKeyHandle = SafeKeyHandle;
102 }
103 }
104
105 public override int KeySize
106 {
107 get
108 {
109 byte[] keyParameter = CapiHelper.GetKeyParameter(SafeKeyHandle, 1);
111 return _keySize;
112 }
113 }
114
115 public override KeySizes[] LegalKeySizes => new KeySizes[1]
116 {
117 new KeySizes(512, 1024, 64)
118 };
119
120 public bool PersistKeyInCsp
121 {
122 get
123 {
125 }
126 set
127 {
128 bool persistKeyInCsp = PersistKeyInCsp;
129 if (value != persistKeyInCsp)
130 {
132 }
133 }
134 }
135
136 public bool PublicOnly
137 {
138 get
139 {
140 byte[] keyParameter = CapiHelper.GetKeyParameter(SafeKeyHandle, 2);
141 return keyParameter[0] == 1;
142 }
143 }
144
145 public static bool UseMachineKeyStore
146 {
147 get
148 {
149 return s_useMachineKeyStore == CspProviderFlags.UseMachineKeyStore;
150 }
151 set
152 {
153 s_useMachineKeyStore = (value ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags);
154 }
155 }
156
157 public override string? KeyExchangeAlgorithm => null;
158
159 public override string SignatureAlgorithm => "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
160
162 : this(new CspParameters(13, null, null, s_useMachineKeyStore))
163 {
164 }
165
166 public DSACryptoServiceProvider(int dwKeySize)
167 : this(dwKeySize, new CspParameters(13, null, null, s_useMachineKeyStore))
168 {
169 }
170
171 [SupportedOSPlatform("windows")]
173 : this(0, parameters)
174 {
175 }
176
177 [SupportedOSPlatform("windows")]
178 public DSACryptoServiceProvider(int dwKeySize, CspParameters? parameters)
179 {
180 if (dwKeySize < 0)
181 {
183 }
185 _keySize = dwKeySize;
186 _sha1 = SHA1.Create();
188 {
189 SafeKeyHandle safeKeyHandle = SafeKeyHandle;
190 }
191 }
192
193 protected override void Dispose(bool disposing)
194 {
195 if (disposing)
196 {
197 if (_safeKeyHandle != null && !_safeKeyHandle.IsClosed)
198 {
200 }
202 {
204 }
205 _disposed = true;
206 }
207 base.Dispose(disposing);
208 }
209
210 public byte[] ExportCspBlob(bool includePrivateParameters)
211 {
212 return CapiHelper.ExportKeyBlob(includePrivateParameters, SafeKeyHandle);
213 }
214
215 public override DSAParameters ExportParameters(bool includePrivateParameters)
216 {
217 byte[] cspBlob = ExportCspBlob(includePrivateParameters);
218 byte[] cspPublicBlob = null;
219 if (includePrivateParameters)
220 {
221 byte keyBlobHeaderVersion = CapiHelper.GetKeyBlobHeaderVersion(cspBlob);
222 if (keyBlobHeaderVersion <= 2)
223 {
224 cspPublicBlob = ExportCspBlob(includePrivateParameters: false);
225 }
226 }
227 return cspBlob.ToDSAParameters(includePrivateParameters, cspPublicBlob);
228 }
229
231 {
232 CapiHelper.AcquireCsp(new CspParameters(13), out var safeProvHandle);
233 return safeProvHandle;
234 }
235
236 public void ImportCspBlob(byte[] keyBlob)
237 {
239 SafeKeyHandle safeKeyHandle;
240 if (IsPublic(keyBlob))
241 {
242 SafeProvHandle safeProvHandle = AcquireSafeProviderHandle();
243 CapiHelper.ImportKeyBlob(safeProvHandle, CspProviderFlags.NoFlags, addNoSaltFlag: false, keyBlob, out safeKeyHandle);
244 SafeProvHandle = safeProvHandle;
245 }
246 else
247 {
248 CapiHelper.ImportKeyBlob(SafeProvHandle, _parameters.Flags, addNoSaltFlag: false, keyBlob, out safeKeyHandle);
249 }
250 SafeKeyHandle = safeKeyHandle;
251 }
252
253 public override void ImportParameters(DSAParameters parameters)
254 {
255 byte[] keyBlob = parameters.ToKeyBlob();
256 ImportCspBlob(keyBlob);
257 }
258
259 public override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> passwordBytes, ReadOnlySpan<byte> source, out int bytesRead)
260 {
262 base.ImportEncryptedPkcs8PrivateKey(passwordBytes, source, out bytesRead);
263 }
264
265 public override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<char> password, ReadOnlySpan<byte> source, out int bytesRead)
266 {
268 base.ImportEncryptedPkcs8PrivateKey(password, source, out bytesRead);
269 }
270
271 public byte[] SignData(Stream inputStream)
272 {
273 byte[] rgbHash = _sha1.ComputeHash(inputStream);
274 return SignHash(rgbHash, null);
275 }
276
277 public byte[] SignData(byte[] buffer)
278 {
279 byte[] rgbHash = _sha1.ComputeHash(buffer);
280 return SignHash(rgbHash, null);
281 }
282
283 public byte[] SignData(byte[] buffer, int offset, int count)
284 {
285 byte[] rgbHash = _sha1.ComputeHash(buffer, offset, count);
286 return SignHash(rgbHash, null);
287 }
288
289 public bool VerifyData(byte[] rgbData, byte[] rgbSignature)
290 {
291 byte[] rgbHash = _sha1.ComputeHash(rgbData);
292 return VerifyHash(rgbHash, null, rgbSignature);
293 }
294
295 public override byte[] CreateSignature(byte[] rgbHash)
296 {
297 return SignHash(rgbHash, null);
298 }
299
300 public override bool VerifySignature(byte[] rgbHash, byte[] rgbSignature)
301 {
302 return VerifyHash(rgbHash, null, rgbSignature);
303 }
304
305 protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
306 {
307 if (hashAlgorithm != HashAlgorithmName.SHA1)
308 {
310 }
311 return _sha1.ComputeHash(data, offset, count);
312 }
313
314 protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
315 {
316 if (hashAlgorithm != HashAlgorithmName.SHA1)
317 {
319 }
320 return _sha1.ComputeHash(data);
321 }
322
323 public byte[] SignHash(byte[] rgbHash, string? str)
324 {
325 if (rgbHash == null)
326 {
327 throw new ArgumentNullException("rgbHash");
328 }
329 if (PublicOnly)
330 {
332 }
333 int calgHash = CapiHelper.NameOrOidToHashAlgId(str, OidGroup.HashAlgorithm);
334 if (rgbHash.Length != _sha1.HashSize / 8)
335 {
337 }
338 return CapiHelper.SignValue(SafeProvHandle, SafeKeyHandle, _parameters.KeyNumber, 8704, calgHash, rgbHash);
339 }
340
341 public bool VerifyHash(byte[] rgbHash, string? str, byte[] rgbSignature)
342 {
343 if (rgbHash == null)
344 {
345 throw new ArgumentNullException("rgbHash");
346 }
347 if (rgbSignature == null)
348 {
349 throw new ArgumentNullException("rgbSignature");
350 }
351 int calgHash = CapiHelper.NameOrOidToHashAlgId(str, OidGroup.HashAlgorithm);
352 return CapiHelper.VerifySign(SafeProvHandle, SafeKeyHandle, 8704, calgHash, rgbHash, rgbSignature);
353 }
354
355 private static bool IsPublic(byte[] keyBlob)
356 {
357 if (keyBlob == null)
358 {
359 throw new ArgumentNullException("keyBlob");
360 }
361 if (keyBlob[0] != 6)
362 {
363 return false;
364 }
365 if ((keyBlob[11] != 49 && keyBlob[11] != 51) || keyBlob[10] != 83 || keyBlob[9] != 83 || keyBlob[8] != 68)
366 {
367 return false;
368 }
369 return true;
370 }
371
372 private void ThrowIfDisposed()
373 {
374 if (_disposed)
375 {
376 throw new ObjectDisposedException("DSACryptoServiceProvider");
377 }
378 }
379}
static void AcquireCsp(CspParameters cspParameters, out SafeProvHandle safeProvHandle)
static SafeKeyHandle GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, int keySize, SafeProvHandle safeProvHandle)
static bool GetPersistKeyInCsp(SafeProvHandle safeProvHandle)
static void SetPersistKeyInCsp(SafeProvHandle safeProvHandle, bool fPersistKeyInCsp)
static SafeProvHandle CreateProvHandle(CspParameters parameters, bool randomKeyContainer)
static CspParameters SaveCspParameters(CspAlgorithmType keyType, CspParameters userParameters, CspProviderFlags defaultFlags, out bool randomKeyContainer)
static byte GetKeyBlobHeaderVersion(byte[] cspBlob)
static int NameOrOidToHashAlgId(string nameOrOid, OidGroup oidGroup)
static void ImportKeyBlob(SafeProvHandle saveProvHandle, CspProviderFlags flags, bool addNoSaltFlag, byte[] keyBlob, out SafeKeyHandle safeKeyHandle)
static byte[] SignValue(SafeProvHandle hProv, SafeKeyHandle hKey, int keyNumber, int calgKey, int calgHash, byte[] hash)
static byte[] ExportKeyBlob(bool includePrivateParameters, SafeKeyHandle safeKeyHandle)
static byte[] GetKeyParameter(SafeKeyHandle safeKeyHandle, int keyParam)
static bool VerifySign(SafeProvHandle hProv, SafeKeyHandle hKey, int calgKey, int calgHash, byte[] hash, byte[] signature)
static int ReadInt32LittleEndian(ReadOnlySpan< byte > source)
static string Cryptography_UnknownHashAlgorithm
Definition SR.cs:152
static string Cryptography_InvalidHashSize
Definition SR.cs:32
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Cryptography_CSP_NoPrivateKey
Definition SR.cs:48
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
override bool VerifySignature(byte[] rgbHash, byte[] rgbSignature)
DSACryptoServiceProvider(int dwKeySize, CspParameters? parameters)
override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, ReadOnlySpan< byte > source, out int bytesRead)
override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > source, out int bytesRead)
byte[] SignData(byte[] buffer, int offset, int count)
bool VerifyHash(byte[] rgbHash, string? str, byte[] rgbSignature)
override DSAParameters ExportParameters(bool includePrivateParameters)
override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
static new SHA1 Create()
Definition SHA1.cs:55
override void Dispose(bool disposing)
override void Dispose(bool disposing)