< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.X509DataSecurityKeyIdentifierClauseSerializer
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/X509DataSecurityKeyIdentifierClauseSerializer.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 77
Coverable lines: 77
Total lines: 272
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 46
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/X509DataSecurityKeyIdentifierClauseSerializer.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.Xml;
 7
 8namespace CoreWCF.IdentityModel.Tokens
 9{
 10    /// <summary>
 11    /// Implementation of SecurityKeyIdentifierClauseSerializer that handles X.509 Certificate
 12    /// reference types.
 13    /// </summary>
 14    public class X509DataSecurityKeyIdentifierClauseSerializer : SecurityKeyIdentifierClauseSerializer
 15    {
 16        /// <summary>
 17        /// Checks if the given reader is referring to a &lt;ds:X509Data> element.
 18        /// </summary>
 19        /// <param name=nameof(reader)>XmlReader positioned at the SecurityKeyIdentifierClause. </param>
 20        /// <returns>True if the XmlReader is referring to a &lt;ds:X509Data> element.</returns>
 21        /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception>
 22        public override bool CanReadKeyIdentifierClause(XmlReader reader)
 23        {
 024            if (reader == null)
 25            {
 026                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 27            }
 28
 029            return reader.IsStartElement(XmlSignatureConstants.Elements.X509Data, XmlSignatureConstants.Namespace);
 30        }
 31
 32        /// <summary>
 33        /// Checks if the given SecurityKeyIdentifierClause can be serialized. The
 34        /// supported SecurityKeyIdentifierClause are,
 35        /// 1. <see cref="System.IdentityModel.Tokens.X509IssuerSerialKeyIdentifierClause"/>
 36        /// 2. <see cref="System.IdentityModel.Tokens.X509RawDataKeyIdentifierClause"/>
 37        /// 3. <see cref="System.IdentityModel.Tokens.X509SubjectKeyIdentifierClause"/>
 38        /// </summary>
 39        /// <param name="securityKeyIdentifierClause">SecurityKeyIdentifierClause to be serialized.</param>
 40        /// <returns>True if the 'securityKeyIdentifierClause' is supported.</returns>
 41        /// <exception cref="ArgumentNullException">The parameter 'securityKeyIdentifierClause' is null.</exception>
 42        public override bool CanWriteKeyIdentifierClause(SecurityKeyIdentifierClause securityKeyIdentifierClause)
 43        {
 044            if (securityKeyIdentifierClause == null)
 45            {
 046                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityKeyIdentifierClause));
 47            }
 48
 049            return securityKeyIdentifierClause is X509IssuerSerialKeyIdentifierClause ||
 050                securityKeyIdentifierClause is X509RawDataKeyIdentifierClause ||
 051                securityKeyIdentifierClause is X509SubjectKeyIdentifierClause;
 52        }
 53
 54        /// <summary>
 55        /// Deserializes a SecurityKeyIdentifierClause from a given XmlReader.
 56        /// </summary>
 57        /// <param name="reader">XmlReader that references a SecurityKeyIdentifierClause.</param>
 58        /// <returns>Instance of SecurityKeyIdentifierClause</returns>
 59        /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception>
 60        /// <exception cref="InvalidOperationException">The XmlReader is not positioned at a valid X.509 SecurityTokenRe
 61        public override SecurityKeyIdentifierClause ReadKeyIdentifierClause(XmlReader reader)
 62        {
 063            if (!this.CanReadKeyIdentifierClause(reader))
 64            {
 065                throw new InvalidOperationException(
 066                    SR.Format(SR.ID3032, reader.LocalName, reader.NamespaceURI, XmlSignatureConstants.Elements.X509Data,
 67            }
 68
 069            XmlDictionaryReader dictionaryReader = XmlDictionaryReader.CreateDictionaryReader(reader);
 70
 71            // Read the starting <X509Data> element.
 072            dictionaryReader.ReadStartElement(XmlSignatureConstants.Elements.X509Data, XmlSignatureConstants.Namespace);
 73
 074            List<SecurityKeyIdentifierClause> clauses = new List<SecurityKeyIdentifierClause>();
 075            while (dictionaryReader.IsStartElement())
 76            {
 077                if (dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509IssuerSerial, XmlSignatureConstan
 78                {
 079                    clauses.Add(CreateIssuerSerialKeyIdentifierClause(dictionaryReader));
 80                }
 081                else if (dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509SKI, XmlSignatureConstants.N
 82                {
 083                    clauses.Add(CreateSubjectKeyIdentifierClause(dictionaryReader));
 84                }
 085                else if (dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509Certificate, XmlSignatureCon
 86                {
 087                    clauses.Add(CreateRawDataKeyIdentifierClause(dictionaryReader));
 88                }
 89                else
 90                {
 91                    // Skip the element since it is not one of <X509IssuerSerial>, <X509SKI> and <X509Certificate>
 092                    dictionaryReader.Skip();
 93                }
 94            }
 95
 96            // Read the ending </X509Data> element.
 097            dictionaryReader.ReadEndElement();
 98
 99            // Return the first identified clause or null if none is identified
 0100            return clauses.Count > 0 ? clauses[0] : null;
 101        }
 102
 103        /// <summary>
 104        /// Serialize a SecurityKeyIdentifierClause to the given XmlWriter.
 105        /// </summary>
 106        /// <param name="writer">XmlWriter to which the SecurityKeyIdentifierClause is serialized.</param>
 107        /// <param name="securityKeyIdentifierClause">SecurityKeyIdentifierClause to serialize.</param>
 108        /// <exception cref="ArgumentNullException">The input parameter 'reader' or 'securityKeyIdentifierClause' is nul
 109        /// <exception cref="ArgumentException">The parameter 'securityKeyIdentifierClause' is not a supported clause ty
 110        public override void WriteKeyIdentifierClause(XmlWriter writer, SecurityKeyIdentifierClause securityKeyIdentifie
 111        {
 0112            if (writer == null)
 113            {
 0114                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
 115            }
 116
 0117            if (securityKeyIdentifierClause == null)
 118            {
 0119                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityKeyIdentifierClause));
 120            }
 121
 0122            X509IssuerSerialKeyIdentifierClause issuerSerialClause = securityKeyIdentifierClause as X509IssuerSerialKeyI
 0123            if (issuerSerialClause != null)
 124            {
 0125                writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Data, XmlSigna
 0126                writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509IssuerSerial, 
 0127                writer.WriteElementString(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509IssuerName, X
 0128                writer.WriteElementString(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509SerialNumber,
 0129                writer.WriteEndElement();
 0130                writer.WriteEndElement();
 0131                return;
 132            }
 133
 0134            X509SubjectKeyIdentifierClause skiClause = securityKeyIdentifierClause as X509SubjectKeyIdentifierClause;
 0135            if (skiClause != null)
 136            {
 0137                writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Data, XmlSigna
 0138                writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509SKI, XmlSignat
 0139                byte[] ski = skiClause.GetX509SubjectKeyIdentifier();
 0140                writer.WriteBase64(ski, 0, ski.Length);
 0141                writer.WriteEndElement();
 0142                writer.WriteEndElement();
 0143                return;
 144            }
 145#if INCLUDE_CERT_CHAIN
 146            X509ChainRawDataKeyIdentifierClause x509ChainDataClause = securityKeyIdentifierClause as X509ChainRawDataKey
 147            if ( x509ChainDataClause != null )
 148            {
 149                writer.WriteStartElement( XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Data, XmlSign
 150                for( int i = 0; i < x509ChainDataClause.CertificateCount; i++ )
 151                {
 152                    writer.WriteStartElement( XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Certifica
 153                    byte[] rawData = x509ChainDataClause.GetX509RawData( i );
 154                    writer.WriteBase64( rawData, 0, rawData.Length );
 155                    writer.WriteEndElement();
 156                }
 157                writer.WriteEndElement();
 158                return;
 159            }
 160#endif
 161
 0162            X509RawDataKeyIdentifierClause rawDataClause = securityKeyIdentifierClause as X509RawDataKeyIdentifierClause
 0163            if (rawDataClause != null)
 164            {
 0165                writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Data, XmlSigna
 0166                writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Certificate, X
 0167                byte[] rawData = rawDataClause.GetX509RawData();
 0168                writer.WriteBase64(rawData, 0, rawData.Length);
 0169                writer.WriteEndElement();
 0170                writer.WriteEndElement();
 0171                return;
 172            }
 173
 0174            throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(securityKeyIdentifierClause), SR.Format(
 175        }
 176
 177        /// <summary>
 178        /// Parses the "X509IssuerSerial" element and generates a corresponding <see cref="X509IssuerSerialKeyIdentifier
 179        /// </summary>
 180        /// <param name="dictionaryReader">The <see cref="XmlDictionaryReader"/> currently positioning on the "X509Issue
 181        /// <returns>An instance of <see cref="X509IssuerSerialKeyIdentifierClause"/> created from the "X509IssuerSerial
 182        private static SecurityKeyIdentifierClause CreateIssuerSerialKeyIdentifierClause(XmlDictionaryReader dictionaryR
 183        {
 0184            dictionaryReader.ReadStartElement(XmlSignatureConstants.Elements.X509IssuerSerial, XmlSignatureConstants.Nam
 185
 0186            if (!dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509IssuerName, XmlSignatureConstants.Na
 187            {
 0188                throw new InvalidOperationException(SR.Format(SR.ID3032, dictionaryReader.LocalName, dictionaryReader.Na
 189            }
 190
 0191            string issuerName = dictionaryReader.ReadElementContentAsString(XmlSignatureConstants.Elements.X509IssuerNam
 192
 0193            if (!dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509SerialNumber, XmlSignatureConstants.
 194            {
 0195                throw new InvalidOperationException(SR.Format(SR.ID3032, dictionaryReader.LocalName, dictionaryReader.Na
 196            }
 197
 0198            string serialNumber = dictionaryReader.ReadElementContentAsString(XmlSignatureConstants.Elements.X509SerialN
 199
 0200            dictionaryReader.ReadEndElement(); // Reade the ending </X509IssuerSerial> element.
 201
 0202            return new X509IssuerSerialKeyIdentifierClause(issuerName, serialNumber);
 203        }
 204
 205        /// <summary>
 206        /// Parses the "X509SKI" element and generates a corresponding <see cref="SecurityKeyIdentifierClause"/> instanc
 207        /// </summary>
 208        /// <param name="dictionaryReader">The <see cref="XmlDictionaryReader"/> currently positioning on the "X509SKI" 
 209        /// <returns>An instance of <see cref="X509SubjectKeyIdentifierClause"/> created from the "X509SKI" element.</re
 210        private static SecurityKeyIdentifierClause CreateSubjectKeyIdentifierClause(XmlDictionaryReader dictionaryReader
 211        {
 0212            byte[] ski = dictionaryReader.ReadElementContentAsBase64();
 0213            if ((ski == null) || (ski.Length == 0))
 214            {
 0215                throw new InvalidOperationException(SR.Format(SR.ID4258, XmlSignatureConstants.Elements.X509SKI, XmlSign
 216            }
 217
 0218            return new X509SubjectKeyIdentifierClause(ski);
 219        }
 220
 221        /// <summary>
 222        /// Parses the "X509Certificate" element and generates a corresponding <see cref="X509RawDataKeyIdentifierClause
 223        /// </summary>
 224        /// <param name="dictionaryReader">The <see cref="XmlDictionaryReader"/> currently positioning on the "X509Certi
 225        /// <returns>An instance of <see cref="X509RawDataKeyIdentifierClause"/> created from the "X509Certificate" elem
 226        private static SecurityKeyIdentifierClause CreateRawDataKeyIdentifierClause(XmlDictionaryReader dictionaryReader
 227        {
 228#if INCLUDE_CERT_CHAIN
 229                List<byte[]> rawDatas = new List<byte[]>();
 230                while (dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509Certificate, XmlSignatureConst
 231                {
 232                    byte[] rawBuffer = dictionaryReader.ReadElementContentAsBase64();
 233                    if (rawBuffer == null || rawBuffer.Length == 0)
 234                    {
 235                        throw new InvalidOperationException(SR.Format(SR.ID4258, XmlSignatureConstants.Elements.X509Cert
 236                    }
 237
 238                    rawDatas.Add(rawBuffer);
 239                }
 240
 241                if (rawDatas.Count > 1)
 242                {
 243                    return new X509ChainRawDataKeyIdentifierClause(rawDatas);
 244                }
 245                else
 246                {
 247                    return new X509RawDataKeyIdentifierClause(rawDatas[0]);
 248                }
 249#else
 0250            byte[] rawData = null;
 0251            while (dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509Certificate, XmlSignatureConstants
 252            {
 0253                if (rawData == null)
 254                {
 0255                    rawData = dictionaryReader.ReadElementContentAsBase64();
 0256                    if ((rawData == null) || (rawData.Length == 0))
 257                    {
 0258                        throw new InvalidOperationException(SR.Format(SR.ID4258, XmlSignatureConstants.Elements.X509Cert
 259                    }
 260                }
 261                else
 262                {
 263                    // We do not support reading intermediary certs.
 0264                    dictionaryReader.Skip();
 265                }
 266            }
 267
 0268            return new X509RawDataKeyIdentifierClause(rawData);
 269#endif
 270        }
 271    }
 272}