< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.Tokens.WrappedKeySecurityToken
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/WrappedKeySecurityToken.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 74
Coverable lines: 74
Total lines: 193
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 32
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%440%
.ctor(...)100%110%
.ctor(...)0%440%
.ctor(...)0%660%
.ctor(...)100%110%
GetHash()0%220%
GetWrappedKey()100%110%
EnsureEncryptedKeySetUp()0%660%
CanCreateKeyIdentifierClause()0%220%
CreateKeyIdentifierClause()0%220%
MatchesKeyIdentifierClause(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/WrappedKeySecurityToken.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.Collections.ObjectModel;
 6using System.Security.Cryptography;
 7using System.Xml;
 8using CoreWCF.IdentityModel;
 9using CoreWCF.IdentityModel.Tokens;
 10
 11namespace CoreWCF.Security.Tokens
 12{
 13    public class WrappedKeySecurityToken : SecurityToken
 14    {
 15        private readonly string _id;
 16        private readonly DateTime _effectiveTime;
 17        private readonly ReadOnlyCollection<SecurityKey> _securityKey;
 18        private readonly byte[] _wrappedKey;
 19        private readonly bool _serializeCarriedKeyName;
 20        private byte[] _wrappedKeyHash;
 21        private readonly XmlDictionaryString _wrappingAlgorithmDictionaryString;
 22
 23        internal WrappedKeySecurityToken(string id, byte[] keyToWrap, ISspiNegotiation wrappingSspiContext)
 024            : this(id, keyToWrap, (wrappingSspiContext != null) ? (wrappingSspiContext.KeyEncryptionAlgorithm) : null, w
 25        {
 26
 027        }
 28
 29        internal WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, ISspiNegotiation wrappin
 030        : this(id, keyToWrap, wrappingAlgorithm, null)
 31        {
 032            if (wrappingSspiContext == null)
 33            {
 034                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(wrappingSspiContext));
 35            }
 36
 037            if (wrappedKey == null)
 38            {
 039                _wrappedKey = wrappingSspiContext.Encrypt(keyToWrap);
 40            }
 41            else
 42            {
 043                _wrappedKey = wrappedKey;
 44            }
 045           _serializeCarriedKeyName = false;
 046        }
 47
 48        internal WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, XmlDictionaryString wrap
 049            : this(id, keyToWrap, wrappingAlgorithm, wrappingAlgorithmDictionaryString, wrappingToken, wrappingTokenRefe
 50        {
 051        }
 52
 53        internal WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, XmlDictionaryString wrap
 054             : this(id, keyToWrap, wrappingAlgorithm, wrappingAlgorithmDictionaryString)
 55        {
 056            WrappingToken = wrappingToken ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(wra
 057            WrappingTokenReference = wrappingTokenReference;
 058            if (wrappedKey == null)
 59            {
 060                _wrappedKey = SecurityUtils.EncryptKey(wrappingToken, wrappingAlgorithm, keyToWrap);
 61            }
 62            else
 63            {
 064                _wrappedKey = wrappedKey;
 65            }
 066            WrappingSecurityKey = wrappingSecurityKey;
 067            _serializeCarriedKeyName = true;
 068        }
 69
 070        private WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, XmlDictionaryString wrapp
 71        {
 072            if (keyToWrap == null)
 73            {
 074                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyToWrap));
 75            }
 76
 077            _id = id ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(id));
 078            _effectiveTime = DateTime.UtcNow;
 079            _securityKey = SecurityUtils.CreateSymmetricSecurityKeys(keyToWrap);
 080            WrappingAlgorithm = wrappingAlgorithm ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(na
 081            _wrappingAlgorithmDictionaryString = wrappingAlgorithmDictionaryString;
 082        }
 83
 84        internal WrappedKeySecurityToken(
 85          string id,
 86          byte[] keyToWrap,
 87          string wrappingAlgorithm,
 88          SecurityToken wrappingToken,
 89          SecurityKeyIdentifier wrappingTokenReference,
 90          byte[] wrappedKey,
 91          SecurityKey wrappingSecurityKey)
 092          : this(id, keyToWrap, wrappingAlgorithm, (XmlDictionaryString)null, wrappingToken, wrappingTokenReference, wra
 93        {
 094        }
 95
 096        public override string Id => _id;
 97
 098        public override DateTime ValidFrom => _effectiveTime;
 99
 0100        public override DateTime ValidTo => DateTime.MaxValue;
 101
 0102        internal EncryptedKey EncryptedKey { get; set; }
 103
 0104        internal ReferenceList ReferenceList => EncryptedKey?.ReferenceList;
 105
 0106        public string WrappingAlgorithm { get; }
 107
 0108        internal SecurityKey WrappingSecurityKey { get; }
 109
 0110        public SecurityToken WrappingToken { get; }
 111
 0112        public SecurityKeyIdentifier WrappingTokenReference { get; }
 113
 0114        internal string CarriedKeyName => null;
 115
 0116        public override ReadOnlyCollection<SecurityKey> SecurityKeys => _securityKey;
 117
 118        internal byte[] GetHash()
 119        {
 0120            if (_wrappedKeyHash == null)
 121            {
 0122                EnsureEncryptedKeySetUp();
 0123                using (HashAlgorithm hash = CryptoHelper.NewSha1HashAlgorithm())
 124                {
 0125                    _wrappedKeyHash = hash.ComputeHash(EncryptedKey.GetWrappedKey());
 0126                }
 127            }
 0128            return _wrappedKeyHash;
 129        }
 130
 131        public byte[] GetWrappedKey()
 132        {
 0133            return SecurityUtils.CloneBuffer(_wrappedKey);
 134        }
 135
 136        internal void EnsureEncryptedKeySetUp()
 137        {
 0138            if (EncryptedKey == null)
 139            {
 0140                EncryptedKey ek = new EncryptedKey
 0141                {
 0142                    Id = Id
 0143                };
 0144                if (_serializeCarriedKeyName)
 145                {
 0146                    ek.CarriedKeyName = CarriedKeyName;
 147                }
 148                else
 149                {
 0150                    ek.CarriedKeyName = null;
 151                }
 0152                ek.EncryptionMethod = WrappingAlgorithm;
 0153                ek.EncryptionMethodDictionaryString = _wrappingAlgorithmDictionaryString;
 0154                ek.SetUpKeyWrap(_wrappedKey);
 0155                if (WrappingTokenReference != null)
 156                {
 0157                    ek.KeyIdentifier = WrappingTokenReference;
 158                }
 0159                EncryptedKey = ek;
 160            }
 0161        }
 162
 163        public override bool CanCreateKeyIdentifierClause<T>()
 164        {
 0165            if (typeof(T) == typeof(EncryptedKeyHashIdentifierClause))
 166            {
 0167                return true;
 168            }
 169
 0170            return base.CanCreateKeyIdentifierClause<T>();
 171        }
 172
 173        public override T CreateKeyIdentifierClause<T>()
 174        {
 0175            if (typeof(T) == typeof(EncryptedKeyHashIdentifierClause))
 176            {
 0177                return new EncryptedKeyHashIdentifierClause(GetHash()) as T;
 178            }
 179
 0180            return base.CreateKeyIdentifierClause<T>();
 181        }
 182
 183        public override bool MatchesKeyIdentifierClause(SecurityKeyIdentifierClause keyIdentifierClause)
 184        {
 0185            if (keyIdentifierClause is EncryptedKeyHashIdentifierClause encKeyIdentifierClause)
 186            {
 0187                return encKeyIdentifierClause.Matches(GetHash());
 188            }
 189
 0190            return base.MatchesKeyIdentifierClause(keyIdentifierClause);
 191        }
 192    }
 193}