| | | 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.Xml; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 9 | | { |
| | | 10 | | public sealed class RsaSecurityKey : AsymmetricSecurityKey |
| | | 11 | | { |
| | | 12 | | private PrivateKeyStatus _privateKeyStatus = PrivateKeyStatus.AvailabilityNotDetermined; |
| | | 13 | | private readonly RSA _rsa; |
| | | 14 | | |
| | 0 | 15 | | public RsaSecurityKey(RSA rsa) |
| | | 16 | | { |
| | 0 | 17 | | _rsa = rsa ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rsa)); |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | public override int KeySize |
| | | 21 | | { |
| | 0 | 22 | | get { return _rsa.KeySize; } |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | public override byte[] DecryptKey(string algorithm, byte[] keyData) |
| | | 26 | | { |
| | | 27 | | switch (algorithm) |
| | | 28 | | { |
| | | 29 | | case SecurityAlgorithms.RsaV15KeyWrap: |
| | 0 | 30 | | return EncryptedXml.DecryptKey(keyData, _rsa, false); |
| | | 31 | | case SecurityAlgorithms.RsaOaepKeyWrap: |
| | 0 | 32 | | return EncryptedXml.DecryptKey(keyData, _rsa, true); |
| | | 33 | | default: |
| | 0 | 34 | | if (IsSupportedAlgorithm(algorithm)) |
| | | 35 | | { |
| | 0 | 36 | | return EncryptedXml.DecryptKey(keyData, _rsa, false); |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format("c", |
| | 0 | 40 | | algorithm, "DecryptKey"))); |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public override byte[] EncryptKey(string algorithm, byte[] keyData) |
| | | 45 | | { |
| | | 46 | | switch (algorithm) |
| | | 47 | | { |
| | | 48 | | case SecurityAlgorithms.RsaV15KeyWrap: |
| | 0 | 49 | | return EncryptedXml.EncryptKey(keyData, _rsa, false); |
| | | 50 | | case SecurityAlgorithms.RsaOaepKeyWrap: |
| | 0 | 51 | | return EncryptedXml.EncryptKey(keyData, _rsa, true); |
| | | 52 | | default: |
| | 0 | 53 | | if (IsSupportedAlgorithm(algorithm)) |
| | | 54 | | { |
| | 0 | 55 | | return EncryptedXml.EncryptKey(keyData, _rsa, false); |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format(SR.Un |
| | 0 | 59 | | algorithm, nameof(EncryptKey)))); |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public override AsymmetricAlgorithm GetAsymmetricAlgorithm(string algorithm, bool requiresPrivateKey) |
| | | 64 | | { |
| | 0 | 65 | | if (requiresPrivateKey && !HasPrivateKey()) |
| | | 66 | | { |
| | 0 | 67 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.NoPrivateKeyAvai |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | return _rsa; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | public override HashAlgorithm GetHashAlgorithmForSignature(string algorithm) |
| | | 74 | | { |
| | 0 | 75 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 76 | | { |
| | 0 | 77 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, SR.Format(SR.EmptyOrNullArgument |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | object algorithmObject = CryptoHelper.GetAlgorithmFromConfig(algorithm); |
| | | 81 | | |
| | 0 | 82 | | if (algorithmObject != null) |
| | | 83 | | { |
| | 0 | 84 | | if (algorithmObject is SignatureDescription description) |
| | | 85 | | { |
| | 0 | 86 | | return description.CreateDigest(); |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | if (algorithmObject is HashAlgorithm hashAlgorithm) |
| | | 90 | | { |
| | 0 | 91 | | return hashAlgorithm; |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format(SR.Unsupp |
| | 0 | 95 | | algorithm))); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | switch (algorithm) |
| | | 99 | | { |
| | | 100 | | case SecurityAlgorithms.RsaSha1Signature: |
| | 0 | 101 | | return CryptoHelper.NewSha1HashAlgorithm(); |
| | | 102 | | case SecurityAlgorithms.RsaSha256Signature: |
| | 0 | 103 | | return CryptoHelper.NewSha256HashAlgorithm(); |
| | | 104 | | default: |
| | 0 | 105 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format(SR.Un |
| | 0 | 106 | | algorithm, "GetHashAlgorithmForSignature"))); |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public override AsymmetricSignatureDeformatter GetSignatureDeformatter(string algorithm) |
| | | 111 | | { |
| | 0 | 112 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 113 | | { |
| | 0 | 114 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, SR.Format(SR.EmptyOrNullArgument |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | object algorithmObject = CryptoHelper.GetAlgorithmFromConfig(algorithm); |
| | 0 | 118 | | if (algorithmObject != null) |
| | | 119 | | { |
| | 0 | 120 | | if (algorithmObject is SignatureDescription description) |
| | | 121 | | { |
| | 0 | 122 | | return description.CreateDeformatter(_rsa); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | try |
| | | 126 | | { |
| | 0 | 127 | | if (algorithmObject is AsymmetricSignatureDeformatter asymmetricSignatureDeformatter) |
| | | 128 | | { |
| | 0 | 129 | | asymmetricSignatureDeformatter.SetKey(_rsa); |
| | 0 | 130 | | return asymmetricSignatureDeformatter; |
| | | 131 | | } |
| | 0 | 132 | | } |
| | 0 | 133 | | catch (InvalidCastException e) |
| | | 134 | | { |
| | 0 | 135 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Alg |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format(SR.Unsupp |
| | 0 | 139 | | algorithm, "GetSignatureDeformatter"))); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | switch (algorithm) |
| | | 143 | | { |
| | | 144 | | case SecurityAlgorithms.RsaSha1Signature: |
| | | 145 | | case SecurityAlgorithms.RsaSha256Signature: |
| | 0 | 146 | | return new RSAPKCS1SignatureDeformatter(_rsa); |
| | | 147 | | default: |
| | 0 | 148 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format(SR.Un |
| | 0 | 149 | | algorithm, "GetSignatureDeformatter"))); |
| | | 150 | | } |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | public override AsymmetricSignatureFormatter GetSignatureFormatter(string algorithm) |
| | | 154 | | { |
| | 0 | 155 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 156 | | { |
| | 0 | 157 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, SR.Format(SR.EmptyOrNullArgument |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | object algorithmObject = CryptoHelper.GetAlgorithmFromConfig(algorithm); |
| | 0 | 161 | | if (algorithmObject != null) |
| | | 162 | | { |
| | 0 | 163 | | if (algorithmObject is SignatureDescription description) |
| | | 164 | | { |
| | 0 | 165 | | return description.CreateFormatter(_rsa); |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | try |
| | | 169 | | { |
| | 0 | 170 | | if (algorithmObject is AsymmetricSignatureFormatter asymmetricSignatureFormatter) |
| | | 171 | | { |
| | 0 | 172 | | asymmetricSignatureFormatter.SetKey(_rsa); |
| | 0 | 173 | | return asymmetricSignatureFormatter; |
| | | 174 | | } |
| | 0 | 175 | | } |
| | 0 | 176 | | catch (InvalidCastException e) |
| | | 177 | | { |
| | 0 | 178 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Alg |
| | | 179 | | } |
| | | 180 | | |
| | 0 | 181 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format(SR.Unsupp |
| | 0 | 182 | | algorithm, nameof(GetSignatureFormatter)))); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | switch (algorithm) |
| | | 186 | | { |
| | | 187 | | case SecurityAlgorithms.RsaSha1Signature: |
| | | 188 | | case SecurityAlgorithms.RsaSha256Signature: |
| | | 189 | | // Ensure that we have an RSA algorithm object. |
| | 0 | 190 | | return new RSAPKCS1SignatureFormatter(_rsa); |
| | | 191 | | default: |
| | 0 | 192 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.Format(SR.Un |
| | 0 | 193 | | algorithm, "GetSignatureFormatter"))); |
| | | 194 | | } |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | public override bool HasPrivateKey() |
| | | 198 | | { |
| | 0 | 199 | | if (_privateKeyStatus == PrivateKeyStatus.AvailabilityNotDetermined) |
| | | 200 | | { |
| | 0 | 201 | | if (_rsa is RSACryptoServiceProvider rsaCryptoServiceProvider) |
| | | 202 | | { |
| | 0 | 203 | | _privateKeyStatus = rsaCryptoServiceProvider.PublicOnly |
| | 0 | 204 | | ? PrivateKeyStatus.DoesNotHavePrivateKey |
| | 0 | 205 | | : PrivateKeyStatus.HasPrivateKey; |
| | | 206 | | } |
| | | 207 | | else |
| | | 208 | | { |
| | | 209 | | try |
| | | 210 | | { |
| | 0 | 211 | | byte[] hash = new byte[20]; |
| | 0 | 212 | | _rsa.DecryptValue(hash); // imitate signing |
| | 0 | 213 | | _privateKeyStatus = PrivateKeyStatus.HasPrivateKey; |
| | 0 | 214 | | } |
| | | 215 | | #pragma warning disable CA1031 // Do not catch general exception types - interpret as no private key, don't need to use |
| | 0 | 216 | | catch (CryptographicException) |
| | | 217 | | { |
| | 0 | 218 | | _privateKeyStatus = PrivateKeyStatus.DoesNotHavePrivateKey; |
| | 0 | 219 | | } |
| | | 220 | | #pragma warning restore CA1031 // Do not catch general exception types |
| | | 221 | | } |
| | | 222 | | } |
| | 0 | 223 | | return _privateKeyStatus == PrivateKeyStatus.HasPrivateKey; |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | public override bool IsSupportedAlgorithm(string algorithm) |
| | | 227 | | { |
| | 0 | 228 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 229 | | { |
| | 0 | 230 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, SR.Format(SR.EmptyOrNullArgument |
| | | 231 | | } |
| | | 232 | | object algorithmObject; |
| | | 233 | | try |
| | | 234 | | { |
| | 0 | 235 | | algorithmObject = CryptoHelper.GetAlgorithmFromConfig(algorithm); |
| | 0 | 236 | | } |
| | | 237 | | #pragma warning disable CA1031 // Do not catch general exception types |
| | 0 | 238 | | catch (InvalidOperationException) |
| | | 239 | | { |
| | 0 | 240 | | algorithmObject = null; |
| | | 241 | | // We swallow the exception and continue. |
| | 0 | 242 | | } |
| | | 243 | | #pragma warning restore CA1031 // Do not catch general exception types |
| | | 244 | | |
| | 0 | 245 | | if (algorithmObject != null) |
| | | 246 | | { |
| | 0 | 247 | | if (algorithmObject is SignatureDescription signatureDescription) |
| | | 248 | | { |
| | 0 | 249 | | return true; |
| | | 250 | | } |
| | | 251 | | |
| | 0 | 252 | | if (algorithmObject is AsymmetricAlgorithm asymmetricAlgorithm) |
| | | 253 | | { |
| | 0 | 254 | | return true; |
| | | 255 | | } |
| | | 256 | | |
| | 0 | 257 | | return false; |
| | | 258 | | } |
| | | 259 | | switch (algorithm) |
| | | 260 | | { |
| | | 261 | | case SecurityAlgorithms.RsaV15KeyWrap: |
| | | 262 | | case SecurityAlgorithms.RsaOaepKeyWrap: |
| | | 263 | | case SecurityAlgorithms.RsaSha1Signature: |
| | | 264 | | case SecurityAlgorithms.RsaSha256Signature: |
| | 0 | 265 | | return true; |
| | | 266 | | default: |
| | 0 | 267 | | return false; |
| | | 268 | | } |
| | | 269 | | } |
| | | 270 | | |
| | | 271 | | private enum PrivateKeyStatus |
| | | 272 | | { |
| | | 273 | | AvailabilityNotDetermined, |
| | | 274 | | HasPrivateKey, |
| | | 275 | | DoesNotHavePrivateKey |
| | | 276 | | } |
| | | 277 | | } |
| | | 278 | | } |