< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.EncryptedDataElement
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/EncryptedDataElement.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 68
Coverable lines: 68
Total lines: 199
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 30
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
CanReadFrom(...)0%220%
.ctor()100%110%
.ctor(...)100%110%
Decrypt(...)0%660%
Encrypt(...)100%110%
ExtractIVAndDecrypt(...)0%440%
GenerateIVAndEncrypt(...)100%110%
ReadExtensions(...)100%110%
ReadXml(...)0%440%
WriteXml(...)0%14140%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/EncryptedDataElement.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;
 6using CoreWCF.IdentityModel.Tokens;
 7using System.Security.Cryptography;
 8using System.Xml;
 9
 10namespace CoreWCF.IdentityModel
 11{
 12    /// <summary>
 13    /// This class implements a deserialization for: EncryptedData as defined in section 3.4 of http://www.w3.org/TR/200
 14    /// </summary>
 15    internal class EncryptedDataElement : EncryptedTypeElement
 16    {
 17        public static bool CanReadFrom( XmlReader reader )
 18        {
 019            return reader != null && reader.IsStartElement(
 020                XmlEncryptionConstants.Elements.EncryptedData,
 021                XmlEncryptionConstants.Namespace );
 22        }
 23
 24        public EncryptedDataElement()
 025            : this( null )
 26        {
 027        }
 28
 29        public EncryptedDataElement( SecurityTokenSerializer tokenSerializer )
 030            : base( tokenSerializer )
 31        {
 032            KeyIdentifier = new SecurityKeyIdentifier( new EmptySecurityKeyIdentifierClause() );
 033        }
 34
 35        /// <summary>
 36        /// Decrypts the data
 37        /// </summary>
 38        /// <param name="algorithm"></param>
 39        /// <returns></returns>
 40        /// <exception cref="ArgumentNullException">When algorithm is null</exception>
 41        /// <exception cref="InvalidOperationException">When no cipher data has been read</exception>
 42        public byte[] Decrypt( SymmetricAlgorithm algorithm )
 43        {
 044            if ( algorithm == null )
 45            {
 046                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( nameof(algorithm) );
 47            }
 48
 049            if ( CipherData == null || CipherData.CipherValue == null )
 50            {
 051                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new InvalidOperationException( SR.Format( SR.
 52            }
 53
 054            byte[] cipherText = CipherData.CipherValue;
 55
 056            return ExtractIVAndDecrypt( algorithm, cipherText, 0, cipherText.Length );
 57        }
 58
 59        public void Encrypt( SymmetricAlgorithm algorithm, byte[] buffer, int offset, int length )
 60        {
 61            byte[] iv;
 62            byte[] cipherText;
 063            GenerateIVAndEncrypt( algorithm, buffer, offset, length, out iv, out cipherText );
 064            CipherData.SetCipherValueFragments( iv, cipherText );
 065        }
 66
 67        static byte[] ExtractIVAndDecrypt( SymmetricAlgorithm algorithm, byte[] cipherText, int offset, int count )
 68        {
 069            byte[] iv = new byte[algorithm.BlockSize / 8];
 70
 71            //
 72            // Make sure cipherText has enough bytes after the offset, for Buffer.BlockCopy to copy.
 73            //
 074            if ( cipherText.Length - offset < iv.Length )
 75            {
 076                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new InvalidOperationException( SR.Format( SR.
 77            }
 78
 079            Buffer.BlockCopy( cipherText, offset, iv, 0, iv.Length );
 80
 081            algorithm.Padding = PaddingMode.ISO10126;
 082            algorithm.Mode = CipherMode.CBC;
 83
 084            ICryptoTransform decrTransform = null;
 085            byte[] plainText = null;
 86
 87            try
 88            {
 089                decrTransform = algorithm.CreateDecryptor( algorithm.Key, iv );
 090                plainText = decrTransform.TransformFinalBlock( cipherText, offset + iv.Length, count - iv.Length );
 091            }
 92            finally
 93            {
 094                if ( decrTransform != null )
 95                {
 096                    decrTransform.Dispose();
 97                }
 098            }
 99
 0100            return plainText;
 101        }
 102
 103        static void GenerateIVAndEncrypt( SymmetricAlgorithm algorithm, byte[] plainText, int offset, int length, out by
 104        {
 0105            RandomNumberGenerator random = CryptoHelper.RandomNumberGenerator;
 0106            int ivSize = algorithm.BlockSize / 8;
 0107            iv = new byte[ivSize];
 0108            random.GetBytes( iv );
 0109            algorithm.Padding = PaddingMode.PKCS7;
 0110            algorithm.Mode = CipherMode.CBC;
 0111            ICryptoTransform encrTransform = algorithm.CreateEncryptor( algorithm.Key, iv );
 0112            cipherText = encrTransform.TransformFinalBlock( plainText, offset, length );
 0113            encrTransform.Dispose();
 0114        }
 115
 116        public override void ReadExtensions( XmlDictionaryReader reader )
 117        {
 118            // nothing to do here
 0119        }
 120
 121        /// <summary>
 122        /// Reads an EncryptedData element
 123        /// </summary>
 124        /// <param name="reader"></param>
 125        /// <exception cref="ArgumentNullException">When reader is null</exception>
 126        /// <exception cref="ArgumentNullException">When securityTokenSerializer is null</exception>
 127        public override void ReadXml( XmlDictionaryReader reader )
 128        {
 0129            if ( reader == null )
 130            {
 0131                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "reader" );
 132            }
 133
 0134            reader.MoveToContent();
 0135            if ( !reader.IsStartElement( XmlEncryptionConstants.Elements.EncryptedData, XmlEncryptionConstants.Namespace
 136            {
 0137                throw DiagnosticUtility.ExceptionUtility.ThrowHelperXml( reader, SR.Format( SR.ID4193 ) );
 138            }
 139
 140            // <EncryptedData> extends <EncryptedType>
 141            // base will read the start element and the end element.
 0142            base.ReadXml( reader );
 143
 0144        }
 145
 146        /// <summary>
 147        /// Writes the EncryptedData element
 148        /// </summary>
 149        /// <param name="writer"></param>
 150        /// <param name="securityTokenSerializer"></param>
 151        /// <exception cref="ArgumentNullException">When securityTokenSerializer is null</exception>
 152        /// <exception cref="InvalidOperationException">When KeyIdentifier is null</exception>
 153        public virtual void WriteXml( XmlWriter writer, SecurityTokenSerializer securityTokenSerializer )
 154        {
 0155            if ( writer == null )
 156            {
 0157                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( nameof(writer));
 158            }
 159
 0160            if ( securityTokenSerializer == null )
 161            {
 0162                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( nameof(securityTokenSerializer));
 163            }
 164
 0165            if ( KeyIdentifier == null )
 166            {
 0167                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new InvalidOperationException( SR.Format( SR.
 168            }
 169
 170            // <EncryptedData>
 0171            writer.WriteStartElement( XmlEncryptionConstants.Prefix, XmlEncryptionConstants.Elements.EncryptedData, XmlE
 172
 0173            if ( !string.IsNullOrEmpty( Id ) )
 174            {
 0175                writer.WriteAttributeString( XmlEncryptionConstants.Attributes.Id, null, Id );
 176            }
 177
 0178            if ( !string.IsNullOrEmpty( Type ) )
 179            {
 0180                writer.WriteAttributeString( XmlEncryptionConstants.Attributes.Type, null, Type );
 181            }
 182
 0183            if ( EncryptionMethod != null )
 184            {
 0185                EncryptionMethod.WriteXml( writer );
 186            }
 187
 0188            if ( KeyIdentifier != null )
 189            {
 0190                securityTokenSerializer.WriteKeyIdentifier( XmlDictionaryWriter.CreateDictionaryWriter( writer ), KeyIde
 191            }
 192
 0193            CipherData.WriteXml( writer );
 194
 195            // <EncryptedData>
 0196            writer.WriteEndElement();
 0197        }
 198    }
 199}