-
Notifications
You must be signed in to change notification settings - Fork 401
/
AsymmetricAdapter.cs
467 lines (406 loc) · 18.2 KB
/
AsymmetricAdapter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
#if NET6_0_OR_GREATER
using System.Buffers;
using System.Diagnostics;
#endif
using System.Security.Cryptography;
using Microsoft.IdentityModel.Logging;
namespace Microsoft.IdentityModel.Tokens
{
delegate byte[] EncryptDelegate(byte[] bytes);
delegate byte[] DecryptDelegate(byte[] bytes);
delegate byte[] SignDelegate(byte[] bytes);
delegate byte[] SignUsingOffsetDelegate(byte[] bytes, int offset, int count);
#if NET6_0_OR_GREATER
delegate bool SignUsingSpanDelegate(ReadOnlySpan<byte> bytes, Span<byte> signature, out int bytesWritten);
#endif
delegate bool VerifyDelegate(byte[] bytes, byte[] signature);
delegate bool VerifyUsingOffsetDelegate(byte[] bytes, int offset, int count, byte[] signature);
/// <summary>
/// This adapter abstracts the 'RSA' differences between versions of .NET targets.
/// </summary>
internal class AsymmetricAdapter : IDisposable
{
#if DESKTOP
private bool _useRSAOeapPadding = false;
#endif
private bool _disposeCryptoOperators = false;
private bool _disposed = false;
private DecryptDelegate _decryptFunction = DecryptFunctionNotFound;
private EncryptDelegate _encryptFunction = EncryptFunctionNotFound;
private SignDelegate _signFunction = SignFunctionNotFound;
private SignUsingOffsetDelegate _signUsingOffsetFunction = SignUsingOffsetNotFound;
#if NET6_0_OR_GREATER
private SignUsingSpanDelegate _signUsingSpanFunction = SignUsingSpanNotFound;
#endif
private VerifyDelegate _verifyFunction = VerifyNotFound;
private VerifyUsingOffsetDelegate _verifyUsingOffsetFunction = VerifyUsingOffsetNotFound;
// Encryption algorithms do not need a HashAlgorithm, this is called by RSAKeyWrap
internal AsymmetricAdapter(SecurityKey key, string algorithm, bool requirePrivateKey)
: this(key, algorithm, null, requirePrivateKey)
{
}
internal AsymmetricAdapter(SecurityKey key, string algorithm, HashAlgorithm hashAlgorithm, HashAlgorithmName hashAlgorithmName, bool requirePrivateKey)
: this(key, algorithm, hashAlgorithm, requirePrivateKey)
{
HashAlgorithmName = hashAlgorithmName;
}
internal AsymmetricAdapter(SecurityKey key, string algorithm, HashAlgorithm hashAlgorithm, bool requirePrivateKey)
{
HashAlgorithm = hashAlgorithm;
// RsaSecurityKey has either Rsa OR RsaParameters.
// If we use the RsaParameters, we create a new RSA object and will need to dispose.
if (key is RsaSecurityKey rsaKey)
{
InitializeUsingRsaSecurityKey(rsaKey, algorithm);
}
else if (key is X509SecurityKey x509Key)
{
InitializeUsingX509SecurityKey(x509Key, algorithm, requirePrivateKey);
}
else if (key is JsonWebKey jsonWebKey)
{
if (JsonWebKeyConverter.TryConvertToSecurityKey(jsonWebKey, out SecurityKey securityKey))
{
if (securityKey is RsaSecurityKey rsaSecurityKeyFromJsonWebKey)
InitializeUsingRsaSecurityKey(rsaSecurityKeyFromJsonWebKey, algorithm);
else if (securityKey is X509SecurityKey x509SecurityKeyFromJsonWebKey)
InitializeUsingX509SecurityKey(x509SecurityKeyFromJsonWebKey, algorithm, requirePrivateKey);
else if (securityKey is ECDsaSecurityKey edcsaSecurityKeyFromJsonWebKey)
InitializeUsingEcdsaSecurityKey(edcsaSecurityKeyFromJsonWebKey);
else
throw LogHelper.LogExceptionMessage(new NotSupportedException(LogHelper.FormatInvariant(LogMessages.IDX10684, LogHelper.MarkAsNonPII(algorithm), key)));
}
}
else if (key is ECDsaSecurityKey ecdsaKey)
{
InitializeUsingEcdsaSecurityKey(ecdsaKey);
}
else
throw LogHelper.LogExceptionMessage(new NotSupportedException(LogHelper.FormatInvariant(LogMessages.IDX10684, LogHelper.MarkAsNonPII(algorithm), key)));
}
internal byte[] Decrypt(byte[] data)
{
return _decryptFunction(data);
}
internal static byte[] DecryptFunctionNotFound(byte[] _)
{
// we should never get here, its a bug if we do.
throw LogHelper.LogExceptionMessage(new NotSupportedException(LogMessages.IDX10711));
}
/// <summary>
/// Calls <see cref="Dispose(bool)"/> and <see cref="GC.SuppressFinalize"/>.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
_disposed = true;
if (disposing)
{
if (_disposeCryptoOperators)
{
if (ECDsa != null)
ECDsa.Dispose();
#if DESKTOP
if (RsaCryptoServiceProviderProxy != null)
RsaCryptoServiceProviderProxy.Dispose();
#endif
if (RSA != null)
RSA.Dispose();
}
}
}
}
private ECDsa ECDsa { get; set; }
internal byte[] Encrypt(byte[] data)
{
return _encryptFunction(data);
}
internal static byte[] EncryptFunctionNotFound(byte[] _)
{
// we should never get here, its a bug if we do.
throw LogHelper.LogExceptionMessage(new NotSupportedException(LogMessages.IDX10712));
}
private HashAlgorithm HashAlgorithm { get; set; }
private void InitializeUsingEcdsaSecurityKey(ECDsaSecurityKey ecdsaSecurityKey)
{
ECDsa = ecdsaSecurityKey.ECDsa;
_signFunction = SignECDsa;
_signUsingOffsetFunction = SignUsingOffsetECDsa;
#if NET6_0_OR_GREATER
_signUsingSpanFunction = SignUsingSpanECDsa;
#endif
_verifyFunction = VerifyECDsa;
_verifyUsingOffsetFunction = VerifyUsingOffsetECDsa;
}
private void InitializeUsingRsa(RSA rsa, string algorithm)
{
// The return value for X509Certificate2.GetPrivateKey OR X509Certificate2.GetPublicKey.Key is a RSACryptoServiceProvider
// These calls return an AsymmetricAlgorithm which doesn't have API's to do much and need to be cast.
// RSACryptoServiceProvider is wrapped with RSACryptoServiceProviderProxy as some CryptoServiceProviders (CSP's) do
// not natively support SHA2.
#if DESKTOP
if (rsa is RSACryptoServiceProvider rsaCryptoServiceProvider)
{
_useRSAOeapPadding = algorithm.Equals(SecurityAlgorithms.RsaOAEP)
|| algorithm.Equals(SecurityAlgorithms.RsaOaepKeyWrap);
RsaCryptoServiceProviderProxy = new RSACryptoServiceProviderProxy(rsaCryptoServiceProvider);
_decryptFunction = DecryptWithRsaCryptoServiceProviderProxy;
_encryptFunction = EncryptWithRsaCryptoServiceProviderProxy;
_signFunction = SignWithRsaCryptoServiceProviderProxy;
_signUsingOffsetFunction = SignWithRsaCryptoServiceProviderProxyUsingOffset;
_verifyFunction = VerifyWithRsaCryptoServiceProviderProxy;
_verifyUsingOffsetFunction = VerifyWithRsaCryptoServiceProviderProxyUsingOffset;
// RSACryptoServiceProviderProxy will track if a new RSA object is created and dispose appropriately.
_disposeCryptoOperators = true;
return;
}
#endif
if (algorithm.Equals(SecurityAlgorithms.RsaSsaPssSha256) ||
algorithm.Equals(SecurityAlgorithms.RsaSsaPssSha256Signature) ||
algorithm.Equals(SecurityAlgorithms.RsaSsaPssSha384) ||
algorithm.Equals(SecurityAlgorithms.RsaSsaPssSha384Signature) ||
algorithm.Equals(SecurityAlgorithms.RsaSsaPssSha512) ||
algorithm.Equals(SecurityAlgorithms.RsaSsaPssSha512Signature))
{
RSASignaturePadding = RSASignaturePadding.Pss;
}
else
{
// default RSASignaturePadding for other supported RSA algorithms is Pkcs1
RSASignaturePadding = RSASignaturePadding.Pkcs1;
}
RSAEncryptionPadding = (algorithm.Equals(SecurityAlgorithms.RsaOAEP) || algorithm.Equals(SecurityAlgorithms.RsaOaepKeyWrap))
? RSAEncryptionPadding.OaepSHA1
: RSAEncryptionPadding.Pkcs1;
RSA = rsa;
_decryptFunction = DecryptWithRsa;
_encryptFunction = EncryptWithRsa;
_signFunction = SignRsa;
_signUsingOffsetFunction = SignUsingOffsetRsa;
#if NET6_0_OR_GREATER
_signUsingSpanFunction = SignUsingSpanRsa;
#endif
_verifyFunction = VerifyRsa;
_verifyUsingOffsetFunction = VerifyUsingOffsetRsa;
}
private void InitializeUsingRsaSecurityKey(RsaSecurityKey rsaSecurityKey, string algorithm)
{
if (rsaSecurityKey.Rsa != null)
{
InitializeUsingRsa(rsaSecurityKey.Rsa, algorithm);
}
else
{
#if NET472 || NET6_0_OR_GREATER
var rsa = RSA.Create(rsaSecurityKey.Parameters);
#else
var rsa = RSA.Create();
rsa.ImportParameters(rsaSecurityKey.Parameters);
#endif
InitializeUsingRsa(rsa, algorithm);
_disposeCryptoOperators = true;
}
}
private void InitializeUsingX509SecurityKey(X509SecurityKey x509SecurityKey, string algorithm, bool requirePrivateKey)
{
if (requirePrivateKey)
InitializeUsingRsa(x509SecurityKey.PrivateKey as RSA, algorithm);
else
InitializeUsingRsa(x509SecurityKey.PublicKey as RSA, algorithm);
}
private RSA RSA { get; set; }
internal byte[] Sign(byte[] bytes)
{
return _signFunction(bytes);
}
#if NET6_0_OR_GREATER
internal bool SignUsingSpan(ReadOnlySpan<byte> data, Span<byte> destination, out int bytesWritten)
{
return _signUsingSpanFunction(data, destination, out bytesWritten);
}
#endif
internal byte[] SignUsingOffset(byte[] bytes, int offset, int count)
{
return _signUsingOffsetFunction(bytes, offset, count);
}
private static byte[] SignFunctionNotFound(byte[] _)
{
// we should never get here, its a bug if we do.
throw LogHelper.LogExceptionMessage(new CryptographicException(LogMessages.IDX10685));
}
private static byte[] SignUsingOffsetNotFound(byte[] b, int c, int d)
{
// we should never get here, its a bug if we do.
throw LogHelper.LogExceptionMessage(new CryptographicException(LogMessages.IDX10685));
}
#if NET6_0_OR_GREATER
#pragma warning disable CA1801 // Review unused parameters
private static bool SignUsingSpanNotFound(ReadOnlySpan<byte> data, Span<byte> destination, out int bytesWritten)
#pragma warning restore CA1801 // Review unused parameters
{
// we should never get here, its a bug if we do.
throw LogHelper.LogExceptionMessage(new CryptographicException(LogMessages.IDX10685));
}
#endif
private byte[] SignECDsa(byte[] bytes)
{
return ECDsa.SignHash(HashAlgorithm.ComputeHash(bytes));
}
#if NET6_0_OR_GREATER
internal bool SignUsingSpanECDsa(ReadOnlySpan<byte> data, Span<byte> destination, out int bytesWritten)
{
// ECDSA.TrySignData will return true and set bytesWritten = 64, if destination is null.
if (destination.Length == 0)
{
bytesWritten = 0;
return false;
}
bool success = ECDsa.TrySignData(data, destination, HashAlgorithmName, out bytesWritten);
if (!success || bytesWritten == 0)
return false;
return destination.Length >= bytesWritten;
}
#endif
private byte[] SignUsingOffsetECDsa(byte[] bytes, int offset, int count)
{
return ECDsa.SignHash(HashAlgorithm.ComputeHash(bytes, offset, count));
}
internal bool Verify(byte[] bytes, byte[] signature)
{
return _verifyFunction(bytes, signature);
}
internal bool VerifyUsingOffset(byte[] bytes, int offset, int count, byte[] signature)
{
return _verifyUsingOffsetFunction(bytes, offset, count, signature);
}
private static bool VerifyNotFound(byte[] bytes, byte[] signature)
{
// we should never get here, its a bug if we do.
throw LogHelper.LogExceptionMessage(new NotSupportedException(LogMessages.IDX10686));
}
private static bool VerifyUsingOffsetNotFound(byte[] bytes, int offset, int count, byte[] signature)
{
// we should never get here, its a bug if we do.
throw LogHelper.LogExceptionMessage(new NotSupportedException(LogMessages.IDX10686));
}
private bool VerifyECDsa(byte[] bytes, byte[] signature)
{
#if NET6_0_OR_GREATER
return VerifyUsingSpan(isRSA: false, bytes, signature);
#else
return ECDsa.VerifyHash(HashAlgorithm.ComputeHash(bytes), signature);
#endif
}
private bool VerifyUsingOffsetECDsa(byte[] bytes, int offset, int count, byte[] signature)
{
#if NET6_0_OR_GREATER
return VerifyUsingSpan(isRSA: false, bytes.AsSpan(offset, count), signature);
#else
return ECDsa.VerifyHash(HashAlgorithm.ComputeHash(bytes, offset, count), signature);
#endif
}
private byte[] DecryptWithRsa(byte[] bytes)
{
return RSA.Decrypt(bytes, RSAEncryptionPadding);
}
private byte[] EncryptWithRsa(byte[] bytes)
{
return RSA.Encrypt(bytes, RSAEncryptionPadding);
}
private HashAlgorithmName HashAlgorithmName { get; set; }
private RSAEncryptionPadding RSAEncryptionPadding { get; set; }
private RSASignaturePadding RSASignaturePadding { get; set; }
private byte[] SignRsa(byte[] bytes)
{
return RSA.SignHash(HashAlgorithm.ComputeHash(bytes), HashAlgorithmName, RSASignaturePadding);
}
#if NET6_0_OR_GREATER
internal bool SignUsingSpanRsa(ReadOnlySpan<byte> data, Span<byte> destination, out int bytesWritten)
{
return RSA.TrySignData(data, destination, HashAlgorithmName, RSASignaturePadding, out bytesWritten);
}
#endif
private byte[] SignUsingOffsetRsa(byte[] bytes, int offset, int count)
{
return RSA.SignData(bytes, offset, count, HashAlgorithmName, RSASignaturePadding);
}
private bool VerifyRsa(byte[] bytes, byte[] signature)
{
#if NET6_0_OR_GREATER
return VerifyUsingSpan(isRSA: true, bytes, signature);
#else
return RSA.VerifyHash(HashAlgorithm.ComputeHash(bytes), signature, HashAlgorithmName, RSASignaturePadding);
#endif
}
private bool VerifyUsingOffsetRsa(byte[] bytes, int offset, int count, byte[] signature)
{
#if NET6_0_OR_GREATER
return VerifyUsingSpan(isRSA: true, bytes.AsSpan(offset, count), signature);
#else
return RSA.VerifyHash(HashAlgorithm.ComputeHash(bytes, offset, count), signature, HashAlgorithmName, RSASignaturePadding);
#endif
}
#if NET6_0_OR_GREATER
private bool VerifyUsingSpan(bool isRSA, ReadOnlySpan<byte> bytes, byte[] signature)
{
int hashByteLength = HashAlgorithm.HashSize / 8;
byte[] array = null;
Span<byte> hash = hashByteLength <= 256 ? stackalloc byte[256] : array = ArrayPool<byte>.Shared.Rent(hashByteLength);
hash = hash.Slice(0, hashByteLength);
try
{
bool hashResult = HashAlgorithm.TryComputeHash(bytes, hash, out int bytesWritten);
Debug.Assert(hashResult && bytesWritten == hashByteLength, "HashAlgorithm.TryComputeHash failed");
return isRSA ?
RSA.VerifyHash(hash, signature, HashAlgorithmName, RSASignaturePadding) :
ECDsa.VerifyHash(hash, signature);
}
finally
{
if (array is not null)
{
ArrayPool<byte>.Shared.Return(array, clearArray: true);
}
}
}
#endif
#region DESKTOP related code
#if DESKTOP
internal byte[] DecryptWithRsaCryptoServiceProviderProxy(byte[] bytes)
{
return RsaCryptoServiceProviderProxy.Decrypt(bytes, _useRSAOeapPadding);
}
internal byte[] EncryptWithRsaCryptoServiceProviderProxy(byte[] bytes)
{
return RsaCryptoServiceProviderProxy.Encrypt(bytes, _useRSAOeapPadding);
}
private RSACryptoServiceProviderProxy RsaCryptoServiceProviderProxy { get; set; }
internal byte[] SignWithRsaCryptoServiceProviderProxy(byte[] bytes)
{
return RsaCryptoServiceProviderProxy.SignData(bytes, HashAlgorithm);
}
internal byte[] SignWithRsaCryptoServiceProviderProxyUsingOffset(byte[] bytes, int offset, int length)
{
return RsaCryptoServiceProviderProxy.SignData(bytes, offset, length, HashAlgorithm);
}
private bool VerifyWithRsaCryptoServiceProviderProxy(byte[] bytes, byte[] signature)
{
return RsaCryptoServiceProviderProxy.VerifyData(bytes, HashAlgorithm, signature);
}
private bool VerifyWithRsaCryptoServiceProviderProxyUsingOffset(byte[] bytes, int offset, int length, byte[] signature)
{
return RsaCryptoServiceProviderProxy.VerifyDataWithLength(bytes, offset, length, HashAlgorithm, HashAlgorithmName, signature);
}
#endif
#endregion
}
}