protected EncryptedType ()
		{
			cipherData = new CipherData ();
			encoding = null;
			encryptionMethod = null;
			encryptionProperties = new EncryptionPropertyCollection ();
			id = null;
			keyInfo = new KeyInfo ();
			mimeType = null;
			type = null;
		}
Пример #2
0
        //
        // private methods
        //

        private byte[] GetCipherValue (CipherData cipherData) {
            if (cipherData == null)
                throw new ArgumentNullException("cipherData");

            WebResponse response = null;
            Stream inputStream = null;

            if (cipherData.CipherValue != null) {
                return cipherData.CipherValue;
            } else if (cipherData.CipherReference != null) {
                if (cipherData.CipherReference.CipherValue != null)
                    return cipherData.CipherReference.CipherValue;
                Stream decInputStream = null;
                // See if the CipherReference is a local URI
                if (cipherData.CipherReference.Uri.Length == 0) {
                    // self referenced Uri
                    string baseUri = (m_document == null ? null : m_document.BaseURI);
                    TransformChain tc = cipherData.CipherReference.TransformChain;
                    decInputStream = tc.TransformToOctetStream(m_document, m_xmlResolver, baseUri);
                } else if (cipherData.CipherReference.Uri[0] == '#') {
                    string idref = Utils.ExtractIdFromLocalUri(cipherData.CipherReference.Uri); 
                    // Serialize 
                    inputStream = new MemoryStream(m_encoding.GetBytes(GetIdElement(m_document, idref).OuterXml));
                    string baseUri = (m_document == null ? null : m_document.BaseURI);
                    TransformChain tc = cipherData.CipherReference.TransformChain;
                    decInputStream = tc.TransformToOctetStream(inputStream, m_xmlResolver, baseUri);
                } else {
                    DownloadCipherValue(cipherData, out inputStream, out decInputStream, out response);
                }
                // read the output stream into a memory stream
                byte[] cipherValue = null;
                using (MemoryStream ms = new MemoryStream()) {
                    Utils.Pump(decInputStream, ms);
                    cipherValue = ms.ToArray();
                    // Close the stream and return
                    if (response != null)
                        response.Close();
                    if (inputStream != null)
                        inputStream.Close();
                    decInputStream.Close();
                }

                // cache the cipher value for Perf reasons in case we call this routine twice
                cipherData.CipherReference.CipherValue = cipherValue;
                return cipherValue;
            }

            // Throw a CryptographicException if we were unable to retrieve the cipher data.
            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_MissingCipherData"));
        }
 private byte[] GetCipherValue(CipherData cipherData)
 {
     return (byte[])GetCipherValueMethod.Invoke(this, new object[] { cipherData });
 }
Пример #4
0
		protected EncryptedType ()
		{
			cipherData = new CipherData ();
			encryptionProperties = new EncryptionPropertyCollection ();
			keyInfo = new KeyInfo ();
		}
Пример #5
0
        private void DecryptEncryptedGrants(XmlNodeList encryptedGrantList, IRelDecryptor decryptor) {
            XmlElement       encryptionMethod    = null;
            XmlElement       keyInfo             = null;
            XmlElement       cipherData          = null;
            EncryptionMethod encryptionMethodObj = null;
            KeyInfo          keyInfoObj          = null;
            CipherData       cipherDataObj       = null;

            for (int i = 0, count = encryptedGrantList.Count; i < count; i++) {
                encryptionMethod = encryptedGrantList[i].SelectSingleNode("//r:encryptedGrant/enc:EncryptionMethod", namespaceManager) as XmlElement;
                keyInfo          = encryptedGrantList[i].SelectSingleNode("//r:encryptedGrant/dsig:KeyInfo", namespaceManager) as XmlElement;
                cipherData       = encryptedGrantList[i].SelectSingleNode("//r:encryptedGrant/enc:CipherData", namespaceManager) as XmlElement;
                if ((encryptionMethod != null) &&
                    (keyInfo != null) &&
                    (cipherData != null)) {
                    encryptionMethodObj = new EncryptionMethod();
                    keyInfoObj          = new KeyInfo();
                    cipherDataObj       = new CipherData();

                    encryptionMethodObj.LoadXml(encryptionMethod);
                    keyInfoObj.LoadXml(keyInfo);
                    cipherDataObj.LoadXml(cipherData);

                    MemoryStream toDecrypt        = null;
                    Stream       decryptedContent = null;
                    StreamReader streamReader     = null;

                    try {
                        toDecrypt = new MemoryStream(cipherDataObj.CipherValue);
                        decryptedContent = relDecryptor.Decrypt(encryptionMethodObj,
                                                                keyInfoObj, toDecrypt);

                        if ((decryptedContent == null) || (decryptedContent.Length == 0))
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_XrmlUnableToDecryptGrant"));

                        streamReader = new StreamReader(decryptedContent);
                        string clearContent = streamReader.ReadToEnd();

                        encryptedGrantList[i].ParentNode.InnerXml = clearContent;
                    }
                    finally {
                        if (toDecrypt != null)
                            toDecrypt.Close();

                        if (decryptedContent != null)
                            decryptedContent.Close();

                        if (streamReader != null)
                            streamReader.Close();
                    }

                    encryptionMethodObj = null;
                    keyInfoObj          = null;
                    cipherDataObj       = null;
                }

                encryptionMethod = null;
                keyInfo          = null;
                cipherData       = null;
            }
        }
Пример #6
0
		public override void LoadXml (XmlElement value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");

			if ((value.LocalName != XmlEncryption.ElementNames.EncryptedData) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
				throw new CryptographicException ("Malformed EncryptedData element.");
			else {
				EncryptionMethod = null;
				EncryptionMethod = null;
				EncryptionProperties.Clear ();
				Id = null;
				Type = null;
				MimeType = null;
				Encoding = null;

				foreach (XmlNode n in value.ChildNodes) {
					if (n is XmlWhitespace)
						continue;

					switch (n.LocalName) {
					case XmlEncryption.ElementNames.EncryptionMethod:
						EncryptionMethod = new EncryptionMethod ();
						EncryptionMethod.LoadXml ((XmlElement) n);
						break;
					case XmlSignature.ElementNames.KeyInfo:
						KeyInfo = new KeyInfo ();
						KeyInfo.LoadXml ((XmlElement) n);
						break;
					case XmlEncryption.ElementNames.CipherData:
						CipherData = new CipherData ();
						CipherData.LoadXml ((XmlElement) n);
						break;
					case XmlEncryption.ElementNames.EncryptionProperties:
						foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
							EncryptionProperties.Add (new EncryptionProperty (element));
						break;
					}
				}

				if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
					Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
				if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
					Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
				if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
					MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
				if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
					Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
			}
		}
 private byte[] GetCipherValue(CipherData cipherData)
 {
     if (cipherData == null)
     {
         throw new ArgumentNullException("cipherData");
     }
     WebResponse response = null;
     Stream input = null;
     if (cipherData.CipherValue != null)
     {
         return cipherData.CipherValue;
     }
     if (cipherData.CipherReference == null)
     {
         throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_MissingCipherData"));
     }
     if (cipherData.CipherReference.CipherValue != null)
     {
         return cipherData.CipherReference.CipherValue;
     }
     Stream decInputStream = null;
     if (cipherData.CipherReference.Uri.Length == 0)
     {
         string baseUri = (this.m_document == null) ? null : this.m_document.BaseURI;
         decInputStream = cipherData.CipherReference.TransformChain.TransformToOctetStream(this.m_document, this.m_xmlResolver, baseUri);
     }
     else if (cipherData.CipherReference.Uri[0] == '#')
     {
         string idValue = System.Security.Cryptography.Xml.Utils.ExtractIdFromLocalUri(cipherData.CipherReference.Uri);
         input = new MemoryStream(this.m_encoding.GetBytes(this.GetIdElement(this.m_document, idValue).OuterXml));
         string str3 = (this.m_document == null) ? null : this.m_document.BaseURI;
         decInputStream = cipherData.CipherReference.TransformChain.TransformToOctetStream(input, this.m_xmlResolver, str3);
     }
     else
     {
         this.DownloadCipherValue(cipherData, out input, out decInputStream, out response);
     }
     byte[] buffer = null;
     using (MemoryStream stream3 = new MemoryStream())
     {
         System.Security.Cryptography.Xml.Utils.Pump(decInputStream, stream3);
         buffer = stream3.ToArray();
         if (response != null)
         {
             response.Close();
         }
         if (input != null)
         {
             input.Close();
         }
         decInputStream.Close();
     }
     cipherData.CipherReference.CipherValue = buffer;
     return buffer;
 }
        private void DecryptEncryptedGrants(XmlNodeList encryptedGrantList, IRelDecryptor decryptor)
        {
            XmlElement       encryptionMethod    = null;
            XmlElement       keyInfo             = null;
            XmlElement       cipherData          = null;
            EncryptionMethod encryptionMethodObj = null;
            KeyInfo          keyInfoObj          = null;
            CipherData       cipherDataObj       = null;

            for (int i = 0, count = encryptedGrantList.Count; i < count; i++)
            {
                encryptionMethod = encryptedGrantList[i].SelectSingleNode("//r:encryptedGrant/enc:EncryptionMethod", _namespaceManager) as XmlElement;
                keyInfo          = encryptedGrantList[i].SelectSingleNode("//r:encryptedGrant/dsig:KeyInfo", _namespaceManager) as XmlElement;
                cipherData       = encryptedGrantList[i].SelectSingleNode("//r:encryptedGrant/enc:CipherData", _namespaceManager) as XmlElement;
                if ((encryptionMethod != null) &&
                    (keyInfo != null) &&
                    (cipherData != null))
                {
                    encryptionMethodObj = new EncryptionMethod();
                    keyInfoObj          = new KeyInfo();
                    cipherDataObj       = new CipherData();

                    encryptionMethodObj.LoadXml(encryptionMethod);
                    keyInfoObj.LoadXml(keyInfo);
                    cipherDataObj.LoadXml(cipherData);

                    MemoryStream toDecrypt        = null;
                    Stream       decryptedContent = null;
                    StreamReader streamReader     = null;

                    try
                    {
                        toDecrypt        = new MemoryStream(cipherDataObj.CipherValue);
                        decryptedContent = _relDecryptor.Decrypt(encryptionMethodObj,
                                                                 keyInfoObj, toDecrypt);

                        if ((decryptedContent == null) || (decryptedContent.Length == 0))
                        {
                            throw new CryptographicException(SR.Cryptography_Xml_XrmlUnableToDecryptGrant);
                        }

                        streamReader = new StreamReader(decryptedContent);
                        string clearContent = streamReader.ReadToEnd();

                        encryptedGrantList[i].ParentNode.InnerXml = clearContent;
                    }
                    finally
                    {
                        if (toDecrypt != null)
                        {
                            toDecrypt.Close();
                        }

                        if (decryptedContent != null)
                        {
                            decryptedContent.Close();
                        }

                        if (streamReader != null)
                        {
                            streamReader.Close();
                        }
                    }

                    encryptionMethodObj = null;
                    keyInfoObj          = null;
                    cipherDataObj       = null;
                }

                encryptionMethod = null;
                keyInfo          = null;
                cipherData       = null;
            }
        }
Пример #9
0
        //
        // private methods
        //

        private byte[] GetCipherValue(CipherData cipherData)
        {
            if (cipherData == null)
            {
                throw new ArgumentNullException(nameof(cipherData));
            }

            Stream inputStream = null;

            if (cipherData.CipherValue != null)
            {
                return(cipherData.CipherValue);
            }
            else if (cipherData.CipherReference != null)
            {
                if (cipherData.CipherReference.CipherValue != null)
                {
                    return(cipherData.CipherReference.CipherValue);
                }
                Stream decInputStream = null;
                if (cipherData.CipherReference.Uri == null)
                {
                    throw new CryptographicException(SR.Cryptography_Xml_UriNotSupported);
                }
                // See if the CipherReference is a local URI
                if (cipherData.CipherReference.Uri.Length == 0)
                {
                    // self referenced Uri
                    string         baseUri = (_document == null ? null : _document.BaseURI);
                    TransformChain tc      = cipherData.CipherReference.TransformChain;
                    if (tc == null)
                    {
                        throw new CryptographicException(SR.Cryptography_Xml_UriNotSupported);
                    }
                    decInputStream = tc.TransformToOctetStream(_document, _xmlResolver, baseUri);
                }
                else if (cipherData.CipherReference.Uri[0] == '#')
                {
                    string idref = Utils.ExtractIdFromLocalUri(cipherData.CipherReference.Uri);
                    // Serialize
                    XmlElement idElem = GetIdElement(_document, idref);
                    if (idElem == null || idElem.OuterXml == null)
                    {
                        throw new CryptographicException(SR.Cryptography_Xml_UriNotSupported);
                    }
                    inputStream = new MemoryStream(_encoding.GetBytes(idElem.OuterXml));
                    string         baseUri = (_document == null ? null : _document.BaseURI);
                    TransformChain tc      = cipherData.CipherReference.TransformChain;
                    if (tc == null)
                    {
                        throw new CryptographicException(SR.Cryptography_Xml_UriNotSupported);
                    }
                    decInputStream = tc.TransformToOctetStream(inputStream, _xmlResolver, baseUri);
                }
                else
                {
                    throw new CryptographicException(SR.Cryptography_Xml_UriNotResolved, cipherData.CipherReference.Uri);
                }
                // read the output stream into a memory stream
                byte[] cipherValue = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    Utils.Pump(decInputStream, ms);
                    cipherValue = ms.ToArray();
                    // Close the stream and return
                    if (inputStream != null)
                    {
                        inputStream.Close();
                    }
                    decInputStream.Close();
                }

                // cache the cipher value for Perf reasons in case we call this routine twice
                cipherData.CipherReference.CipherValue = cipherValue;
                return(cipherValue);
            }

            // Throw a CryptographicException if we were unable to retrieve the cipher data.
            throw new CryptographicException(SR.Cryptography_Xml_MissingCipherData);
        }
 private void DownloadCipherValue(CipherData cipherData, out Stream inputStream, out Stream decInputStream, out WebResponse response)
 {
     SecurityManager.GetStandardSandbox(this.m_evidence).PermitOnly();
     WebRequest request = WebRequest.Create(cipherData.CipherReference.Uri);
     if (request == null)
     {
         throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_UriNotResolved"), cipherData.CipherReference.Uri);
     }
     response = request.GetResponse();
     if (response == null)
     {
         throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_UriNotResolved"), cipherData.CipherReference.Uri);
     }
     inputStream = response.GetResponseStream();
     if (inputStream == null)
     {
         throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_UriNotResolved"), cipherData.CipherReference.Uri);
     }
     decInputStream = cipherData.CipherReference.TransformChain.TransformToOctetStream(inputStream, this.m_xmlResolver, cipherData.CipherReference.Uri);
 }
Пример #11
0
        public override void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable);

            nsm.AddNamespace("enc", EncryptedXml.XmlEncNamespaceUrl);
            nsm.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl);

            Id        = Utils.GetAttribute(value, "Id", EncryptedXml.XmlEncNamespaceUrl);
            Type      = Utils.GetAttribute(value, "Type", EncryptedXml.XmlEncNamespaceUrl);
            MimeType  = Utils.GetAttribute(value, "MimeType", EncryptedXml.XmlEncNamespaceUrl);
            Encoding  = Utils.GetAttribute(value, "Encoding", EncryptedXml.XmlEncNamespaceUrl);
            Recipient = Utils.GetAttribute(value, "Recipient", EncryptedXml.XmlEncNamespaceUrl);

            XmlNode encryptionMethodNode = value.SelectSingleNode("enc:EncryptionMethod", nsm);

            // EncryptionMethod
            EncryptionMethod = new EncryptionMethod();
            if (encryptionMethodNode != null)
            {
                EncryptionMethod.LoadXml(encryptionMethodNode as XmlElement);
            }

            // Key Info
            KeyInfo = new KeyInfo();
            XmlNode keyInfoNode = value.SelectSingleNode("ds:KeyInfo", nsm);

            if (keyInfoNode != null)
            {
                KeyInfo.LoadXml(keyInfoNode as XmlElement);
            }

            // CipherData
            XmlNode cipherDataNode = value.SelectSingleNode("enc:CipherData", nsm);

            if (cipherDataNode == null)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_MissingCipherData"));
            }

            CipherData = new CipherData();
            CipherData.LoadXml(cipherDataNode as XmlElement);

            // EncryptionProperties
            XmlNode encryptionPropertiesNode = value.SelectSingleNode("enc:EncryptionProperties", nsm);

            if (encryptionPropertiesNode != null)
            {
                // Select the EncryptionProperty elements inside the EncryptionProperties element
                XmlNodeList encryptionPropertyNodes = encryptionPropertiesNode.SelectNodes("enc:EncryptionProperty", nsm);
                if (encryptionPropertyNodes != null)
                {
                    foreach (XmlNode node in encryptionPropertyNodes)
                    {
                        EncryptionProperty ep = new EncryptionProperty();
                        ep.LoadXml(node as XmlElement);
                        EncryptionProperties.Add(ep);
                    }
                }
            }

            // CarriedKeyName
            XmlNode carriedKeyNameNode = value.SelectSingleNode("enc:CarriedKeyName", nsm);

            if (carriedKeyNameNode != null)
            {
                CarriedKeyName = carriedKeyNameNode.InnerText;
            }

            // ReferenceList
            XmlNode referenceListNode = value.SelectSingleNode("enc:ReferenceList", nsm);

            if (referenceListNode != null)
            {
                // Select the DataReference elements inside the ReferenceList element
                XmlNodeList dataReferenceNodes = referenceListNode.SelectNodes("enc:DataReference", nsm);
                if (dataReferenceNodes != null)
                {
                    foreach (XmlNode node in dataReferenceNodes)
                    {
                        DataReference dr = new DataReference();
                        dr.LoadXml(node as XmlElement);
                        ReferenceList.Add(dr);
                    }
                }
                // Select the KeyReference elements inside the ReferenceList element
                XmlNodeList keyReferenceNodes = referenceListNode.SelectNodes("enc:KeyReference", nsm);
                if (keyReferenceNodes != null)
                {
                    foreach (XmlNode node in keyReferenceNodes)
                    {
                        KeyReference kr = new KeyReference();
                        kr.LoadXml(node as XmlElement);
                        ReferenceList.Add(kr);
                    }
                }
            }

            // Save away the cached value
            _cachedXml = value;
        }
Пример #12
0
        internal XmlElement GetXml(XmlDocument document)
        {
            // Create the EncryptedKey element
            XmlElement encryptedKeyElement = (XmlElement)document.CreateElement("EncryptedKey", EncryptedXml.XmlEncNamespaceUrl);

            // Deal with attributes
            if (!string.IsNullOrEmpty(Id))
            {
                encryptedKeyElement.SetAttribute("Id", Id);
            }
            if (!string.IsNullOrEmpty(Type))
            {
                encryptedKeyElement.SetAttribute("Type", Type);
            }
            if (!string.IsNullOrEmpty(MimeType))
            {
                encryptedKeyElement.SetAttribute("MimeType", MimeType);
            }
            if (!string.IsNullOrEmpty(Encoding))
            {
                encryptedKeyElement.SetAttribute("Encoding", Encoding);
            }
            if (!string.IsNullOrEmpty(Recipient))
            {
                encryptedKeyElement.SetAttribute("Recipient", Recipient);
            }

            // EncryptionMethod
            if (EncryptionMethod != null)
            {
                encryptedKeyElement.AppendChild(EncryptionMethod.GetXml(document));
            }

            // KeyInfo
            if (KeyInfo.Count > 0)
            {
                encryptedKeyElement.AppendChild(KeyInfo.GetXml(document));
            }

            // CipherData
            if (CipherData == null)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_MissingCipherData"));
            }
            encryptedKeyElement.AppendChild(CipherData.GetXml(document));

            // EncryptionProperties
            if (EncryptionProperties.Count > 0)
            {
                XmlElement encryptionPropertiesElement = document.CreateElement("EncryptionProperties", EncryptedXml.XmlEncNamespaceUrl);
                for (int index = 0; index < EncryptionProperties.Count; index++)
                {
                    EncryptionProperty ep = EncryptionProperties.Item(index);
                    encryptionPropertiesElement.AppendChild(ep.GetXml(document));
                }
                encryptedKeyElement.AppendChild(encryptionPropertiesElement);
            }

            // ReferenceList
            if (ReferenceList.Count > 0)
            {
                XmlElement referenceListElement = document.CreateElement("ReferenceList", EncryptedXml.XmlEncNamespaceUrl);
                for (int index = 0; index < ReferenceList.Count; index++)
                {
                    referenceListElement.AppendChild(ReferenceList[index].GetXml(document));
                }
                encryptedKeyElement.AppendChild(referenceListElement);
            }

            // CarriedKeyName
            if (CarriedKeyName != null)
            {
                XmlElement carriedKeyNameElement = (XmlElement)document.CreateElement("CarriedKeyName", EncryptedXml.XmlEncNamespaceUrl);
                XmlText    carriedKeyNameText    = document.CreateTextNode(CarriedKeyName);
                carriedKeyNameElement.AppendChild(carriedKeyNameText);
                encryptedKeyElement.AppendChild(carriedKeyNameElement);
            }

            return(encryptedKeyElement);
        }
Пример #13
0
        public override void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable);

            nsm.AddNamespace("enc", EncryptedXml.XmlEncNamespaceUrl);
            nsm.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl);

            Id       = Utils.GetAttribute(value, "Id", EncryptedXml.XmlEncNamespaceUrl);
            Type     = Utils.GetAttribute(value, "Type", EncryptedXml.XmlEncNamespaceUrl);
            MimeType = Utils.GetAttribute(value, "MimeType", EncryptedXml.XmlEncNamespaceUrl);
            Encoding = Utils.GetAttribute(value, "Encoding", EncryptedXml.XmlEncNamespaceUrl);

            XmlNode encryptionMethodNode = value.SelectSingleNode("enc:EncryptionMethod", nsm);

            // EncryptionMethod
            EncryptionMethod = new EncryptionMethod();
            if (encryptionMethodNode != null)
            {
                EncryptionMethod.LoadXml(encryptionMethodNode as XmlElement);
            }

            // Key Info
            KeyInfo = new KeyInfo();
            XmlNode keyInfoNode = value.SelectSingleNode("ds:KeyInfo", nsm);

            if (keyInfoNode != null)
            {
                KeyInfo.LoadXml(keyInfoNode as XmlElement);
            }

            // CipherData
            XmlNode cipherDataNode = value.SelectSingleNode("enc:CipherData", nsm);

            if (cipherDataNode == null)
            {
                throw new CryptographicException(SR.Cryptography_Xml_MissingCipherData);
            }

            CipherData = new CipherData();
            CipherData.LoadXml(cipherDataNode as XmlElement);

            // EncryptionProperties
            XmlNode encryptionPropertiesNode = value.SelectSingleNode("enc:EncryptionProperties", nsm);

            if (encryptionPropertiesNode != null)
            {
                // Select the EncryptionProperty elements inside the EncryptionProperties element
                XmlNodeList encryptionPropertyNodes = encryptionPropertiesNode.SelectNodes("enc:EncryptionProperty", nsm);
                if (encryptionPropertyNodes != null)
                {
                    foreach (XmlNode node in encryptionPropertyNodes)
                    {
                        EncryptionProperty ep = new EncryptionProperty();
                        ep.LoadXml(node as XmlElement);
                        EncryptionProperties.Add(ep);
                    }
                }
            }

            // Save away the cached value
            _cachedXml = value;
        }
Пример #14
0
 private void DownloadCipherValue (CipherData cipherData, out Stream inputStream, out Stream decInputStream, out WebResponse response) {
     // maybe a network stream, make sure we allow just what is needed!!
     PermissionSet ps = SecurityManager.GetStandardSandbox(m_evidence);
     ps.PermitOnly();
     WebRequest request = WebRequest.Create(cipherData.CipherReference.Uri);
     if (request == null) 
         throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_UriNotResolved"), cipherData.CipherReference.Uri);
     response = request.GetResponse();
     if (response == null) 
         throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_UriNotResolved"), cipherData.CipherReference.Uri);
     inputStream = response.GetResponseStream();
     if (inputStream == null) 
         throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_UriNotResolved"), cipherData.CipherReference.Uri);
     TransformChain tc = cipherData.CipherReference.TransformChain;
     decInputStream = tc.TransformToOctetStream(inputStream, m_xmlResolver, cipherData.CipherReference.Uri);
 }
 private void DecryptEncryptedGrants(XmlNodeList encryptedGrantList, IRelDecryptor decryptor)
 {
     XmlElement element = null;
     XmlElement element2 = null;
     XmlElement element3 = null;
     EncryptionMethod encryptionMethod = null;
     KeyInfo keyInfo = null;
     CipherData data = null;
     int num = 0;
     int count = encryptedGrantList.Count;
     while (num < count)
     {
         element = encryptedGrantList[num].SelectSingleNode("//r:encryptedGrant/enc:EncryptionMethod", this.namespaceManager) as XmlElement;
         element2 = encryptedGrantList[num].SelectSingleNode("//r:encryptedGrant/dsig:KeyInfo", this.namespaceManager) as XmlElement;
         element3 = encryptedGrantList[num].SelectSingleNode("//r:encryptedGrant/enc:CipherData", this.namespaceManager) as XmlElement;
         if (((element != null) && (element2 != null)) && (element3 != null))
         {
             encryptionMethod = new EncryptionMethod();
             keyInfo = new KeyInfo();
             data = new CipherData();
             encryptionMethod.LoadXml(element);
             keyInfo.LoadXml(element2);
             data.LoadXml(element3);
             MemoryStream toDecrypt = null;
             Stream stream = null;
             StreamReader reader = null;
             try
             {
                 toDecrypt = new MemoryStream(data.CipherValue);
                 stream = this.relDecryptor.Decrypt(encryptionMethod, keyInfo, toDecrypt);
                 if ((stream == null) || (stream.Length == 0L))
                 {
                     throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_XrmlUnableToDecryptGrant"));
                 }
                 reader = new StreamReader(stream);
                 string str = reader.ReadToEnd();
                 encryptedGrantList[num].ParentNode.InnerXml = str;
             }
             finally
             {
                 if (toDecrypt != null)
                 {
                     toDecrypt.Close();
                 }
                 if (stream != null)
                 {
                     stream.Close();
                 }
                 if (reader != null)
                 {
                     reader.Close();
                 }
             }
             encryptionMethod = null;
             keyInfo = null;
             data = null;
         }
         element = null;
         element2 = null;
         element3 = null;
         num++;
     }
 }
Пример #16
0
		public override void LoadXml (XmlElement value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");

			if ((value.LocalName != XmlEncryption.ElementNames.EncryptedKey) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
				throw new CryptographicException ("Malformed EncryptedKey element.");
			else {
				EncryptionMethod = null;
				EncryptionMethod = null;
				EncryptionProperties.Clear ();
				ReferenceList.Clear ();
				CarriedKeyName = null;
				Id = null;
				Type = null;
				MimeType = null;
				Encoding = null;
				Recipient = null;

				foreach (XmlNode n in value.ChildNodes) {
					if (n is XmlWhitespace)
						continue;

					switch (n.LocalName) {
					case XmlEncryption.ElementNames.EncryptionMethod:
						EncryptionMethod = new EncryptionMethod ();
						EncryptionMethod.LoadXml ((XmlElement) n);
						break;
					case XmlSignature.ElementNames.KeyInfo:
						KeyInfo = new KeyInfo ();
						KeyInfo.LoadXml ((XmlElement) n);
						break;
					case XmlEncryption.ElementNames.CipherData:
						CipherData = new CipherData ();
						CipherData.LoadXml ((XmlElement) n);
						break;
					case XmlEncryption.ElementNames.EncryptionProperties:
						foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
							EncryptionProperties.Add (new EncryptionProperty (element));
						break;
					case XmlEncryption.ElementNames.ReferenceList:
						foreach (XmlNode r in ((XmlElement) n).ChildNodes) {
							if (r is XmlWhitespace) 
								continue;

							switch (r.LocalName) {
							case XmlEncryption.ElementNames.DataReference:
								DataReference dr = new DataReference ();
								dr.LoadXml ((XmlElement) r);
								AddReference (dr);
								break;
							case XmlEncryption.ElementNames.KeyReference:
								KeyReference kr = new KeyReference ();
								kr.LoadXml ((XmlElement) r);
								AddReference (kr);
								break;
							}
						}
						break;
					case XmlEncryption.ElementNames.CarriedKeyName:
						CarriedKeyName = ((XmlElement) n).InnerText;
						break;
					}
				}

				if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
					Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
				if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
					Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
				if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
					MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
				if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
					Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
				if (value.HasAttribute (XmlEncryption.AttributeNames.Recipient))
					Encoding = value.Attributes [XmlEncryption.AttributeNames.Recipient].Value;
			}
		}
Пример #17
0
 protected EncryptedType()
 {
     cipherData           = new CipherData();
     encryptionProperties = new EncryptionPropertyCollection();
     keyInfo = new KeyInfo();
 }