| | | 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 System.Security.Cryptography.X509Certificates; |
| | | 7 | | using System.Security.Cryptography.Xml; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 10 | | { |
| | | 11 | | public class X509AsymmetricSecurityKey : AsymmetricSecurityKey |
| | | 12 | | { |
| | | 13 | | private readonly X509Certificate2 _certificate; |
| | | 14 | | private AsymmetricAlgorithm _privateKey; |
| | | 15 | | private bool _privateKeyAvailabilityDetermined; |
| | | 16 | | private AsymmetricAlgorithm _publicKey; |
| | | 17 | | private bool _publicKeyAvailabilityDetermined; |
| | | 18 | | |
| | 4 | 19 | | public X509AsymmetricSecurityKey(X509Certificate2 certificate) |
| | | 20 | | { |
| | 4 | 21 | | _certificate = certificate ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certif |
| | 4 | 22 | | } |
| | | 23 | | |
| | | 24 | | public override int KeySize |
| | | 25 | | { |
| | 3 | 26 | | get { return PublicKey.KeySize; } |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | private AsymmetricAlgorithm PrivateKey |
| | | 30 | | { |
| | | 31 | | get |
| | | 32 | | { |
| | 0 | 33 | | if (!_privateKeyAvailabilityDetermined) |
| | | 34 | | { |
| | 0 | 35 | | lock (ThisLock) |
| | | 36 | | { |
| | 0 | 37 | | _privateKey = _certificate.GetRSAPrivateKey(); |
| | 0 | 38 | | if (_privateKey != null) |
| | | 39 | | { |
| | | 40 | | // ProviderType == 1 is PROV_RSA_FULL provider type that only supports SHA1. |
| | | 41 | | // Change it to PROV_RSA_AES=24 that supports SHA2 also. |
| | 0 | 42 | | if (_privateKey is RSACryptoServiceProvider rsaCsp && rsaCsp.CspKeyContainerInfo.ProviderTyp |
| | | 43 | | { |
| | 0 | 44 | | CspParameters csp = new CspParameters |
| | 0 | 45 | | { |
| | 0 | 46 | | ProviderType = 24, |
| | 0 | 47 | | KeyContainerName = rsaCsp.CspKeyContainerInfo.KeyContainerName, |
| | 0 | 48 | | KeyNumber = (int)rsaCsp.CspKeyContainerInfo.KeyNumber |
| | 0 | 49 | | }; |
| | 0 | 50 | | if (rsaCsp.CspKeyContainerInfo.MachineKeyStore) |
| | | 51 | | { |
| | 0 | 52 | | csp.Flags = CspProviderFlags.UseMachineKeyStore; |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | csp.Flags |= CspProviderFlags.UseExistingKey; |
| | 0 | 56 | | _privateKey = new RSACryptoServiceProvider(csp); |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | else |
| | | 60 | | { |
| | 0 | 61 | | _privateKey = _certificate.GetECDsaPrivateKey(); |
| | | 62 | | // We don't support DSA certificates as we need DSACertificateExtensions which is in netstan |
| | | 63 | | // As we target netstandard2.0, we don't have access to DSACertificateExtensions. If there's |
| | | 64 | | // dependencies forward to provide support. |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | if (_certificate.HasPrivateKey && _privateKey == null) |
| | | 68 | | { |
| | 0 | 69 | | DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.PrivateKeyN |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | _privateKeyAvailabilityDetermined = true; |
| | 0 | 73 | | } |
| | | 74 | | } |
| | 0 | 75 | | return _privateKey; |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | private AsymmetricAlgorithm PublicKey |
| | | 80 | | { |
| | | 81 | | get |
| | | 82 | | { |
| | 12 | 83 | | if (!_publicKeyAvailabilityDetermined) |
| | | 84 | | { |
| | 3 | 85 | | lock (ThisLock) |
| | | 86 | | { |
| | 3 | 87 | | if (!_publicKeyAvailabilityDetermined) |
| | | 88 | | { |
| | 3 | 89 | | _publicKey = _certificate.GetRSAPublicKey(); |
| | 3 | 90 | | if (_publicKey == null) |
| | | 91 | | { |
| | | 92 | | // Need DSACertificateExtensions to support DSA certificate which is in netstandard2.1 |
| | | 93 | | // have access to DSACertificateExtensions |
| | 0 | 94 | | DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.PublicK |
| | | 95 | | } |
| | | 96 | | |
| | 3 | 97 | | _publicKeyAvailabilityDetermined = true; |
| | | 98 | | } |
| | 3 | 99 | | } |
| | | 100 | | } |
| | 12 | 101 | | return _publicKey; |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | 7 | 105 | | private object ThisLock { get; } = new object(); |
| | | 106 | | |
| | | 107 | | public override byte[] DecryptKey(string algorithm, byte[] keyData) |
| | | 108 | | { |
| | 0 | 109 | | throw new PlatformNotSupportedException(); |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | public override byte[] EncryptKey(string algorithm, byte[] keyData) |
| | | 113 | | { |
| | 0 | 114 | | throw new PlatformNotSupportedException(); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | public override AsymmetricAlgorithm GetAsymmetricAlgorithm(string algorithm, bool privateKey) |
| | | 118 | | { |
| | 3 | 119 | | if (privateKey) |
| | | 120 | | { |
| | 0 | 121 | | if (PrivateKey == null) |
| | | 122 | | { |
| | 0 | 123 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.NoPrivateKeyA |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 127 | | { |
| | 0 | 128 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, SR.Format(SR.EmptyOrNullArgu |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | switch (algorithm) |
| | | 132 | | { |
| | | 133 | | case SignedXml.XmlDsigDSAUrl: |
| | 0 | 134 | | if ((PrivateKey as DSA) != null) |
| | | 135 | | { |
| | 0 | 136 | | return (PrivateKey as DSA); |
| | | 137 | | } |
| | 0 | 138 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Algorithm |
| | | 139 | | |
| | | 140 | | case SignedXml.XmlDsigRSASHA1Url: |
| | | 141 | | case SecurityAlgorithms.RsaSha256Signature: |
| | | 142 | | case EncryptedXml.XmlEncRSA15Url: |
| | | 143 | | case EncryptedXml.XmlEncRSAOAEPUrl: |
| | 0 | 144 | | if ((PrivateKey as RSA) != null) |
| | | 145 | | { |
| | 0 | 146 | | return (PrivateKey as RSA); |
| | | 147 | | } |
| | 0 | 148 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Algorithm |
| | | 149 | | default: |
| | 0 | 150 | | if (IsSupportedAlgorithm(algorithm)) |
| | | 151 | | { |
| | 0 | 152 | | return PrivateKey; |
| | | 153 | | } |
| | | 154 | | else |
| | | 155 | | { |
| | 0 | 156 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Forma |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | else |
| | | 161 | | { |
| | | 162 | | switch (algorithm) |
| | | 163 | | { |
| | | 164 | | case SignedXml.XmlDsigDSAUrl: |
| | 0 | 165 | | if (PublicKey is DSA dsaPrivateKey) |
| | | 166 | | { |
| | 0 | 167 | | return dsaPrivateKey; |
| | | 168 | | } |
| | 0 | 169 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException("AlgorithmAn |
| | | 170 | | case SignedXml.XmlDsigRSASHA1Url: |
| | | 171 | | case SecurityAlgorithms.RsaSha256Signature: |
| | | 172 | | case EncryptedXml.XmlEncRSA15Url: |
| | | 173 | | case EncryptedXml.XmlEncRSAOAEPUrl: |
| | 3 | 174 | | if ((PublicKey as RSA) != null) |
| | | 175 | | { |
| | 3 | 176 | | return (PublicKey as RSA); |
| | | 177 | | } |
| | 0 | 178 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException("AlgorithmAn |
| | | 179 | | default: |
| | | 180 | | |
| | 0 | 181 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | public override HashAlgorithm GetHashAlgorithmForSignature(string algorithm) |
| | | 187 | | { |
| | 0 | 188 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 189 | | { |
| | 0 | 190 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, SR.Format(SR.EmptyOrNullArgument |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | object algorithmObject = CryptoHelper.GetAlgorithmFromConfig(algorithm); |
| | | 194 | | |
| | 0 | 195 | | if (algorithmObject != null) |
| | | 196 | | { |
| | 0 | 197 | | if (algorithmObject is SignatureDescription description) |
| | | 198 | | { |
| | 0 | 199 | | return description.CreateDigest(); |
| | | 200 | | } |
| | | 201 | | |
| | 0 | 202 | | if (algorithmObject is HashAlgorithm hashAlgorithm) |
| | | 203 | | { |
| | 0 | 204 | | return hashAlgorithm; |
| | | 205 | | } |
| | | 206 | | |
| | 0 | 207 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format(SR.Unsupp |
| | 0 | 208 | | algorithm, "CreateDigest"))); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | switch (algorithm) |
| | | 212 | | { |
| | | 213 | | case SignedXml.XmlDsigDSAUrl: |
| | | 214 | | case SignedXml.XmlDsigRSASHA1Url: |
| | 0 | 215 | | return CryptoHelper.NewSha1HashAlgorithm(); |
| | | 216 | | case SecurityAlgorithms.RsaSha256Signature: |
| | 0 | 217 | | return CryptoHelper.NewSha256HashAlgorithm(); |
| | | 218 | | default: |
| | 0 | 219 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Uns |
| | | 220 | | } |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | public override AsymmetricSignatureDeformatter GetSignatureDeformatter(string algorithm) |
| | | 224 | | { |
| | | 225 | | // We support one of the two algoritms, but not both. |
| | | 226 | | // XmlDsigDSAUrl = "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; |
| | | 227 | | // XmlDsigRSASHA1Url = "http://www.w3.org/2000/09/xmldsig#rsa-sha1"; |
| | | 228 | | |
| | 0 | 229 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 230 | | { |
| | 0 | 231 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, SR.Format(SR.EmptyOrNullArgument |
| | | 232 | | } |
| | | 233 | | |
| | 0 | 234 | | object algorithmObject = CryptoHelper.GetAlgorithmFromConfig(algorithm); |
| | 0 | 235 | | if (algorithmObject != null) |
| | | 236 | | { |
| | 0 | 237 | | if (algorithmObject is SignatureDescription description) |
| | | 238 | | { |
| | 0 | 239 | | return description.CreateDeformatter(PublicKey); |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | try |
| | | 243 | | { |
| | 0 | 244 | | if (algorithmObject is AsymmetricSignatureDeformatter asymmetricSignatureDeformatter) |
| | | 245 | | { |
| | 0 | 246 | | asymmetricSignatureDeformatter.SetKey(PublicKey); |
| | 0 | 247 | | return asymmetricSignatureDeformatter; |
| | | 248 | | } |
| | 0 | 249 | | } |
| | 0 | 250 | | catch (InvalidCastException e) |
| | | 251 | | { |
| | 0 | 252 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.AlgorithmAndP |
| | | 253 | | } |
| | | 254 | | |
| | 0 | 255 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format(SR.Unsupp |
| | 0 | 256 | | algorithm, nameof(GetSignatureDeformatter)))); |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | switch (algorithm) |
| | | 260 | | { |
| | | 261 | | case SignedXml.XmlDsigDSAUrl: |
| | | 262 | | |
| | | 263 | | // Ensure that we have a DSA algorithm object. |
| | 0 | 264 | | DSA dsa = (PublicKey as DSA); |
| | 0 | 265 | | if (dsa == null) |
| | | 266 | | { |
| | 0 | 267 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException("PublicKeyNo |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | return new DSASignatureDeformatter(dsa); |
| | | 271 | | |
| | | 272 | | case SignedXml.XmlDsigRSASHA1Url: |
| | | 273 | | case SecurityAlgorithms.RsaSha256Signature: |
| | | 274 | | // Ensure that we have an RSA algorithm object. |
| | 0 | 275 | | RSA rsa = (PublicKey as RSA); |
| | 0 | 276 | | if (rsa == null) |
| | | 277 | | { |
| | 0 | 278 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.PublicKey |
| | | 279 | | } |
| | | 280 | | |
| | 0 | 281 | | return new RSAPKCS1SignatureDeformatter(rsa); |
| | | 282 | | |
| | | 283 | | default: |
| | 0 | 284 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Uns |
| | | 285 | | } |
| | 0 | 286 | | } |
| | | 287 | | |
| | | 288 | | public override AsymmetricSignatureFormatter GetSignatureFormatter(string algorithm) |
| | | 289 | | { |
| | | 290 | | // One can sign only if the private key is present. |
| | 0 | 291 | | if (PrivateKey == null) |
| | | 292 | | { |
| | 0 | 293 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.NoPrivateKeyAvail |
| | | 294 | | } |
| | | 295 | | |
| | 0 | 296 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 297 | | { |
| | 0 | 298 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, SR.Format(SR.EmptyOrNullArgument |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | // We support: |
| | | 302 | | // XmlDsigDSAUrl = "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; |
| | | 303 | | // XmlDsigRSASHA1Url = "http://www.w3.org/2000/09/xmldsig#rsa-sha1"; |
| | | 304 | | // RsaSha256Signature = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"; |
| | 0 | 305 | | AsymmetricAlgorithm privateKey = PrivateKey; |
| | | 306 | | |
| | 0 | 307 | | object algorithmObject = CryptoHelper.GetAlgorithmFromConfig(algorithm); |
| | 0 | 308 | | if (algorithmObject != null) |
| | | 309 | | { |
| | 0 | 310 | | if (algorithmObject is SignatureDescription description) |
| | | 311 | | { |
| | 0 | 312 | | return description.CreateFormatter(privateKey); |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | try |
| | | 316 | | { |
| | 0 | 317 | | if (algorithmObject is AsymmetricSignatureFormatter asymmetricSignatureFormatter) |
| | | 318 | | { |
| | 0 | 319 | | asymmetricSignatureFormatter.SetKey(privateKey); |
| | 0 | 320 | | return asymmetricSignatureFormatter; |
| | | 321 | | } |
| | 0 | 322 | | } |
| | 0 | 323 | | catch (InvalidCastException e) |
| | | 324 | | { |
| | 0 | 325 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.AlgorithmAndP |
| | | 326 | | } |
| | | 327 | | |
| | 0 | 328 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format(SR.Unsupp |
| | 0 | 329 | | algorithm, nameof(GetSignatureFormatter)))); |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | switch (algorithm) |
| | | 333 | | { |
| | | 334 | | case SignedXml.XmlDsigDSAUrl: |
| | | 335 | | |
| | | 336 | | // Ensure that we have a DSA algorithm object. |
| | 0 | 337 | | DSA dsa = (PrivateKey as DSA); |
| | 0 | 338 | | if (dsa == null) |
| | | 339 | | { |
| | 0 | 340 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.PrivateKe |
| | | 341 | | } |
| | 0 | 342 | | return new DSASignatureFormatter(dsa); |
| | | 343 | | case SignedXml.XmlDsigRSASHA1Url: |
| | | 344 | | // Ensure that we have an RSA algorithm object. |
| | 0 | 345 | | RSA rsa = (PrivateKey as RSA); |
| | 0 | 346 | | if (rsa == null) |
| | | 347 | | { |
| | 0 | 348 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.PrivateKe |
| | | 349 | | } |
| | | 350 | | |
| | 0 | 351 | | return new RSAPKCS1SignatureFormatter(rsa); |
| | | 352 | | |
| | | 353 | | case SecurityAlgorithms.RsaSha256Signature: |
| | | 354 | | // Ensure that we have an RSA algorithm object. |
| | 0 | 355 | | RSA rsaSha256 = (privateKey as RSA); |
| | 0 | 356 | | if (rsaSha256 == null) |
| | | 357 | | { |
| | 0 | 358 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.PrivateKe |
| | | 359 | | } |
| | | 360 | | |
| | 0 | 361 | | return new RSAPKCS1SignatureFormatter(rsaSha256); |
| | | 362 | | |
| | | 363 | | default: |
| | 0 | 364 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Uns |
| | | 365 | | } |
| | 0 | 366 | | } |
| | | 367 | | |
| | | 368 | | public override bool HasPrivateKey() |
| | | 369 | | { |
| | 0 | 370 | | return (PrivateKey != null); |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | public override bool IsSupportedAlgorithm(string algorithm) |
| | | 374 | | { |
| | 6 | 375 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 376 | | { |
| | 0 | 377 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, SR.Format(SR.EmptyOrNullArgument |
| | | 378 | | } |
| | | 379 | | |
| | 6 | 380 | | object algorithmObject = null; |
| | | 381 | | try |
| | | 382 | | { |
| | 6 | 383 | | algorithmObject = CryptoHelper.GetAlgorithmFromConfig(algorithm); |
| | 6 | 384 | | } |
| | 0 | 385 | | catch (InvalidOperationException) |
| | | 386 | | { |
| | 0 | 387 | | algorithm = null; |
| | 0 | 388 | | } |
| | | 389 | | |
| | 6 | 390 | | if (algorithmObject != null) |
| | | 391 | | { |
| | 3 | 392 | | if (algorithmObject is SignatureDescription signatureDescription) |
| | | 393 | | { |
| | 0 | 394 | | return true; |
| | | 395 | | } |
| | | 396 | | |
| | 3 | 397 | | if (algorithmObject is AsymmetricAlgorithm asymmetricAlgorithm) |
| | | 398 | | { |
| | 0 | 399 | | return true; |
| | | 400 | | } |
| | | 401 | | |
| | 3 | 402 | | return false; |
| | | 403 | | } |
| | | 404 | | |
| | | 405 | | switch (algorithm) |
| | | 406 | | { |
| | | 407 | | case SignedXml.XmlDsigDSAUrl: |
| | 0 | 408 | | return (PublicKey is DSA); |
| | | 409 | | |
| | | 410 | | case SignedXml.XmlDsigRSASHA1Url: |
| | | 411 | | case SecurityAlgorithms.RsaSha256Signature: |
| | | 412 | | case EncryptedXml.XmlEncRSA15Url: |
| | | 413 | | case EncryptedXml.XmlEncRSAOAEPUrl: |
| | 3 | 414 | | return (PublicKey is RSA); |
| | | 415 | | default: |
| | 0 | 416 | | return false; |
| | | 417 | | } |
| | | 418 | | } |
| | | 419 | | } |
| | | 420 | | } |