< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.SecurityKeyElement
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SecurityKeyElement.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 49
Coverable lines: 49
Total lines: 230
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 114
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%220%
.ctor(...)0%220%
Initialize(...)100%110%
DecryptKey(...)0%220%
EncryptKey(...)0%220%
IsAsymmetricAlgorithm(...)0%10100%
IsSupportedAlgorithm(...)0%220%
IsSymmetricAlgorithm(...)0%74740%
ResolveKey()0%18180%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SecurityKeyElement.cs

#LineLine coverage
 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
 4using System;
 5using CoreWCF.IdentityModel.Selectors;
 6
 7namespace 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>
 027        public SecurityKeyElement(SecurityKeyIdentifierClause securityKeyIdentifierClause, SecurityTokenResolver securit
 28        {
 029            if (securityKeyIdentifierClause == null)
 30            {
 031                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityKeyIdentifierClause));
 32            }
 33
 034            Initialize(new SecurityKeyIdentifier(securityKeyIdentifierClause), securityTokenResolver);
 035        }
 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>
 043        public SecurityKeyElement(SecurityKeyIdentifier securityKeyIdentifier, SecurityTokenResolver securityTokenResolv
 44        {
 045            if (securityKeyIdentifier == null)
 46            {
 047                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityKeyIdentifier));
 48            }
 49
 050            Initialize(securityKeyIdentifier, securityTokenResolver);
 051        }
 52
 53        private void Initialize(SecurityKeyIdentifier securityKeyIdentifier, SecurityTokenResolver securityTokenResolver
 54        {
 055            _keyLock = new object();
 056            _securityKeyIdentifier = securityKeyIdentifier;
 057            _securityTokenResolver = securityTokenResolver;
 058        }
 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        {
 068            if (_securityKey == null)
 69            {
 070                ResolveKey();
 71            }
 72
 073            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        {
 084            if (_securityKey == null)
 85            {
 086                ResolveKey();
 87            }
 88
 089            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:
 0109                    return true;
 110                default:
 0111                    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        {
 0122            if (_securityKey == null)
 123            {
 0124                ResolveKey();
 125            }
 126
 0127            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:
 0147                    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:
 0160                    return true;
 161                default:
 0162                    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            {
 0174                if (_securityKey == null)
 175                {
 0176                    ResolveKey();
 177                }
 178
 0179                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        {
 0191            if (_securityKeyIdentifier == null)
 192            {
 0193                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("ski");
 194            }
 195
 0196            if (_securityKey == null)
 197            {
 0198                lock (_keyLock)
 199                {
 0200                    if (_securityKey == null)
 201                    {
 202
 0203                        if (_securityTokenResolver != null)
 204                        {
 0205                            for (int i = 0; i < _securityKeyIdentifier.Count; ++i)
 206                            {
 0207                                if (_securityTokenResolver.TryResolveSecurityKey(_securityKeyIdentifier[i], out _securit
 208                                {
 0209                                    return;
 210                                }
 211                            }
 212                        }
 213
 214                        // most likely a public key, do this last
 0215                        if (_securityKeyIdentifier.CanCreateKey)
 216                        {
 0217                            _securityKey = _securityKeyIdentifier.CreateKey();
 0218                            return;
 219                        }
 220
 0221                        throw DiagnosticUtility.ExceptionUtility.ThrowHelper(
 0222                            new SecurityTokenException(SR.Format(SR.ID2080,
 0223                                        _securityTokenResolver == null ? "null" : _securityTokenResolver.ToString(),
 0224                                        _securityKeyIdentifier == null ? "null" : _securityKeyIdentifier.ToString())), S
 225                    }
 0226                }
 227            }
 0228        }
 229    }
 230}