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 System.Security.Cryptography.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; }
// // 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 = (_document == null ? null : _document.BaseURI); TransformChain tc = cipherData.CipherReference.TransformChain; decInputStream = tc.TransformToOctetStream(_document, _xmlResolver, baseUri); } else if (cipherData.CipherReference.Uri[0] == '#') { string idref = Utils.ExtractIdFromLocalUri(cipherData.CipherReference.Uri); // Serialize inputStream = new MemoryStream(_encoding.GetBytes(GetIdElement(_document, idref).OuterXml)); string baseUri = (_document == null ? null : _document.BaseURI); TransformChain tc = cipherData.CipherReference.TransformChain; 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 (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(SR.Cryptography_Xml_MissingCipherData); }
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 System.Security.Cryptography.CryptographicException(SR.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); }
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 System.Security.Cryptography.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; } }