Click here to Skip to main content
11,720,324 members (81,725 online)
Click here to Skip to main content
Articles » Languages » XML » Web Services » Downloads
Add your own
alternative version

Central Key Management

, 8 Mar 2006 46.6K 992 49
A central key manager for multiple web server clients in a web farm.
centralkeymanager_src.zip
Core
Cryptography
WrappedData.xsx
Properties
KeyManager
Properties
TestClient
Properties
Settings.settings
Web References
KeyManager
Reference.map
Service.disco
Service.wsdl
SessionKeyInfo.datasource
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Schema;

namespace Core.Cryptography
{
    /// <summary>
    /// WrappedData contains a piece of data that has been encrypted. The clear text data was
    /// encrypted using a temporary session key. This temporary session key is encrypyted with 
    /// the requestor's public key. This class provides a convienient way to pass encrypted
    /// information along with the data needed to decrypt it by the requestor.
    /// </summary>
    public class WrappedData : System.Xml.Serialization.IXmlSerializable
    {
        private string _certificate;
        private string _encryptedData;
        private string _iv;
        private string _encryptedKey;

        /// <summary>
        /// IV used in the symmetric key encryption
        /// </summary>
        public string IV
        {
            get { return _iv; }
            set { _iv = value; }
        }

        /// <summary>
        /// The encrypted temporary Symmetric key
        /// </summary>
        public string EncryptedKey
        {
            get { return _encryptedKey; }
            set { _encryptedKey = value; }
        }

        /// <summary>
        /// Certificate used to encrypt the data
        /// </summary>
        public string Certificate
        {
            get { return _certificate; }
            set { _certificate = value; }
        }

        /// <summary>
        /// The secret encrypted by the temporary Symmetric key
        /// </summary>
        public string EncryptedData
        {
            get { return _encryptedData; }
            set { _encryptedData = value; }
        }

        // IXmlSerializable is implemented since it allows for the Wrapped Data to be easily
        // serialzed to and from XML. This XML serialized format can be readily stored or
        // transferred.
        #region IXmlSerializable Members

        private void SchemaValidation(object source, ValidationEventArgs args)
        {
        }

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<?xml version='1.0' encoding='utf-8'?>");
            sb.Append("<xs:schema id='XMLSchema1' targetNamespace='http://www.oswsolutions.com/WrappedData.xsd' elementFormDefault='qualified' xmlns='http://www.oswsolutions.com/WrappedData.xsd' xmlns:mstns='http://www.oswsolutions.com/WrappedData.xsd' xmlns:xs='http://www.w3.org/2001/XMLSchema'>"); 
            sb.Append("  <xs:complexType name='WrappedData'>"); 
            sb.Append("    <xs:sequence>"); 
            sb.Append("      <xs:element name='Certificate' type='xs:string' />"); 
            sb.Append("      <xs:element name='EncryptedKey' type='xs:string' />"); 
            sb.Append("      <xs:element name='EncryptedData' type='xs:string' />"); 
            sb.Append("      <xs:element name='IV' type='xs:string' />"); 
            sb.Append("    </xs:sequence>"); 
            sb.Append("  </xs:complexType>"); 
            sb.Append("  <xs:element name='Data' type='WrappedData' />"); 
            sb.Append("</xs:schema>");

            System.IO.StringReader reader = new System.IO.StringReader(sb.ToString() );
            XmlSchema schema = XmlSchema.Read(reader, new ValidationEventHandler( this.SchemaValidation) );

            return schema;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            XPathDocument document = new XPathDocument(reader);
            XPathNavigator navigator = document.CreateNavigator();

            XPathNodeIterator iter = navigator.Select("/Data");

            if (iter.MoveNext() == true)
            {
                if (iter.Current.MoveToChild("Certificate", string.Empty) == true)
                    this.Certificate = iter.Current.Value;
                else
                    throw new ApplicationException("Invalid Wrapped Key: No certificate");

                if (iter.Current.MoveToNext("EncryptedKey", string.Empty))
                    this.EncryptedKey = iter.Current.Value;
                else
                    throw new ApplicationException("Invalid Wrapped Key: No EncryptedKey");

                if (iter.Current.MoveToNext("EncryptedData", string.Empty) == true)
                    this.EncryptedData = iter.Current.Value;
                else
                    throw new ApplicationException("Invalid Wrapped Key: No EncryptedData");

                if (iter.Current.MoveToNext("IV", string.Empty) == true)
                    this.IV = iter.Current.Value;
                else
                    throw new ApplicationException("Invalid Wrapped Key: No IV");

            }

        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            XmlDocument doc = new XmlDocument();
            XmlElement data = doc.CreateElement("Data");
            XmlElement cert = doc.CreateElement("Certificate");
            XmlElement encData = doc.CreateElement("EncryptedData");
            XmlElement encKey = doc.CreateElement("EncryptedKey");
            XmlElement iv = doc.CreateElement("IV");

            cert.InnerText = Certificate;
            encData.InnerText = EncryptedData;
            encKey.InnerText = EncryptedKey;
            iv.InnerText = IV;
            doc.AppendChild(data);
            data.AppendChild(cert);
            data.AppendChild(encKey);
            data.AppendChild(encData);
            data.AppendChild(iv);

            doc.WriteTo(writer);
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Brian Bayless
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150901.1 | Last Updated 8 Mar 2006
Article Copyright 2006 by Brian Bayless
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid