XmlDsigSep2000.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Security / XmlDsigSep2000.cs / 1 / XmlDsigSep2000.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

namespace System.ServiceModel.Security 
{
    using HexBinary = System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary; 
    using System; 
    using System.Collections;
    using System.Collections.Generic; 
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Text; 
    using System.Threading;
    using System.Xml; 
    using System.IdentityModel.Claims; 
    using System.IdentityModel.Policy;
    using System.IdentityModel.Tokens; 
    using System.Security.Cryptography;
    using System.Security.Cryptography.X509Certificates;
    using System.ServiceModel.Security.Tokens;
    using System.ServiceModel; 
    using System.ServiceModel.Channels;
    using System.ServiceModel.Security; 
    using System.Runtime.Serialization; 

    using KeyIdentifierEntry = WSSecurityTokenSerializer.KeyIdentifierEntry; 
    using KeyIdentifierClauseEntry = WSSecurityTokenSerializer.KeyIdentifierClauseEntry;
    using TokenEntry = WSSecurityTokenSerializer.TokenEntry;

    class XmlDsigSep2000 : WSSecurityTokenSerializer.SerializerEntries 
    {
        WSSecurityTokenSerializer tokenSerializer; 
 
        public XmlDsigSep2000(WSSecurityTokenSerializer tokenSerializer)
        { 
            this.tokenSerializer = tokenSerializer;
        }

        public override void PopulateKeyIdentifierEntries(IList keyIdentifierEntries) 
        {
            keyIdentifierEntries.Add(new KeyInfoEntry(this.tokenSerializer)); 
        } 

        public override void PopulateKeyIdentifierClauseEntries(IList keyIdentifierClauseEntries) 
        {
            keyIdentifierClauseEntries.Add(new KeyNameClauseEntry());
            keyIdentifierClauseEntries.Add(new KeyValueClauseEntry());
            keyIdentifierClauseEntries.Add(new X509CertificateClauseEntry()); 
        }
 
        internal class KeyInfoEntry : KeyIdentifierEntry 
        {
            WSSecurityTokenSerializer tokenSerializer; 

            public KeyInfoEntry(WSSecurityTokenSerializer tokenSerializer)
            {
                this.tokenSerializer = tokenSerializer; 
            }
 
            protected override XmlDictionaryString LocalName 
            {
                get 
                {
                    return XD.XmlSignatureDictionary.KeyInfo;
                }
            } 

            protected override XmlDictionaryString NamespaceUri 
            { 
                get
                { 
                    return XD.XmlSignatureDictionary.Namespace;
                }
            }
 
            public override SecurityKeyIdentifier ReadKeyIdentifierCore(XmlDictionaryReader reader)
            { 
                reader.ReadStartElement(LocalName, NamespaceUri); 
                SecurityKeyIdentifier keyIdentifier = new SecurityKeyIdentifier();
                while (reader.IsStartElement()) 
                {
                    SecurityKeyIdentifierClause clause = this.tokenSerializer.ReadKeyIdentifierClause(reader);
                    keyIdentifier.Add(clause);
                } 
                reader.ReadEndElement();
                return keyIdentifier; 
            } 

            public override bool SupportsCore(SecurityKeyIdentifier keyIdentifier) 
            {
                return true;
            }
 
            public override void WriteKeyIdentifierCore(XmlDictionaryWriter writer, SecurityKeyIdentifier keyIdentifier)
            { 
                writer.WriteStartElement(XD.XmlSignatureDictionary.Prefix.Value, LocalName, NamespaceUri); 
                bool clauseWritten = false;
                foreach (SecurityKeyIdentifierClause clause in keyIdentifier) 
                {
                    this.tokenSerializer.WriteKeyIdentifierClause(writer, clause);
                    clauseWritten = true;
                } 
                writer.WriteEndElement(); // KeyInfo
                if (!clauseWritten) 
                { 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.NoKeyInfoClausesToWrite)));
                } 
            }
        }

        // name 
        internal class KeyNameClauseEntry : KeyIdentifierClauseEntry
        { 
            protected override XmlDictionaryString LocalName 
            {
                get 
                {
                    return XD.XmlSignatureDictionary.KeyName;
                }
            } 

            protected override XmlDictionaryString NamespaceUri 
            { 
                get
                { 
                    return XD.XmlSignatureDictionary.Namespace;
                }
            }
 
            public override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlDictionaryReader reader)
            { 
                reader.ReadStartElement(XD.XmlSignatureDictionary.KeyName, NamespaceUri); 
                string name = reader.ReadString();
                reader.ReadEndElement(); 

                return new KeyNameIdentifierClause(name);
            }
 
            public override bool SupportsCore(SecurityKeyIdentifierClause keyIdentifierClause)
            { 
                return keyIdentifierClause is KeyNameIdentifierClause; 
            }
 
            public override void WriteKeyIdentifierClauseCore(XmlDictionaryWriter writer, SecurityKeyIdentifierClause keyIdentifierClause)
            {
                KeyNameIdentifierClause nameClause = keyIdentifierClause as KeyNameIdentifierClause;
 
                writer.WriteElementString(XD.XmlSignatureDictionary.Prefix.Value, XD.XmlSignatureDictionary.KeyName, NamespaceUri, nameClause.KeyName);
            } 
        } 
        // so far, we only support one type of KeyValue - RSAKeyValue
        //    
        //     
        //       xA7SEU+...
        //         AQAB
        //      
        //   
        internal class KeyValueClauseEntry : KeyIdentifierClauseEntry 
        { 
            protected override XmlDictionaryString LocalName
            { 
                get
                {
                    return XD.XmlSignatureDictionary.KeyValue;
                } 
            }
 
            protected override XmlDictionaryString NamespaceUri 
            {
                get 
                {
                    return XD.XmlSignatureDictionary.Namespace;
                }
            } 

 
            public override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlDictionaryReader reader) 
            {
                reader.ReadStartElement(XD.XmlSignatureDictionary.KeyValue, NamespaceUri); 
                reader.ReadStartElement(XD.XmlSignatureDictionary.RsaKeyValue, NamespaceUri);
                reader.ReadStartElement(XD.XmlSignatureDictionary.Modulus, NamespaceUri);

                byte[] modulus = Convert.FromBase64String(reader.ReadString()); 

                reader.ReadEndElement(); 
                reader.ReadStartElement(XD.XmlSignatureDictionary.Exponent, NamespaceUri); 
                byte[] exponent = Convert.FromBase64String(reader.ReadString());
                reader.ReadEndElement(); 
                reader.ReadEndElement();
                reader.ReadEndElement();

                RSA rsa = new RSACryptoServiceProvider(); 
                RSAParameters rsaParameters = new RSAParameters();
                rsaParameters.Modulus = modulus; 
                rsaParameters.Exponent = exponent; 
                rsa.ImportParameters(rsaParameters);
 
                return new RsaKeyIdentifierClause(rsa);
            }

            public override bool SupportsCore(SecurityKeyIdentifierClause keyIdentifierClause) 
            {
                return keyIdentifierClause is RsaKeyIdentifierClause; 
            } 

            public override void WriteKeyIdentifierClauseCore(XmlDictionaryWriter writer, SecurityKeyIdentifierClause keyIdentifierClause) 
            {
                RsaKeyIdentifierClause rsaClause = keyIdentifierClause as RsaKeyIdentifierClause;

                writer.WriteStartElement(XD.XmlSignatureDictionary.Prefix.Value, XD.XmlSignatureDictionary.KeyValue, NamespaceUri); 
                writer.WriteStartElement(XD.XmlSignatureDictionary.Prefix.Value, XD.XmlSignatureDictionary.RsaKeyValue, NamespaceUri);
                writer.WriteStartElement(XD.XmlSignatureDictionary.Prefix.Value, XD.XmlSignatureDictionary.Modulus, NamespaceUri); 
                rsaClause.WriteModulusAsBase64(writer); 
                writer.WriteEndElement();
                writer.WriteStartElement(XD.XmlSignatureDictionary.Prefix.Value, XD.XmlSignatureDictionary.Exponent, NamespaceUri); 
                rsaClause.WriteExponentAsBase64(writer);
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndElement(); 
            }
        } 
        // so far, we only support one type of X509Data directly under KeyInfo  - X509Certificate 
        //   
        //     ... 
        //   
            // only support 1 certificate right now
        internal class X509CertificateClauseEntry : KeyIdentifierClauseEntry
        { 
            protected override XmlDictionaryString LocalName
            { 
                get 
                {
                    return XD.XmlSignatureDictionary.X509Data; 
                }
            }

            protected override XmlDictionaryString NamespaceUri 
            {
                get 
                { 
                    return XD.XmlSignatureDictionary.Namespace;
                } 
            }

            public override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlDictionaryReader reader)
            { 
                X509Certificate2 certificate = null;
                reader.ReadStartElement(XD.XmlSignatureDictionary.X509Data, NamespaceUri); 
                while (reader.IsStartElement()) 
                {
                    if (certificate == null && reader.IsStartElement(XD.XmlSignatureDictionary.X509Certificate, NamespaceUri)) 
                    {
                        if (!SecurityUtils.TryCreateX509CertificateFromRawData(reader.ReadElementContentAsBase64(), out certificate))
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.InvalidX509RawData))); 
                        }
                    } 
                    else 
                    {
                        reader.Skip(); 
                    }
                }
                reader.ReadEndElement();
                return new X509RawDataKeyIdentifierClause(certificate); 
            }
 
            public override bool SupportsCore(SecurityKeyIdentifierClause keyIdentifierClause) 
            {
                return keyIdentifierClause is X509RawDataKeyIdentifierClause; 
            }

            public override void WriteKeyIdentifierClauseCore(XmlDictionaryWriter writer, SecurityKeyIdentifierClause keyIdentifierClause)
            { 
                X509RawDataKeyIdentifierClause x509Clause = keyIdentifierClause as X509RawDataKeyIdentifierClause;
 
                writer.WriteStartElement(XD.XmlSignatureDictionary.Prefix.Value, XD.XmlSignatureDictionary.X509Data, NamespaceUri); 

                writer.WriteStartElement(XD.XmlSignatureDictionary.Prefix.Value, XD.XmlSignatureDictionary.X509Certificate, NamespaceUri); 
                byte[] certBytes = x509Clause.GetX509RawData();
                writer.WriteBase64(certBytes, 0, certBytes.Length);
                writer.WriteEndElement();
 
                writer.WriteEndElement();
            } 
        } 

    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK