| | | 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.Security.Cryptography; |
| | | 6 | | using CoreWCF.IdentityModel.Tokens; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | using CryptoAlgorithms = CoreWCF.IdentityModel.CryptoHelper; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Security |
| | | 11 | | { |
| | | 12 | | internal static class CryptoHelper |
| | | 13 | | { |
| | | 14 | | private static byte[] s_emptyBuffer; |
| | 0 | 15 | | private static readonly RandomNumberGenerator s_random = new RNGCryptoServiceProvider(); |
| | | 16 | | |
| | | 17 | | private enum CryptoAlgorithmType |
| | | 18 | | { |
| | | 19 | | Unknown, |
| | | 20 | | Symmetric, |
| | | 21 | | Asymmetric |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | internal static byte[] EmptyBuffer |
| | | 25 | | { |
| | | 26 | | get |
| | | 27 | | { |
| | 0 | 28 | | if (s_emptyBuffer == null) |
| | | 29 | | { |
| | 0 | 30 | | byte[] tmp = Array.Empty<byte>(); |
| | 0 | 31 | | s_emptyBuffer = tmp; |
| | | 32 | | } |
| | 0 | 33 | | return s_emptyBuffer; |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | internal static HashAlgorithm NewSha1HashAlgorithm() |
| | | 38 | | { |
| | 0 | 39 | | return CreateHashAlgorithm(SecurityAlgorithms.Sha1Digest); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | internal static HashAlgorithm NewSha256HashAlgorithm() |
| | | 43 | | { |
| | 0 | 44 | | return CreateHashAlgorithm(SecurityAlgorithms.Sha256Digest); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | internal static HashAlgorithm CreateHashAlgorithm(string digestMethod) |
| | | 48 | | { |
| | 0 | 49 | | object algorithmObject = CryptoAlgorithms.GetAlgorithmFromConfig(digestMethod); |
| | 0 | 50 | | if (algorithmObject != null) |
| | | 51 | | { |
| | 0 | 52 | | if (algorithmObject is HashAlgorithm hashAlgorithm) |
| | | 53 | | { |
| | 0 | 54 | | return hashAlgorithm; |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(SR.Cu |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | switch (digestMethod) |
| | | 61 | | { |
| | | 62 | | case SecurityAlgorithms.Sha1Digest: |
| | 0 | 63 | | return SHA1.Create(); |
| | | 64 | | case SecurityAlgorithms.Sha256Digest: |
| | 0 | 65 | | return SHA256.Create(); |
| | | 66 | | default: |
| | 0 | 67 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(S |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | internal static HashAlgorithm CreateHashForAsymmetricSignature(string signatureMethod) |
| | | 72 | | { |
| | 0 | 73 | | object algorithmObject = CryptoAlgorithms.GetAlgorithmFromConfig(signatureMethod); |
| | 0 | 74 | | if (algorithmObject != null) |
| | | 75 | | { |
| | | 76 | | HashAlgorithm hashAlgorithm; |
| | | 77 | | |
| | 0 | 78 | | if (algorithmObject is SignatureDescription signatureDescription) |
| | | 79 | | { |
| | 0 | 80 | | hashAlgorithm = signatureDescription.CreateDigest(); |
| | 0 | 81 | | if (hashAlgorithm != null) |
| | | 82 | | { |
| | 0 | 83 | | return hashAlgorithm; |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | hashAlgorithm = algorithmObject as HashAlgorithm; |
| | 0 | 88 | | if (hashAlgorithm != null) |
| | | 89 | | { |
| | 0 | 90 | | return hashAlgorithm; |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(SR.Cu |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | switch (signatureMethod) |
| | | 97 | | { |
| | | 98 | | case SecurityAlgorithms.RsaSha1Signature: |
| | | 99 | | case SecurityAlgorithms.DsaSha1Signature: |
| | 0 | 100 | | return SHA1.Create(); |
| | | 101 | | case SecurityAlgorithms.RsaSha256Signature: |
| | 0 | 102 | | return SHA256.Create(); |
| | | 103 | | default: |
| | 0 | 104 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(S |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | internal static byte[] ExtractIVAndDecrypt(SymmetricAlgorithm algorithm, byte[] cipherText, int offset, int coun |
| | | 109 | | { |
| | 0 | 110 | | if (cipherText == null) |
| | | 111 | | { |
| | 0 | 112 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(cipherText)); |
| | | 113 | | } |
| | 0 | 114 | | if (count < 0 || count > cipherText.Length) |
| | | 115 | | { |
| | 0 | 116 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(count), |
| | | 117 | | } |
| | 0 | 118 | | if (offset < 0 || offset > cipherText.Length - count) |
| | | 119 | | { |
| | 0 | 120 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(offset) |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | int ivSize = algorithm.BlockSize / 8; |
| | 0 | 124 | | byte[] iv = new byte[ivSize]; |
| | 0 | 125 | | Buffer.BlockCopy(cipherText, offset, iv, 0, iv.Length); |
| | 0 | 126 | | algorithm.Padding = PaddingMode.ISO10126; |
| | 0 | 127 | | algorithm.Mode = CipherMode.CBC; |
| | | 128 | | try |
| | | 129 | | { |
| | 0 | 130 | | using (ICryptoTransform decrTransform = algorithm.CreateDecryptor(algorithm.Key, iv)) |
| | | 131 | | { |
| | 0 | 132 | | return decrTransform.TransformFinalBlock(cipherText, offset + iv.Length, count - iv.Length); |
| | | 133 | | } |
| | | 134 | | } |
| | 0 | 135 | | catch (CryptographicException ex) |
| | | 136 | | { |
| | 0 | 137 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.Decr |
| | | 138 | | } |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | internal static void FillRandomBytes(byte[] buffer) |
| | | 142 | | { |
| | 0 | 143 | | s_random.GetBytes(buffer); |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | private static CryptoAlgorithmType GetAlgorithmType(string algorithm) |
| | | 147 | | { |
| | | 148 | | object algorithmObject; |
| | | 149 | | try |
| | | 150 | | { |
| | 0 | 151 | | algorithmObject = CryptoAlgorithms.GetAlgorithmFromConfig(algorithm); |
| | 0 | 152 | | } |
| | | 153 | | #pragma warning disable CA1031 // Do not catch general exception types |
| | 0 | 154 | | catch (InvalidOperationException) |
| | | 155 | | { |
| | 0 | 156 | | algorithmObject = null; |
| | | 157 | | // We swallow the exception and continue. |
| | 0 | 158 | | } |
| | | 159 | | #pragma warning restore CA1031 // Do not catch general exception types |
| | 0 | 160 | | if (algorithmObject != null) |
| | | 161 | | { |
| | 0 | 162 | | SymmetricAlgorithm symmetricAlgorithm = algorithmObject as SymmetricAlgorithm; |
| | 0 | 163 | | KeyedHashAlgorithm keyedHashAlgorithm = algorithmObject as KeyedHashAlgorithm; |
| | 0 | 164 | | if (symmetricAlgorithm != null || keyedHashAlgorithm != null) |
| | | 165 | | { |
| | 0 | 166 | | return CryptoAlgorithmType.Symmetric; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | // NOTE: A KeyedHashAlgorithm is symmetric in nature. |
| | | 170 | | |
| | 0 | 171 | | AsymmetricAlgorithm asymmetricAlgorithm = algorithmObject as AsymmetricAlgorithm; |
| | 0 | 172 | | SignatureDescription signatureDescription = algorithmObject as SignatureDescription; |
| | 0 | 173 | | if (asymmetricAlgorithm != null || signatureDescription != null) |
| | | 174 | | { |
| | 0 | 175 | | return CryptoAlgorithmType.Asymmetric; |
| | | 176 | | } |
| | | 177 | | |
| | 0 | 178 | | return CryptoAlgorithmType.Unknown; |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | switch (algorithm) |
| | | 182 | | { |
| | | 183 | | case SecurityAlgorithms.DsaSha1Signature: |
| | | 184 | | case SecurityAlgorithms.RsaSha1Signature: |
| | | 185 | | case SecurityAlgorithms.RsaSha256Signature: |
| | | 186 | | case SecurityAlgorithms.RsaOaepKeyWrap: |
| | | 187 | | case SecurityAlgorithms.RsaV15KeyWrap: |
| | 0 | 188 | | return CryptoAlgorithmType.Asymmetric; |
| | | 189 | | case SecurityAlgorithms.HmacSha1Signature: |
| | | 190 | | case SecurityAlgorithms.HmacSha256Signature: |
| | | 191 | | case SecurityAlgorithms.Aes128Encryption: |
| | | 192 | | case SecurityAlgorithms.Aes192Encryption: |
| | | 193 | | case SecurityAlgorithms.Aes256Encryption: |
| | | 194 | | case SecurityAlgorithms.TripleDesEncryption: |
| | | 195 | | case SecurityAlgorithms.Aes128KeyWrap: |
| | | 196 | | case SecurityAlgorithms.Aes192KeyWrap: |
| | | 197 | | case SecurityAlgorithms.Aes256KeyWrap: |
| | | 198 | | case SecurityAlgorithms.TripleDesKeyWrap: |
| | | 199 | | case SecurityAlgorithms.Psha1KeyDerivation: |
| | | 200 | | case SecurityAlgorithms.Psha1KeyDerivationDec2005: |
| | 0 | 201 | | return CryptoAlgorithmType.Symmetric; |
| | | 202 | | default: |
| | 0 | 203 | | return CryptoAlgorithmType.Unknown; |
| | | 204 | | } |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | internal static byte[] GenerateIVAndEncrypt(SymmetricAlgorithm algorithm, byte[] plainText, int offset, int coun |
| | | 208 | | { |
| | 0 | 209 | | GenerateIVAndEncrypt(algorithm, new ArraySegment<byte>(plainText, offset, count), out byte[] iv, out byte[] |
| | 0 | 210 | | byte[] output = Fx.AllocateByteArray(checked(iv.Length + cipherText.Length)); |
| | 0 | 211 | | Buffer.BlockCopy(iv, 0, output, 0, iv.Length); |
| | 0 | 212 | | Buffer.BlockCopy(cipherText, 0, output, iv.Length, cipherText.Length); |
| | 0 | 213 | | return output; |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | internal static void GenerateIVAndEncrypt(SymmetricAlgorithm algorithm, ArraySegment<byte> plainText, out byte[] |
| | | 217 | | { |
| | 0 | 218 | | int ivSize = algorithm.BlockSize / 8; |
| | 0 | 219 | | iv = new byte[ivSize]; |
| | 0 | 220 | | FillRandomBytes(iv); |
| | 0 | 221 | | algorithm.Padding = PaddingMode.PKCS7; |
| | 0 | 222 | | algorithm.Mode = CipherMode.CBC; |
| | 0 | 223 | | using (ICryptoTransform encrTransform = algorithm.CreateEncryptor(algorithm.Key, iv)) |
| | | 224 | | { |
| | 0 | 225 | | cipherText = encrTransform.TransformFinalBlock(plainText.Array, plainText.Offset, plainText.Count); |
| | 0 | 226 | | } |
| | 0 | 227 | | } |
| | | 228 | | |
| | | 229 | | internal static bool IsEqual(byte[] a, byte[] b) |
| | | 230 | | { |
| | 0 | 231 | | if (a == null || b == null || a.Length != b.Length) |
| | | 232 | | { |
| | 0 | 233 | | return false; |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | for (int i = 0; i < a.Length; i++) |
| | | 237 | | { |
| | 0 | 238 | | if (a[i] != b[i]) |
| | | 239 | | { |
| | 0 | 240 | | return false; |
| | | 241 | | } |
| | | 242 | | } |
| | 0 | 243 | | return true; |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | internal static bool IsSymmetricAlgorithm(string algorithm) |
| | | 247 | | { |
| | 0 | 248 | | return GetAlgorithmType(algorithm) == CryptoAlgorithmType.Symmetric; |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | internal static bool IsSymmetricSupportedAlgorithm(string algorithm, int keySize) |
| | | 252 | | { |
| | 0 | 253 | | bool found = false; |
| | | 254 | | object algorithmObject; |
| | | 255 | | try |
| | | 256 | | { |
| | 0 | 257 | | algorithmObject = CryptoAlgorithms.GetAlgorithmFromConfig(algorithm); |
| | 0 | 258 | | } |
| | | 259 | | #pragma warning disable CA1031 // Do not catch general exception types |
| | 0 | 260 | | catch (InvalidOperationException) |
| | | 261 | | { |
| | 0 | 262 | | algorithmObject = null; |
| | | 263 | | // We swallow the exception and continue. |
| | 0 | 264 | | } |
| | | 265 | | #pragma warning restore CA1031 // Do not catch general exception types |
| | 0 | 266 | | if (algorithmObject != null) |
| | | 267 | | { |
| | 0 | 268 | | SymmetricAlgorithm symmetricAlgorithm = algorithmObject as SymmetricAlgorithm; |
| | 0 | 269 | | KeyedHashAlgorithm keyedHashAlgorithm = algorithmObject as KeyedHashAlgorithm; |
| | 0 | 270 | | if (symmetricAlgorithm != null || keyedHashAlgorithm != null) |
| | | 271 | | { |
| | 0 | 272 | | found = true; |
| | | 273 | | } |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | switch (algorithm) |
| | | 277 | | { |
| | | 278 | | case SecurityAlgorithms.DsaSha1Signature: |
| | | 279 | | case SecurityAlgorithms.RsaSha1Signature: |
| | | 280 | | case SecurityAlgorithms.RsaSha256Signature: |
| | | 281 | | case SecurityAlgorithms.RsaOaepKeyWrap: |
| | | 282 | | case SecurityAlgorithms.RsaV15KeyWrap: |
| | 0 | 283 | | return false; |
| | | 284 | | case SecurityAlgorithms.HmacSha1Signature: |
| | | 285 | | case SecurityAlgorithms.HmacSha256Signature: |
| | | 286 | | case SecurityAlgorithms.Psha1KeyDerivation: |
| | | 287 | | case SecurityAlgorithms.Psha1KeyDerivationDec2005: |
| | 0 | 288 | | return true; |
| | | 289 | | case SecurityAlgorithms.Aes128Encryption: |
| | | 290 | | case SecurityAlgorithms.Aes128KeyWrap: |
| | 0 | 291 | | return keySize == 128; |
| | | 292 | | case SecurityAlgorithms.Aes192Encryption: |
| | | 293 | | case SecurityAlgorithms.Aes192KeyWrap: |
| | 0 | 294 | | return keySize == 192; |
| | | 295 | | case SecurityAlgorithms.Aes256Encryption: |
| | | 296 | | case SecurityAlgorithms.Aes256KeyWrap: |
| | 0 | 297 | | return keySize == 256; |
| | | 298 | | case SecurityAlgorithms.TripleDesEncryption: |
| | | 299 | | case SecurityAlgorithms.TripleDesKeyWrap: |
| | 0 | 300 | | return keySize == 128 || keySize == 192; |
| | | 301 | | default: |
| | 0 | 302 | | if (found) |
| | | 303 | | { |
| | 0 | 304 | | return true; |
| | | 305 | | } |
| | | 306 | | |
| | 0 | 307 | | return false; |
| | | 308 | | } |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | internal static void ValidateBufferBounds(Array buffer, int offset, int count) |
| | | 312 | | { |
| | 0 | 313 | | if (buffer == null) |
| | | 314 | | { |
| | 0 | 315 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(buffer))); |
| | | 316 | | } |
| | 0 | 317 | | if (count < 0 || count > buffer.Length) |
| | | 318 | | { |
| | 0 | 319 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(count), |
| | | 320 | | } |
| | 0 | 321 | | if (offset < 0 || offset > buffer.Length - count) |
| | | 322 | | { |
| | 0 | 323 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(offset) |
| | | 324 | | } |
| | 0 | 325 | | } |
| | | 326 | | |
| | | 327 | | internal static void ValidateSymmetricKeyLength(int keyLength, SecurityAlgorithmSuite algorithmSuite) |
| | | 328 | | { |
| | 0 | 329 | | if (!algorithmSuite.IsSymmetricKeyLengthSupported(keyLength)) |
| | | 330 | | { |
| | 0 | 331 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new ArgumentOutOfRangeException(nameof(algor |
| | 0 | 332 | | SR.Format(SR.UnsupportedKeyLength, keyLength, algorithmSuite.ToString()))); |
| | | 333 | | } |
| | 0 | 334 | | if (keyLength % 8 != 0) |
| | | 335 | | { |
| | 0 | 336 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new ArgumentOutOfRangeException(nameof(algor |
| | 0 | 337 | | SR.Format(SR.KeyLengthMustBeMultipleOfEight, keyLength))); |
| | | 338 | | } |
| | 0 | 339 | | } |
| | | 340 | | } |
| | | 341 | | } |