< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.Tokens.DerivedKeySecurityToken
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/DerivedKeySecurityToken.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 112
Coverable lines: 112
Total lines: 241
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 52
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%110%
.ctor(...)100%110%
.ctor(...)0%660%
.ctor(...)100%110%
.ctor(...)100%110%
GetKeyBytes()100%110%
GetNonce()100%110%
TryGetSecurityKeys(...)100%110%
ToString()100%110%
Initialize(...)100%110%
Initialize(...)0%22220%
InitializeDerivedKey(...)0%12120%
InitializeDerivedKey(...)100%110%
EnsureAcceptableOffset(...)0%10100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/DerivedKeySecurityToken.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.Generic;
 6using System.Collections.ObjectModel;
 7using System.Globalization;
 8using System.IO;
 9using System.Security.Cryptography;
 10using System.Text;
 11using System.Xml;
 12using CoreWCF.IdentityModel;
 13using CoreWCF.IdentityModel.Tokens;
 14
 15namespace CoreWCF.Security.Tokens
 16{
 17    internal sealed class DerivedKeySecurityToken : SecurityToken
 18    {
 019        private static readonly byte[] s_defaultLabel = new byte[]
 020            {
 021                (byte)'W', (byte)'S', (byte)'-', (byte)'S', (byte)'e', (byte)'c', (byte)'u', (byte)'r', (byte)'e',
 022                (byte)'C', (byte)'o', (byte)'n', (byte)'v', (byte)'e', (byte)'r', (byte)'s', (byte)'a', (byte)'t', (byte
 023                (byte)'W', (byte)'S', (byte)'-', (byte)'S', (byte)'e', (byte)'c', (byte)'u', (byte)'r', (byte)'e',
 024                (byte)'C', (byte)'o', (byte)'n', (byte)'v', (byte)'e', (byte)'r', (byte)'s', (byte)'a', (byte)'t', (byte
 025            };
 26
 27        public const int DefaultNonceLength = 16;
 28        public const int DefaultDerivedKeyLength = 32;
 29        private string _id;
 30        private byte[] _key;
 31        private ReadOnlyCollection<SecurityKey> _securityKeys;
 32
 33        // create from scratch
 34        public DerivedKeySecurityToken(SecurityToken tokenToDerive, SecurityKeyIdentifierClause tokenToDeriveIdentifier,
 035            : this(tokenToDerive, tokenToDeriveIdentifier, length, SecurityUtils.GenerateId())
 36        {
 037        }
 38
 039        internal DerivedKeySecurityToken(SecurityToken tokenToDerive, SecurityKeyIdentifierClause tokenToDeriveIdentifie
 040            int length, string id)
 41        {
 042            if (length != 16 && length != 24 && length != 32)
 43            {
 044                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.Psha1KeyLen
 45            }
 46
 047            byte[] nonce = new byte[DefaultNonceLength];
 048            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
 049            rng.GetBytes(nonce);
 50
 051            Initialize(id, -1, 0, length, null, nonce, tokenToDerive, tokenToDeriveIdentifier, SecurityAlgorithms.Psha1K
 052        }
 53
 054        internal DerivedKeySecurityToken(int generation, int offset, int length,
 055            string label, int minNonceLength, SecurityToken tokenToDerive,
 056            SecurityKeyIdentifierClause tokenToDeriveIdentifier,
 057            string derivationAlgorithm, string id)
 58        {
 059            byte[] nonce = new byte[minNonceLength];
 060            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
 061            rng.GetBytes(nonce);
 62
 063            Initialize(id, generation, offset, length, label, nonce, tokenToDerive, tokenToDeriveIdentifier, derivationA
 064        }
 65
 66        // create from xml
 067        internal DerivedKeySecurityToken(int generation, int offset, int length,
 068            string label, byte[] nonce, SecurityToken tokenToDerive,
 069            SecurityKeyIdentifierClause tokenToDeriveIdentifier, string derivationAlgorithm, string id)
 70        {
 071            Initialize(id, generation, offset, length, label, nonce, tokenToDerive, tokenToDeriveIdentifier, derivationA
 072        }
 73
 074        public override string Id => _id;
 75
 076        public override DateTime ValidFrom => TokenToDerive.ValidFrom;
 77
 078        public override DateTime ValidTo => TokenToDerive.ValidTo;
 79
 080        public string KeyDerivationAlgorithm { get; private set; }
 81
 082        public int Generation { get; private set; } = -1;
 83
 084        public string Label { get; private set; }
 85
 086        public int Length { get; private set; } = -1;
 87
 088        internal byte[] Nonce { get; private set; }
 89
 090        public int Offset { get; private set; } = -1;
 91
 092        internal SecurityToken TokenToDerive { get; private set; }
 93
 094        internal SecurityKeyIdentifierClause TokenToDeriveIdentifier { get; private set; }
 95
 96        public override ReadOnlyCollection<SecurityKey> SecurityKeys
 97        {
 98            get
 99            {
 0100                if (_securityKeys == null)
 101                {
 0102                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.DerivedKe
 103                }
 0104                return _securityKeys;
 105            }
 106        }
 107
 108        public byte[] GetKeyBytes()
 109        {
 0110            return SecurityUtils.CloneBuffer(_key);
 111        }
 112
 113        public byte[] GetNonce()
 114        {
 0115            return SecurityUtils.CloneBuffer(Nonce);
 116        }
 117
 118        internal bool TryGetSecurityKeys(out ReadOnlyCollection<SecurityKey> keys)
 119        {
 0120            keys = _securityKeys;
 0121            return (keys != null);
 122        }
 123
 124        public override string ToString()
 125        {
 0126            StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
 0127            writer.WriteLine("DerivedKeySecurityToken:");
 0128            writer.WriteLine("   Generation: {0}", Generation);
 0129            writer.WriteLine("   Offset: {0}", Offset);
 0130            writer.WriteLine("   Length: {0}", Length);
 0131            writer.WriteLine("   Label: {0}", Label);
 0132            writer.WriteLine("   Nonce: {0}", Convert.ToBase64String(Nonce));
 0133            writer.WriteLine("   TokenToDeriveFrom:");
 0134            using (XmlTextWriter xmlWriter = new XmlTextWriter(writer))
 135            {
 0136                xmlWriter.Formatting = Formatting.Indented;
 0137                SecurityStandardsManager.DefaultInstance.SecurityTokenSerializer.WriteKeyIdentifierClause(XmlDictionaryW
 0138            }
 0139            return writer.ToString();
 140        }
 141
 142        private void Initialize(string id, int generation, int offset, int length, string label, byte[] nonce,
 143            SecurityToken tokenToDerive, SecurityKeyIdentifierClause tokenToDeriveIdentifier, string derivationAlgorithm
 144        {
 0145            Initialize(id, generation, offset, length, label, nonce, tokenToDerive, tokenToDeriveIdentifier, derivationA
 0146        }
 147
 148        private void Initialize(string id, int generation, int offset, int length, string label, byte[] nonce,
 149            SecurityToken tokenToDerive, SecurityKeyIdentifierClause tokenToDeriveIdentifier, string derivationAlgorithm
 150            bool initializeDerivedKey)
 151        {
 0152            if (tokenToDerive == null)
 153            {
 0154                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokenToDerive));
 155            }
 156
 0157            if (!SecurityUtils.IsSupportedAlgorithm(derivationAlgorithm, tokenToDerive))
 158            {
 0159                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.DerivedKeyCannotDeriv
 160            }
 161
 0162            if (length == -1)
 163            {
 0164                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(length)
 165            }
 0166            if (offset == -1 && generation == -1)
 167            {
 0168                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.DerivedKeyPosAndGenNotSpecified);
 169            }
 0170            if (offset >= 0 && generation >= 0)
 171            {
 0172                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.DerivedKeyPosAndGenBothSpecified);
 173            }
 174
 0175            _id = id ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(id));
 0176            Label = label;
 0177            Nonce = nonce ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(nonce));
 0178            Length = length;
 0179            Offset = offset;
 0180            Generation = generation;
 0181            TokenToDerive = tokenToDerive;
 0182            TokenToDeriveIdentifier = tokenToDeriveIdentifier ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArg
 0183            KeyDerivationAlgorithm = derivationAlgorithm;
 184
 0185            if (initializeDerivedKey)
 186            {
 0187                InitializeDerivedKey(Length);
 188            }
 0189        }
 190
 191        internal void InitializeDerivedKey(int maxKeyLength)
 192        {
 0193            if (_key != null)
 194            {
 0195                return;
 196            }
 0197            if (Length > maxKeyLength)
 198            {
 0199                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.DerivedKeyLengthTooLong, Lengt
 200            }
 201
 0202            _key = SecurityUtils.GenerateDerivedKey(TokenToDerive, KeyDerivationAlgorithm,
 0203                (Label != null ? Encoding.UTF8.GetBytes(Label) : s_defaultLabel), Nonce, Length * 8,
 0204                ((Offset >= 0) ? Offset : Generation * Length));
 0205            if ((_key == null) || (_key.Length == 0))
 206            {
 0207                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.DerivedKeyCannotDeriveFromSecret);
 208            }
 0209            List<SecurityKey> temp = new List<SecurityKey>(1)
 0210            {
 0211                new InMemorySymmetricSecurityKey(_key, false)
 0212            };
 0213            _securityKeys = temp.AsReadOnly();
 0214        }
 215
 216        internal void InitializeDerivedKey(ReadOnlyCollection<SecurityKey> securityKeys)
 217        {
 0218            _key = ((SymmetricSecurityKey)securityKeys[0]).GetSymmetricKey();
 0219            _securityKeys = securityKeys;
 0220        }
 221
 222        internal static void EnsureAcceptableOffset(int offset, int generation, int length, int maxOffset)
 223        {
 0224            if (offset != -1)
 225            {
 0226                if (offset > maxOffset)
 227                {
 0228                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(S
 229                }
 230            }
 231            else
 232            {
 0233                int effectiveOffset = generation * length;
 0234                if ((effectiveOffset < generation && effectiveOffset < length) || effectiveOffset > maxOffset)
 235                {
 0236                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(S
 237                }
 238            }
 0239        }
 240    }
 241}

Methods/Properties

.cctor()
.ctor(CoreWCF.IdentityModel.Tokens.SecurityToken,CoreWCF.IdentityModel.SecurityKeyIdentifierClause,System.Int32)
.ctor(CoreWCF.IdentityModel.Tokens.SecurityToken,CoreWCF.IdentityModel.SecurityKeyIdentifierClause,System.Int32,System.String)
.ctor(System.Int32,System.Int32,System.Int32,System.String,System.Int32,CoreWCF.IdentityModel.Tokens.SecurityToken,CoreWCF.IdentityModel.SecurityKeyIdentifierClause,System.String,System.String)
.ctor(System.Int32,System.Int32,System.Int32,System.String,System.Byte[],CoreWCF.IdentityModel.Tokens.SecurityToken,CoreWCF.IdentityModel.SecurityKeyIdentifierClause,System.String,System.String)
Id()
ValidFrom()
ValidTo()
KeyDerivationAlgorithm()
Generation()
Label()
Length()
Nonce()
Offset()
TokenToDerive()
TokenToDeriveIdentifier()
SecurityKeys()
GetKeyBytes()
GetNonce()
TryGetSecurityKeys(System.Collections.ObjectModel.ReadOnlyCollection`1<CoreWCF.IdentityModel.Tokens.SecurityKey>&)
ToString()
Initialize(System.String,System.Int32,System.Int32,System.Int32,System.String,System.Byte[],CoreWCF.IdentityModel.Tokens.SecurityToken,CoreWCF.IdentityModel.SecurityKeyIdentifierClause,System.String)
Initialize(System.String,System.Int32,System.Int32,System.Int32,System.String,System.Byte[],CoreWCF.IdentityModel.Tokens.SecurityToken,CoreWCF.IdentityModel.SecurityKeyIdentifierClause,System.String,System.Boolean)
InitializeDerivedKey(System.Int32)
InitializeDerivedKey(System.Collections.ObjectModel.ReadOnlyCollection`1<CoreWCF.IdentityModel.Tokens.SecurityKey>)
EnsureAcceptableOffset(System.Int32,System.Int32,System.Int32,System.Int32)