| | | 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 CoreWCF.IdentityModel.Selectors; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// SecurityKeyElement provides delayed resolution of security keys by resolving the SecurityKeyIdentifierClause or |
| | | 11 | | /// only when cryptographic functions are needed. This allows a key clause or identifier that is never used by an a |
| | | 12 | | /// to be serialized and deserialzied on and off the wire without issue. |
| | | 13 | | /// </summary> |
| | | 14 | | public class SecurityKeyElement : SecurityKey |
| | | 15 | | { |
| | | 16 | | private SecurityKey _securityKey; |
| | | 17 | | private object _keyLock; |
| | | 18 | | private SecurityTokenResolver _securityTokenResolver; |
| | | 19 | | private SecurityKeyIdentifier _securityKeyIdentifier; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Constructor to use when working with SecurityKeyIdentifierClauses |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="securityKeyIdentifierClause">SecurityKeyIdentifierClause that represents a SecuriytKey</param> |
| | | 25 | | /// <param name="securityTokenResolver">SecurityTokenResolver that can be resolved to a SecurityKey</param> |
| | | 26 | | /// <exception cref="ArgumentNullException">Thrown if the 'clause' is null</exception> |
| | 0 | 27 | | public SecurityKeyElement(SecurityKeyIdentifierClause securityKeyIdentifierClause, SecurityTokenResolver securit |
| | | 28 | | { |
| | 0 | 29 | | if (securityKeyIdentifierClause == null) |
| | | 30 | | { |
| | 0 | 31 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityKeyIdentifierClause)); |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | Initialize(new SecurityKeyIdentifier(securityKeyIdentifierClause), securityTokenResolver); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Constructor to use when working with SecurityKeyIdentifiers |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="securityKeyIdentifier">SecurityKeyIdentifier that represents a SecuriytKey</param> |
| | | 41 | | /// <param name="securityTokenResolver">SecurityTokenResolver that can be resolved to a SecurityKey</param> |
| | | 42 | | /// <exception cref="ArgumentNullException">Thrown if the 'securityKeyIdentifier' is null</exception> |
| | 0 | 43 | | public SecurityKeyElement(SecurityKeyIdentifier securityKeyIdentifier, SecurityTokenResolver securityTokenResolv |
| | | 44 | | { |
| | 0 | 45 | | if (securityKeyIdentifier == null) |
| | | 46 | | { |
| | 0 | 47 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityKeyIdentifier)); |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | Initialize(securityKeyIdentifier, securityTokenResolver); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | private void Initialize(SecurityKeyIdentifier securityKeyIdentifier, SecurityTokenResolver securityTokenResolver |
| | | 54 | | { |
| | 0 | 55 | | _keyLock = new object(); |
| | 0 | 56 | | _securityKeyIdentifier = securityKeyIdentifier; |
| | 0 | 57 | | _securityTokenResolver = securityTokenResolver; |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Decrypts a key using the specified algorithm. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="algorithm">Algorithm to use when decrypting the key.</param> |
| | | 64 | | /// <param name="keyData">Bytes representing the encrypted key.</param> |
| | | 65 | | /// <returns>Decrypted bytes.</returns> |
| | | 66 | | public override byte[] DecryptKey(string algorithm, byte[] keyData) |
| | | 67 | | { |
| | 0 | 68 | | if (_securityKey == null) |
| | | 69 | | { |
| | 0 | 70 | | ResolveKey(); |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | return _securityKey.DecryptKey(algorithm, keyData); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Encrypts a key using the specified algorithm. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="algorithm">Algorithm to use when encrypting the key.</param> |
| | | 80 | | /// <param name="keyData">Bytes representing the key.</param> |
| | | 81 | | /// <returns>Encrypted bytes.</returns> |
| | | 82 | | public override byte[] EncryptKey(string algorithm, byte[] keyData) |
| | | 83 | | { |
| | 0 | 84 | | if (_securityKey == null) |
| | | 85 | | { |
| | 0 | 86 | | ResolveKey(); |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | return _securityKey.EncryptKey(algorithm, keyData); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Answers question: is the algorithm Asymmetric. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="algorithm">Algorithm to check.</param> |
| | | 96 | | /// <returns>True if algorithm will be processed by runtime as Asymmetric.</returns> |
| | | 97 | | public virtual bool IsAsymmetricAlgorithm(string algorithm) |
| | | 98 | | { |
| | | 99 | | // Copied from System.IdentityModel.CryptoHelper |
| | | 100 | | // no need to ResolveKey |
| | | 101 | | |
| | | 102 | | switch (algorithm) |
| | | 103 | | { |
| | | 104 | | case SecurityAlgorithms.DsaSha1Signature: |
| | | 105 | | case SecurityAlgorithms.RsaSha1Signature: |
| | | 106 | | case SecurityAlgorithms.RsaSha256Signature: |
| | | 107 | | case SecurityAlgorithms.RsaOaepKeyWrap: |
| | | 108 | | case SecurityAlgorithms.RsaV15KeyWrap: |
| | 0 | 109 | | return true; |
| | | 110 | | default: |
| | 0 | 111 | | return false; |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Answers question: is the algorithm is supported by this key. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="algorithm">Algorithm to check.</param> |
| | | 119 | | /// <returns>True if algorithm is supported by this key.</returns> |
| | | 120 | | public override bool IsSupportedAlgorithm(string algorithm) |
| | | 121 | | { |
| | 0 | 122 | | if (_securityKey == null) |
| | | 123 | | { |
| | 0 | 124 | | ResolveKey(); |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | return _securityKey.IsSupportedAlgorithm(algorithm); |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Answers question: is the algorithm Symmetric. |
| | | 132 | | /// </summary> |
| | | 133 | | /// <param name="algorithm">Algorithm to check.</param> |
| | | 134 | | /// <returns>True if algorithm will be processed by runtime as Symmetric.</returns> |
| | | 135 | | public virtual bool IsSymmetricAlgorithm(string algorithm) |
| | | 136 | | { |
| | | 137 | | // Copied from System.IdentityModel.CryptoHelper |
| | | 138 | | // no need to ResolveKey. |
| | | 139 | | |
| | | 140 | | switch (algorithm) |
| | | 141 | | { |
| | | 142 | | case SecurityAlgorithms.DsaSha1Signature: |
| | | 143 | | case SecurityAlgorithms.RsaSha1Signature: |
| | | 144 | | case SecurityAlgorithms.RsaSha256Signature: |
| | | 145 | | case SecurityAlgorithms.RsaOaepKeyWrap: |
| | | 146 | | case SecurityAlgorithms.RsaV15KeyWrap: |
| | 0 | 147 | | return false; |
| | | 148 | | case SecurityAlgorithms.HmacSha1Signature: |
| | | 149 | | case SecurityAlgorithms.HmacSha256Signature: |
| | | 150 | | case SecurityAlgorithms.Aes128Encryption: |
| | | 151 | | case SecurityAlgorithms.Aes192Encryption: |
| | | 152 | | case SecurityAlgorithms.Aes256Encryption: |
| | | 153 | | case SecurityAlgorithms.TripleDesEncryption: |
| | | 154 | | case SecurityAlgorithms.Aes128KeyWrap: |
| | | 155 | | case SecurityAlgorithms.Aes192KeyWrap: |
| | | 156 | | case SecurityAlgorithms.Aes256KeyWrap: |
| | | 157 | | case SecurityAlgorithms.TripleDesKeyWrap: |
| | | 158 | | case SecurityAlgorithms.Psha1KeyDerivation: |
| | | 159 | | case SecurityAlgorithms.Psha1KeyDerivationDec2005: |
| | 0 | 160 | | return true; |
| | | 161 | | default: |
| | 0 | 162 | | return false; |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Gets the key size in bits. |
| | | 168 | | /// </summary> |
| | | 169 | | /// <returns>Key size in bits.</returns> |
| | | 170 | | public override int KeySize |
| | | 171 | | { |
| | | 172 | | get |
| | | 173 | | { |
| | 0 | 174 | | if (_securityKey == null) |
| | | 175 | | { |
| | 0 | 176 | | ResolveKey(); |
| | | 177 | | } |
| | | 178 | | |
| | 0 | 179 | | return _securityKey.KeySize; |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Attempts to resolve the _securityKeyIdentifier into a securityKey. If successful, the private _securityKey |
| | | 185 | | /// Uses the tokenresolver that was passed in, it may be the case a keyIdentifier can |
| | | 186 | | /// generate a securityKey. A RSA key can generate a key with just the public part. |
| | | 187 | | /// </summary> |
| | | 188 | | /// <returns>void</returns> |
| | | 189 | | private void ResolveKey() |
| | | 190 | | { |
| | 0 | 191 | | if (_securityKeyIdentifier == null) |
| | | 192 | | { |
| | 0 | 193 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("ski"); |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | if (_securityKey == null) |
| | | 197 | | { |
| | 0 | 198 | | lock (_keyLock) |
| | | 199 | | { |
| | 0 | 200 | | if (_securityKey == null) |
| | | 201 | | { |
| | | 202 | | |
| | 0 | 203 | | if (_securityTokenResolver != null) |
| | | 204 | | { |
| | 0 | 205 | | for (int i = 0; i < _securityKeyIdentifier.Count; ++i) |
| | | 206 | | { |
| | 0 | 207 | | if (_securityTokenResolver.TryResolveSecurityKey(_securityKeyIdentifier[i], out _securit |
| | | 208 | | { |
| | 0 | 209 | | return; |
| | | 210 | | } |
| | | 211 | | } |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | // most likely a public key, do this last |
| | 0 | 215 | | if (_securityKeyIdentifier.CanCreateKey) |
| | | 216 | | { |
| | 0 | 217 | | _securityKey = _securityKeyIdentifier.CreateKey(); |
| | 0 | 218 | | return; |
| | | 219 | | } |
| | | 220 | | |
| | 0 | 221 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelper( |
| | 0 | 222 | | new SecurityTokenException(SR.Format(SR.ID2080, |
| | 0 | 223 | | _securityTokenResolver == null ? "null" : _securityTokenResolver.ToString(), |
| | 0 | 224 | | _securityKeyIdentifier == null ? "null" : _securityKeyIdentifier.ToString())), S |
| | | 225 | | } |
| | 0 | 226 | | } |
| | | 227 | | } |
| | 0 | 228 | | } |
| | | 229 | | } |
| | | 230 | | } |