| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Reflection; |
| | | 7 | | using System.Security.Cryptography; |
| | | 8 | | using CoreWCF.IdentityModel.Tokens; |
| | | 9 | | using Linq = System.Linq; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.IdentityModel |
| | | 12 | | { |
| | | 13 | | internal static class CryptoHelper |
| | | 14 | | { |
| | | 15 | | private static RandomNumberGenerator s_random; |
| | | 16 | | private const string SHAString = "SHA"; |
| | | 17 | | private const string SHA1String = "SHA1"; |
| | | 18 | | private const string SHA256String = "SHA256"; |
| | | 19 | | private const string SystemSecurityCryptographySha1String = "System.Security.Cryptography.SHA1"; |
| | | 20 | | private static byte[] s_emptyBuffer; |
| | | 21 | | |
| | 2 | 22 | | private static readonly Dictionary<string, Func<object>> s_algorithmDelegateDictionary = new Dictionary<string, |
| | 2 | 23 | | private static readonly object s_algorithmDictionaryLock = new object(); |
| | | 24 | | |
| | | 25 | | internal static byte[] EmptyBuffer |
| | | 26 | | { |
| | | 27 | | get |
| | | 28 | | { |
| | 23 | 29 | | if (s_emptyBuffer == null) |
| | | 30 | | { |
| | 2 | 31 | | byte[] tmp = Array.Empty<byte>(); |
| | 2 | 32 | | s_emptyBuffer = tmp; |
| | | 33 | | } |
| | 23 | 34 | | return s_emptyBuffer; |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public static byte[] GenerateSymmetricKey(int keySizeInBits) |
| | | 39 | | { |
| | 0 | 40 | | int keySizeInBytes = ValidateKeySizeInBytes(keySizeInBits); |
| | 0 | 41 | | byte[] key = new byte[keySizeInBytes]; |
| | 0 | 42 | | CryptoHelper.GenerateRandomBytes(key); |
| | 0 | 43 | | return key; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | private static int ValidateKeySizeInBytes(int keySizeInBits) |
| | | 47 | | { |
| | 0 | 48 | | int keySizeInBytes = keySizeInBits / 8; |
| | | 49 | | |
| | 0 | 50 | | if (keySizeInBits <= 0) |
| | | 51 | | { |
| | 0 | 52 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(keySize |
| | | 53 | | } |
| | 0 | 54 | | else if (keySizeInBytes * 8 != keySizeInBits) |
| | | 55 | | { |
| | 0 | 56 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.ID6002, key |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | return keySizeInBytes; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | internal static void ValidateBufferBounds(Array buffer, int offset, int count) |
| | | 63 | | { |
| | 0 | 64 | | if (buffer == null) |
| | | 65 | | { |
| | 0 | 66 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(buffer))); |
| | | 67 | | } |
| | 0 | 68 | | if (count < 0 || count > buffer.Length) |
| | | 69 | | { |
| | 0 | 70 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(count), |
| | | 71 | | } |
| | 0 | 72 | | if (offset < 0 || offset > buffer.Length - count) |
| | | 73 | | { |
| | 0 | 74 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(offset) |
| | | 75 | | } |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | public static bool FixedTimeEquals(byte[] a, byte[] b) |
| | | 79 | | { |
| | 0 | 80 | | if (a == null && b == null) |
| | | 81 | | { |
| | 0 | 82 | | return true; |
| | | 83 | | } |
| | 0 | 84 | | else if (a == null || b == null) |
| | | 85 | | { |
| | 0 | 86 | | return false; |
| | | 87 | | } |
| | 0 | 88 | | else if (a.Length != b.Length) |
| | | 89 | | { |
| | 0 | 90 | | return false; |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | int result = 0; |
| | 0 | 94 | | int length = a.Length; |
| | | 95 | | |
| | 0 | 96 | | for (int i = 0; i < length; i++) |
| | | 97 | | { |
| | 0 | 98 | | result |= a[i] ^ b[i]; |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | return result == 0; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | internal static byte[] UnwrapKey(byte[] wrappingKey, byte[] wrappedKey, string algorithm) |
| | | 105 | | { |
| | 0 | 106 | | throw new PlatformNotSupportedException(); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | internal static byte[] WrapKey(byte[] wrappingKey, byte[] keyToBeWrapped, string algorithm) |
| | | 110 | | { |
| | 0 | 111 | | throw new PlatformNotSupportedException(); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | internal static byte[] GenerateDerivedKey(byte[] key, string algorithm, byte[] label, byte[] nonce, int derivedK |
| | | 115 | | { |
| | 0 | 116 | | throw new PlatformNotSupportedException(); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | internal static int GetIVSize(string algorithm) |
| | | 120 | | { |
| | 0 | 121 | | throw new PlatformNotSupportedException(); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | internal static ICryptoTransform CreateDecryptor(byte[] key, byte[] iv, string algorithm) |
| | | 125 | | { |
| | 0 | 126 | | throw new PlatformNotSupportedException(); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | internal static ICryptoTransform CreateEncryptor(byte[] key, byte[] iv, string algorithm) |
| | | 130 | | { |
| | 0 | 131 | | throw new PlatformNotSupportedException(); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | internal static KeyedHashAlgorithm CreateKeyedHashAlgorithm(byte[] key, string algorithm) |
| | | 135 | | { |
| | 30 | 136 | | object algorithmObject = GetAlgorithmFromConfig(algorithm); |
| | | 137 | | |
| | 30 | 138 | | if (algorithmObject != null) |
| | | 139 | | { |
| | 30 | 140 | | if (algorithmObject is KeyedHashAlgorithm keyedHashAlgorithm) |
| | | 141 | | { |
| | 30 | 142 | | keyedHashAlgorithm.Key = key; |
| | 30 | 143 | | return keyedHashAlgorithm; |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format("Cus |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | switch (algorithm) |
| | | 150 | | { |
| | | 151 | | case SecurityAlgorithms.HmacSha1Signature: |
| | 0 | 152 | | return new HMACSHA1(key); |
| | | 153 | | case SecurityAlgorithms.HmacSha256Signature: |
| | 0 | 154 | | return new HMACSHA256(key); |
| | | 155 | | default: |
| | 0 | 156 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format( |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | internal static SymmetricAlgorithm GetSymmetricAlgorithm(byte[] key, string algorithm) |
| | | 161 | | { |
| | 0 | 162 | | throw new PlatformNotSupportedException(); |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | internal static bool IsSymmetricSupportedAlgorithm(string algorithm, int keySize) |
| | | 166 | | { |
| | 20 | 167 | | bool found = false; |
| | 20 | 168 | | object algorithmObject = null; |
| | | 169 | | |
| | | 170 | | try |
| | | 171 | | { |
| | 20 | 172 | | algorithmObject = GetAlgorithmFromConfig(algorithm); |
| | 20 | 173 | | } |
| | 0 | 174 | | catch (InvalidOperationException) |
| | | 175 | | { |
| | | 176 | | // We swallow the exception and continue. |
| | 0 | 177 | | } |
| | 20 | 178 | | if (algorithmObject != null) |
| | | 179 | | { |
| | 20 | 180 | | SymmetricAlgorithm symmetricAlgorithm = algorithmObject as SymmetricAlgorithm; |
| | 20 | 181 | | KeyedHashAlgorithm keyedHashAlgorithm = algorithmObject as KeyedHashAlgorithm; |
| | | 182 | | |
| | 20 | 183 | | if (symmetricAlgorithm != null || keyedHashAlgorithm != null) |
| | | 184 | | { |
| | 20 | 185 | | found = true; |
| | | 186 | | } |
| | | 187 | | // The reason we do not return here even when the user has provided a custom algorithm to CryptoConfig |
| | | 188 | | // is because we need to check if the user has overwritten an existing standard URI. |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | switch (algorithm) |
| | | 192 | | { |
| | | 193 | | case SecurityAlgorithms.DsaSha1Signature: |
| | | 194 | | case SecurityAlgorithms.RsaSha1Signature: |
| | | 195 | | case SecurityAlgorithms.RsaSha256Signature: |
| | | 196 | | case SecurityAlgorithms.RsaOaepKeyWrap: |
| | | 197 | | case SecurityAlgorithms.RsaV15KeyWrap: |
| | 0 | 198 | | return false; |
| | | 199 | | case SecurityAlgorithms.HmacSha1Signature: |
| | | 200 | | case SecurityAlgorithms.HmacSha256Signature: |
| | | 201 | | case SecurityAlgorithms.Psha1KeyDerivation: |
| | | 202 | | case SecurityAlgorithms.Psha1KeyDerivationDec2005: |
| | 20 | 203 | | return true; |
| | | 204 | | case SecurityAlgorithms.Aes128Encryption: |
| | | 205 | | case SecurityAlgorithms.Aes128KeyWrap: |
| | 0 | 206 | | return keySize >= 128 && keySize <= 256; |
| | | 207 | | case SecurityAlgorithms.Aes192Encryption: |
| | | 208 | | case SecurityAlgorithms.Aes192KeyWrap: |
| | 0 | 209 | | return keySize >= 192 && keySize <= 256; |
| | | 210 | | case SecurityAlgorithms.Aes256Encryption: |
| | | 211 | | case SecurityAlgorithms.Aes256KeyWrap: |
| | 0 | 212 | | return keySize == 256; |
| | | 213 | | case SecurityAlgorithms.TripleDesEncryption: |
| | | 214 | | case SecurityAlgorithms.TripleDesKeyWrap: |
| | 0 | 215 | | return keySize == 128 || keySize == 192; |
| | | 216 | | default: |
| | 0 | 217 | | if (found) |
| | | 218 | | { |
| | 0 | 219 | | return true; |
| | | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | return false; |
| | | 223 | | // We do not expect the user to map the uri of an existing standrad algorithm with say key size 128 |
| | | 224 | | // to a custom algorithm with keySize 192 bits. If he does that, we anyways make sure that we return |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | internal static void FillRandomBytes(byte[] buffer) |
| | | 229 | | { |
| | 0 | 230 | | RandomNumberGenerator.GetBytes(buffer); |
| | 0 | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// This generates the entropy using random number. This is usually used on the sending |
| | | 235 | | /// side to generate the requestor's entropy. |
| | | 236 | | /// </summary> |
| | | 237 | | /// <param name="data">The array to fill with cryptographically strong random nonzero bytes.</param> |
| | | 238 | | public static void GenerateRandomBytes(byte[] data) |
| | | 239 | | { |
| | 0 | 240 | | RandomNumberGenerator.GetNonZeroBytes(data); |
| | 0 | 241 | | } |
| | | 242 | | |
| | | 243 | | /// <summary> |
| | | 244 | | /// This method generates a random byte array used as entropy with the given size. |
| | | 245 | | /// </summary> |
| | | 246 | | /// <param name="sizeInBits"></param> |
| | | 247 | | /// <returns></returns> |
| | | 248 | | public static byte[] GenerateRandomBytes(int sizeInBits) |
| | | 249 | | { |
| | 0 | 250 | | int sizeInBytes = sizeInBits / 8; |
| | 0 | 251 | | if (sizeInBits <= 0) |
| | | 252 | | { |
| | 0 | 253 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(sizeInB |
| | | 254 | | } |
| | 0 | 255 | | else if (sizeInBytes * 8 != sizeInBits) |
| | | 256 | | { |
| | 0 | 257 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format("ID6002", size |
| | | 258 | | } |
| | | 259 | | |
| | 0 | 260 | | byte[] data = new byte[sizeInBytes]; |
| | 0 | 261 | | GenerateRandomBytes(data); |
| | | 262 | | |
| | 0 | 263 | | return data; |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | internal static RandomNumberGenerator RandomNumberGenerator |
| | | 267 | | { |
| | | 268 | | get |
| | | 269 | | { |
| | 0 | 270 | | if (s_random == null) |
| | | 271 | | { |
| | 0 | 272 | | s_random = RandomNumberGenerator.Create(); |
| | | 273 | | } |
| | 0 | 274 | | return s_random; |
| | | 275 | | } |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | internal static HashAlgorithm CreateHashAlgorithm(string algorithm) |
| | | 279 | | { |
| | 23 | 280 | | object algorithmObject = GetAlgorithmFromConfig(algorithm); |
| | | 281 | | |
| | 23 | 282 | | if (algorithmObject != null) |
| | | 283 | | { |
| | 23 | 284 | | if (algorithmObject is HashAlgorithm hashAlgorithm) |
| | | 285 | | { |
| | 23 | 286 | | return hashAlgorithm; |
| | | 287 | | } |
| | 0 | 288 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.C |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | switch (algorithm) |
| | | 292 | | { |
| | | 293 | | case SHAString: |
| | | 294 | | case SHA1String: |
| | | 295 | | case SystemSecurityCryptographySha1String: |
| | | 296 | | case SecurityAlgorithms.Sha1Digest: |
| | 0 | 297 | | return SHA1.Create(); |
| | | 298 | | case SHA256String: |
| | | 299 | | case SecurityAlgorithms.Sha256Digest: |
| | 0 | 300 | | return SHA256.Create(); |
| | | 301 | | default: |
| | 0 | 302 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format( |
| | | 303 | | } |
| | | 304 | | } |
| | | 305 | | |
| | | 306 | | private static object GetDefaultAlgorithm(string algorithm) |
| | | 307 | | { |
| | 4 | 308 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 309 | | { |
| | 0 | 310 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(algorithm))); |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | switch (algorithm) |
| | | 314 | | { |
| | | 315 | | //case SecurityAlgorithms.RsaSha1Signature: |
| | | 316 | | //case SecurityAlgorithms.DsaSha1Signature: |
| | | 317 | | // For these algorithms above, crypto config returns internal objects. |
| | | 318 | | // As we cannot create those internal objects, we are returning null. |
| | | 319 | | // If no custom algorithm is plugged-in, at least these two algorithms |
| | | 320 | | // will be inside the delegate dictionary. |
| | | 321 | | case SecurityAlgorithms.Sha1Digest: |
| | 2 | 322 | | return SHA1.Create(); |
| | | 323 | | case SecurityAlgorithms.ExclusiveC14n: |
| | 0 | 324 | | throw new PlatformNotSupportedException(); |
| | | 325 | | case SHA256String: |
| | | 326 | | case SecurityAlgorithms.Sha256Digest: |
| | 0 | 327 | | return SHA256.Create(); |
| | | 328 | | case SecurityAlgorithms.Sha512Digest: |
| | 0 | 329 | | return SHA512.Create(); |
| | | 330 | | case SecurityAlgorithms.Aes128Encryption: |
| | | 331 | | case SecurityAlgorithms.Aes192Encryption: |
| | | 332 | | case SecurityAlgorithms.Aes256Encryption: |
| | | 333 | | case SecurityAlgorithms.Aes128KeyWrap: |
| | | 334 | | case SecurityAlgorithms.Aes192KeyWrap: |
| | | 335 | | case SecurityAlgorithms.Aes256KeyWrap: |
| | 0 | 336 | | return Aes.Create(); |
| | | 337 | | case SecurityAlgorithms.TripleDesEncryption: |
| | | 338 | | case SecurityAlgorithms.TripleDesKeyWrap: |
| | 0 | 339 | | return TripleDES.Create(); |
| | | 340 | | case SecurityAlgorithms.HmacSha1Signature: |
| | 2 | 341 | | return new HMACSHA1(); |
| | | 342 | | case SecurityAlgorithms.HmacSha256Signature: |
| | 0 | 343 | | return new HMACSHA256(); |
| | | 344 | | case SecurityAlgorithms.ExclusiveC14nWithComments: |
| | 0 | 345 | | throw new PlatformNotSupportedException(); |
| | | 346 | | case SecurityAlgorithms.Ripemd160Digest: |
| | 0 | 347 | | return null; |
| | | 348 | | case SecurityAlgorithms.DesEncryption: |
| | 0 | 349 | | return DES.Create(); |
| | | 350 | | default: |
| | 0 | 351 | | return null; |
| | | 352 | | } |
| | | 353 | | } |
| | | 354 | | |
| | | 355 | | internal static object GetAlgorithmFromConfig(string algorithm) |
| | | 356 | | { |
| | 79 | 357 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 358 | | { |
| | 0 | 359 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(algorithm))); |
| | | 360 | | } |
| | | 361 | | |
| | 79 | 362 | | object algorithmObject = null; |
| | 79 | 363 | | object defaultObject = null; |
| | | 364 | | |
| | 79 | 365 | | if (!s_algorithmDelegateDictionary.TryGetValue(algorithm, out Func<object> delegateFunction)) |
| | | 366 | | { |
| | 5 | 367 | | lock (s_algorithmDictionaryLock) |
| | | 368 | | { |
| | 5 | 369 | | if (!s_algorithmDelegateDictionary.ContainsKey(algorithm)) |
| | | 370 | | { |
| | | 371 | | try |
| | | 372 | | { |
| | 5 | 373 | | algorithmObject = CryptoConfig.CreateFromName(algorithm); |
| | 5 | 374 | | } |
| | 0 | 375 | | catch (TargetInvocationException) |
| | | 376 | | { |
| | 0 | 377 | | s_algorithmDelegateDictionary[algorithm] = null; |
| | 0 | 378 | | } |
| | | 379 | | |
| | 5 | 380 | | if (algorithmObject == null) |
| | | 381 | | { |
| | 1 | 382 | | s_algorithmDelegateDictionary[algorithm] = null; |
| | | 383 | | } |
| | | 384 | | else |
| | | 385 | | { |
| | 4 | 386 | | defaultObject = GetDefaultAlgorithm(algorithm); |
| | 4 | 387 | | if (defaultObject != null && defaultObject.GetType() == algorithmObject.GetType()) |
| | | 388 | | { |
| | 2 | 389 | | s_algorithmDelegateDictionary[algorithm] = null; |
| | | 390 | | } |
| | | 391 | | else |
| | | 392 | | { |
| | | 393 | | // Create a factory delegate which returns new instances of the algorithm type for later |
| | 2 | 394 | | Type algorithmType = algorithmObject.GetType(); |
| | 2 | 395 | | System.Linq.Expressions.NewExpression algorithmCreationExpression = Linq.Expressions.Exp |
| | 2 | 396 | | Linq.Expressions.LambdaExpression creationFunction = Linq.Expressions.Expression.Lambda< |
| | 2 | 397 | | delegateFunction = creationFunction.Compile() as Func<object>; |
| | | 398 | | |
| | 2 | 399 | | if (delegateFunction != null) |
| | | 400 | | { |
| | 2 | 401 | | s_algorithmDelegateDictionary[algorithm] = delegateFunction; |
| | | 402 | | } |
| | 2 | 403 | | return algorithmObject; |
| | | 404 | | } |
| | | 405 | | } |
| | | 406 | | } |
| | 0 | 407 | | } |
| | | 408 | | } |
| | | 409 | | else |
| | | 410 | | { |
| | 74 | 411 | | if (delegateFunction != null) |
| | | 412 | | { |
| | 21 | 413 | | return delegateFunction.Invoke(); |
| | | 414 | | } |
| | | 415 | | } |
| | | 416 | | |
| | | 417 | | // |
| | | 418 | | // This is a fallback in case CryptoConfig fails to return a valid |
| | | 419 | | // algorithm object. CrytoConfig does not understand all the uri's and |
| | | 420 | | // can return a null in that case, in which case it is our responsibility |
| | | 421 | | // to fallback and create the right algorithm if it is a uri we understand |
| | | 422 | | // |
| | | 423 | | switch (algorithm) |
| | | 424 | | { |
| | | 425 | | case SHA256String: |
| | | 426 | | case SecurityAlgorithms.Sha256Digest: |
| | 0 | 427 | | return SHA256.Create(); |
| | | 428 | | case SecurityAlgorithms.Sha1Digest: |
| | 0 | 429 | | return SHA1.Create(); |
| | | 430 | | case SecurityAlgorithms.HmacSha1Signature: |
| | 53 | 431 | | return new HMACSHA1(); |
| | | 432 | | default: |
| | | 433 | | break; |
| | | 434 | | } |
| | | 435 | | |
| | 3 | 436 | | return null; |
| | 2 | 437 | | } |
| | | 438 | | |
| | | 439 | | internal static HashAlgorithm NewSha1HashAlgorithm() |
| | | 440 | | { |
| | 0 | 441 | | return CreateHashAlgorithm(SecurityAlgorithms.Sha1Digest); |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | internal static HashAlgorithm NewSha256HashAlgorithm() |
| | | 445 | | { |
| | 0 | 446 | | return CreateHashAlgorithm(SecurityAlgorithms.Sha256Digest); |
| | | 447 | | } |
| | | 448 | | |
| | | 449 | | internal static KeyedHashAlgorithm NewHmacSha1KeyedHashAlgorithm(byte[] key) |
| | | 450 | | { |
| | 10 | 451 | | return CreateKeyedHashAlgorithm(key, SecurityAlgorithms.HmacSha1Signature); |
| | | 452 | | } |
| | | 453 | | } |
| | | 454 | | } |
| | | 455 | | |