< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.X509CertificateStoreTokenResolver
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/X509CertificateStoreTokenResolver.cs
Line coverage
10%
Covered lines: 7
Uncovered lines: 62
Coverable lines: 69
Total lines: 202
Line coverage: 10.1%
Branch coverage
2%
Covered branches: 1
Total branches: 50
Branch coverage: 2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
.ctor(...)100%11100%
.ctor(...)50%2283.33%
TryResolveSecurityKeyCore(...)0%16160%
TryResolveTokenCore(...)0%660%
TryResolveTokenCore(...)0%26260%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/X509CertificateStoreTokenResolver.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 System.Security.Cryptography.X509Certificates;
 6using CoreWCF.IdentityModel.Selectors;
 7
 8namespace CoreWCF.IdentityModel.Tokens
 9{
 10    /// <summary>
 11    /// Token Resolver that can resolve X509SecurityTokens against a given X.509 Certificate Store.
 12    /// </summary>
 13    public class X509CertificateStoreTokenResolver : SecurityTokenResolver
 14    {
 15        /// <summary>
 16        /// Initializes an instance of <see cref="X509CertificateStoreTokenResolver"/>
 17        /// </summary>
 18        public X509CertificateStoreTokenResolver()
 019            : this(System.Security.Cryptography.X509Certificates.StoreName.My, StoreLocation.LocalMachine)
 20        {
 021        }
 22
 23        /// <summary>
 24        /// Initializes an instance of <see cref="X509CertificateStoreTokenResolver"/>
 25        /// </summary>
 26        /// <param name="storeName">StoreName of the X.509 Certificate Store.</param>
 27        /// <param name="storeLocation">StoreLocation of the X.509 Certificate store.</param>
 28        public X509CertificateStoreTokenResolver(StoreName storeName, StoreLocation storeLocation)
 229            : this(Enum.GetName(typeof(System.Security.Cryptography.X509Certificates.StoreName), storeName), storeLocati
 30        {
 231        }
 32
 33        /// <summary>
 34        /// Initializes an instance of <see cref="X509CertificateStoreTokenResolver"/>
 35        /// </summary>
 36        /// <param name="storeName">StoreName of the X.509 Certificate Store.</param>
 37        /// <param name="storeLocation">StoreLocation of the X.509 Certificate store.</param>
 238        public X509CertificateStoreTokenResolver(string storeName, StoreLocation storeLocation)
 39        {
 240            if (string.IsNullOrEmpty(storeName))
 41            {
 042                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(storeName));
 43            }
 44
 245            StoreName = storeName;
 246            StoreLocation = storeLocation;
 247        }
 48
 49        /// <summary>
 50        /// Gets the StoreName used by this TokenResolver.
 51        /// </summary>
 052        public string StoreName { get; }
 53
 54        /// <summary>
 55        /// Gets the StoreLocation used by this TokenResolver.
 56        /// </summary>
 057        public StoreLocation StoreLocation { get; }
 58
 59        /// <summary>
 60        /// Resolves the given SecurityKeyIdentifierClause to a SecurityKey.
 61        /// </summary>
 62        /// <param name="keyIdentifierClause">SecurityKeyIdentifierClause to resolve</param>
 63        /// <param name="key">The resolved SecurityKey.</param>
 64        /// <returns>True if successfully resolved.</returns>
 65        /// <exception cref="ArgumentNullException">The input argument 'keyIdentifierClause' is null.</exception>
 66        protected override bool TryResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityK
 67        {
 068            if (keyIdentifierClause == null)
 69            {
 070                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 71            }
 72
 073            key = null;
 074            if (keyIdentifierClause is EncryptedKeyIdentifierClause encryptedKeyIdentifierClause)
 75            {
 076                SecurityKeyIdentifier keyIdentifier = encryptedKeyIdentifierClause.EncryptingKeyIdentifier;
 077                if (keyIdentifier != null && keyIdentifier.Count > 0)
 78                {
 079                    for (int i = 0; i < keyIdentifier.Count; i++)
 80                    {
 081                        if (TryResolveSecurityKey(keyIdentifier[i], out SecurityKey unwrappingSecurityKey))
 82                        {
 083                            byte[] wrappedKey = encryptedKeyIdentifierClause.GetEncryptedKey();
 084                            string wrappingAlgorithm = encryptedKeyIdentifierClause.EncryptionMethod;
 085                            byte[] unwrappedKey = unwrappingSecurityKey.DecryptKey(wrappingAlgorithm, wrappedKey);
 086                            key = new InMemorySymmetricSecurityKey(unwrappedKey, false);
 087                            return true;
 88                        }
 89                    }
 90                }
 91            }
 92            else
 93            {
 094                SecurityToken token = null;
 095                if (TryResolveToken(keyIdentifierClause, out token))
 96                {
 097                    if (token.SecurityKeys.Count > 0)
 98                    {
 099                        key = token.SecurityKeys[0];
 0100                        return true;
 101                    }
 102                }
 103            }
 104
 0105            return false;
 106        }
 107
 108        /// <summary>
 109        /// Resolves the given SecurityKeyIdentifier to a SecurityToken.
 110        /// </summary>
 111        /// <param name="keyIdentifier">SecurityKeyIdentifier to resolve.</param>
 112        /// <param name="token">The resolved SecurityToken.</param>
 113        /// <returns>True if successfully resolved.</returns>
 114        /// <exception cref="ArgumentNullException">The input argument 'keyIdentifier' is null.</exception>
 115        protected override bool TryResolveTokenCore(SecurityKeyIdentifier keyIdentifier, out SecurityToken token)
 116        {
 0117            if (keyIdentifier == null)
 118            {
 0119                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier));
 120            }
 121
 0122            token = null;
 0123            foreach (SecurityKeyIdentifierClause clause in keyIdentifier)
 124            {
 0125                if (TryResolveToken(clause, out token))
 126                {
 0127                    return true;
 128                }
 129            }
 130
 0131            return false;
 0132        }
 133
 134        /// <summary>
 135        /// Resolves the given SecurityKeyIdentifierClause to a SecurityToken.
 136        /// </summary>
 137        /// <param name="keyIdentifierClause">SecurityKeyIdentifierClause to resolve.</param>
 138        /// <param name="token">The resolved SecurityToken.</param>
 139        /// <returns>True if successfully resolved.</returns>
 140        /// <exception cref="ArgumentNullException">The input argument 'keyIdentifierClause' is null.</exception>
 141        protected override bool TryResolveTokenCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken t
 142        {
 0143            if (keyIdentifierClause == null)
 144            {
 0145                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 146            }
 147
 0148            token = null;
 0149            X509Store store = null;
 0150            X509Certificate2Collection certs = null;
 151            try
 152            {
 0153                store = new X509Store(StoreName, StoreLocation);
 0154                store.Open(OpenFlags.ReadOnly);
 0155                certs = store.Certificates;
 0156                foreach (X509Certificate2 cert in certs)
 157                {
 0158                    if (keyIdentifierClause is X509ThumbprintKeyIdentifierClause thumbprintKeyIdentifierClause && thumbp
 159                    {
 0160                        token = new X509SecurityToken(cert);
 0161                        return true;
 162                    }
 163
 0164                    if (keyIdentifierClause is X509IssuerSerialKeyIdentifierClause issuerSerialKeyIdentifierClause && is
 165                    {
 0166                        token = new X509SecurityToken(cert);
 0167                        return true;
 168                    }
 169
 0170                    if (keyIdentifierClause is X509SubjectKeyIdentifierClause subjectKeyIdentifierClause && subjectKeyId
 171                    {
 0172                        token = new X509SecurityToken(cert);
 0173                        return true;
 174                    }
 175
 0176                    if (keyIdentifierClause is X509RawDataKeyIdentifierClause rawDataKeyIdentifierClause && rawDataKeyId
 177                    {
 0178                        token = new X509SecurityToken(cert);
 0179                        return true;
 180                    }
 181                }
 0182            }
 183            finally
 184            {
 0185                if (certs != null)
 186                {
 0187                    for (int i = 0; i < certs.Count; i++)
 188                    {
 0189                        certs[i].Reset();
 190                    }
 191                }
 192
 0193                if (store != null)
 194                {
 0195                    store.Close();
 196                }
 0197            }
 198
 0199            return false;
 0200        }
 201    }
 202}