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); this.Id = Utils.GetAttribute(value, "Id", EncryptedXml.XmlEncNamespaceUrl); this.Type = Utils.GetAttribute(value, "Type", EncryptedXml.XmlEncNamespaceUrl); this.MimeType = Utils.GetAttribute(value, "MimeType", EncryptedXml.XmlEncNamespaceUrl); this.Encoding = Utils.GetAttribute(value, "Encoding", EncryptedXml.XmlEncNamespaceUrl); this.Recipient = Utils.GetAttribute(value, "Recipient", EncryptedXml.XmlEncNamespaceUrl); XmlNode encryptionMethodNode = value.SelectSingleNode("enc:EncryptionMethod", nsm); // EncryptionMethod this.EncryptionMethod = new EncryptionMethod(); if (encryptionMethodNode != null) this.EncryptionMethod.LoadXml(encryptionMethodNode as XmlElement); // Key Info this.KeyInfo = new KeyInfo(); XmlNode keyInfoNode = value.SelectSingleNode("ds:KeyInfo", nsm); if (keyInfoNode != null) this.KeyInfo.LoadXml(keyInfoNode as XmlElement); // CipherData XmlNode cipherDataNode = value.SelectSingleNode("enc:CipherData", nsm); if (cipherDataNode == null) throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_MissingCipherData")); this.CipherData = new CipherData(); this.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); this.EncryptionProperties.Add(ep); } } } // CarriedKeyName XmlNode carriedKeyNameNode = value.SelectSingleNode("enc:CarriedKeyName", nsm); if (carriedKeyNameNode != null) { this.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); this.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); this.ReferenceList.Add(kr); } } } // Save away the cached value m_cachedXml = value; }
EncryptedData Encrypt (XmlElement target, SymmetricAlgorithm actualKey, string ekeyId, ReferenceList refList, SecurityKeyIdentifierClause encClause, EncryptedXml exml, XmlDocument doc) { SecurityAlgorithmSuite suite = security.Element.DefaultAlgorithmSuite; SecurityTokenSerializer serializer = security.TokenSerializer; byte [] encrypted = exml.EncryptData (target, actualKey, false); EncryptedData edata = new EncryptedData (); edata.Id = GenerateId (doc); edata.Type = EncryptedXml.XmlEncElementContentUrl; edata.EncryptionMethod = new EncryptionMethod (suite.DefaultEncryptionAlgorithm); // FIXME: here wsse:DigestMethod should be embedded // inside EncryptionMethod. Since it is not possible // with S.S.C.Xml.EncryptionMethod, we will have to // build our own XML encryption classes. edata.CipherData.CipherValue = encrypted; DataReference dr = new DataReference (); dr.Uri = "#" + edata.Id; refList.Add (dr); if (ShouldOutputEncryptedKey && !CounterParameters.RequireDerivedKeys) edata.KeyInfo = null; else { edata.KeyInfo = new KeyInfo (); edata.KeyInfo.AddClause (new SecurityTokenReferenceKeyInfo (encClause, serializer, doc)); } return edata; }
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; } }
public void AddReference (DataReference dataReference) { ReferenceList.Add(dataReference); }
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); 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(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); } } } // 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; }
public void RoundtripSample1 () { StringWriter sw = new StringWriter (); // Encryption { XmlDocument doc = new XmlDocument (); doc.PreserveWhitespace = true; doc.LoadXml ("<root> <child>sample</child> </root>"); XmlElement body = doc.DocumentElement; RijndaelManaged aes = new RijndaelManaged (); aes.Mode = CipherMode.CBC; aes.KeySize = 256; aes.IV = Convert.FromBase64String ("pBUM5P03rZ6AE4ZK5EyBrw=="); aes.Key = Convert.FromBase64String ("o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640="); aes.Padding = PaddingMode.Zeros; EncryptedXml exml = new EncryptedXml (); byte [] encrypted = exml.EncryptData (body, aes, false); EncryptedData edata = new EncryptedData (); edata.Type = EncryptedXml.XmlEncElementUrl; edata.EncryptionMethod = new EncryptionMethod (EncryptedXml.XmlEncAES256Url); EncryptedKey ekey = new EncryptedKey (); // omit key encryption, here for testing byte [] encKeyBytes = aes.Key; ekey.CipherData = new CipherData (encKeyBytes); ekey.EncryptionMethod = new EncryptionMethod (EncryptedXml.XmlEncRSA15Url); DataReference dr = new DataReference (); dr.Uri = "_0"; ekey.AddReference (dr); edata.KeyInfo.AddClause (new KeyInfoEncryptedKey (ekey)); edata.KeyInfo = new KeyInfo (); ekey.KeyInfo.AddClause (new RSAKeyValue (RSA.Create ())); edata.CipherData.CipherValue = encrypted; EncryptedXml.ReplaceElement (doc.DocumentElement, edata, false); doc.Save (new XmlTextWriter (sw)); } // Decryption { RijndaelManaged aes = new RijndaelManaged (); aes.Mode = CipherMode.CBC; aes.KeySize = 256; aes.Key = Convert.FromBase64String ( "o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640="); aes.Padding = PaddingMode.Zeros; XmlDocument doc = new XmlDocument (); doc.PreserveWhitespace = true; doc.LoadXml (sw.ToString ()); EncryptedXml encxml = new EncryptedXml (doc); EncryptedData edata = new EncryptedData (); edata.LoadXml (doc.DocumentElement); encxml.ReplaceData (doc.DocumentElement, encxml.DecryptData (edata, aes)); } }
public void AddReference(DataReference dataReference) { ReferenceList.Add(dataReference); }
public override void LoadXml(XmlElement value) { if (value == null) { throw new ArgumentNullException("value"); } XmlNamespaceManager nsmgr = new XmlNamespaceManager(value.OwnerDocument.NameTable); nsmgr.AddNamespace("enc", "http://www.w3.org/2001/04/xmlenc#"); nsmgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#"); this.Id = System.Security.Cryptography.Xml.Utils.GetAttribute(value, "Id", "http://www.w3.org/2001/04/xmlenc#"); this.Type = System.Security.Cryptography.Xml.Utils.GetAttribute(value, "Type", "http://www.w3.org/2001/04/xmlenc#"); this.MimeType = System.Security.Cryptography.Xml.Utils.GetAttribute(value, "MimeType", "http://www.w3.org/2001/04/xmlenc#"); this.Encoding = System.Security.Cryptography.Xml.Utils.GetAttribute(value, "Encoding", "http://www.w3.org/2001/04/xmlenc#"); this.Recipient = System.Security.Cryptography.Xml.Utils.GetAttribute(value, "Recipient", "http://www.w3.org/2001/04/xmlenc#"); XmlNode node = value.SelectSingleNode("enc:EncryptionMethod", nsmgr); this.EncryptionMethod = new EncryptionMethod(); if (node != null) { this.EncryptionMethod.LoadXml(node as XmlElement); } base.KeyInfo = new KeyInfo(); XmlNode node2 = value.SelectSingleNode("ds:KeyInfo", nsmgr); if (node2 != null) { base.KeyInfo.LoadXml(node2 as XmlElement); } XmlNode node3 = value.SelectSingleNode("enc:CipherData", nsmgr); if (node3 == null) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_MissingCipherData")); } this.CipherData = new CipherData(); this.CipherData.LoadXml(node3 as XmlElement); XmlNode node4 = value.SelectSingleNode("enc:EncryptionProperties", nsmgr); if (node4 != null) { XmlNodeList list = node4.SelectNodes("enc:EncryptionProperty", nsmgr); if (list != null) { foreach (XmlNode node5 in list) { EncryptionProperty property = new EncryptionProperty(); property.LoadXml(node5 as XmlElement); this.EncryptionProperties.Add(property); } } } XmlNode node6 = value.SelectSingleNode("enc:CarriedKeyName", nsmgr); if (node6 != null) { this.CarriedKeyName = node6.InnerText; } XmlNode node7 = value.SelectSingleNode("enc:ReferenceList", nsmgr); if (node7 != null) { XmlNodeList list2 = node7.SelectNodes("enc:DataReference", nsmgr); if (list2 != null) { foreach (XmlNode node8 in list2) { DataReference reference = new DataReference(); reference.LoadXml(node8 as XmlElement); this.ReferenceList.Add(reference); } } XmlNodeList list3 = node7.SelectNodes("enc:KeyReference", nsmgr); if (list3 != null) { foreach (XmlNode node9 in list3) { KeyReference reference2 = new KeyReference(); reference2.LoadXml(node9 as XmlElement); this.ReferenceList.Add(reference2); } } } base.m_cachedXml = value; }
public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, string EncryptionElementID, RSA Alg, string KeyName) { // Check the arguments. if (Doc == null) throw new ArgumentNullException("Doc"); if (ElementToEncrypt == null) throw new ArgumentNullException("ElementToEncrypt"); if (EncryptionElementID == null) throw new ArgumentNullException("EncryptionElementID"); if (Alg == null) throw new ArgumentNullException("Alg"); if (KeyName == null) throw new ArgumentNullException("KeyName"); //////////////////////////////////////////////// // Find the specified element in the XmlDocument // object and create a new XmlElemnt object. //////////////////////////////////////////////// XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement; // Throw an XmlException if the element was not found. if (elementToEncrypt == null) { throw new XmlException("The specified element was not found"); } RijndaelManaged sessionKey = null; try { ////////////////////////////////////////////////// // Create a new instance of the EncryptedXml class // and use it to encrypt the XmlElement with the // a new random symmetric key. ////////////////////////////////////////////////// // Create a 256 bit Rijndael key. sessionKey = new RijndaelManaged(); sessionKey.KeySize = 256; EncryptedXml eXml = new EncryptedXml(); byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, sessionKey, false); //////////////////////////////////////////////// // Construct an EncryptedData object and populate // it with the desired encryption information. //////////////////////////////////////////////// EncryptedData edElement = new EncryptedData(); edElement.Type = EncryptedXml.XmlEncElementUrl; edElement.Id = EncryptionElementID; // Create an EncryptionMethod element so that the // receiver knows which algorithm to use for decryption. edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url); // Encrypt the session key and add it to an EncryptedKey element. EncryptedKey ek = new EncryptedKey(); byte[] encryptedKey = EncryptedXml.EncryptKey(sessionKey.Key, Alg, false); ek.CipherData = new CipherData(encryptedKey); ek.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url); // Create a new DataReference element // for the KeyInfo element. This optional // element specifies which EncryptedData // uses this key. An XML document can have // multiple EncryptedData elements that use // different keys. DataReference dRef = new DataReference(); // Specify the EncryptedData URI. dRef.Uri = "#" + EncryptionElementID; // Add the DataReference to the EncryptedKey. ek.AddReference(dRef); // Add the encrypted key to the // EncryptedData object. edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek)); // Set the KeyInfo element to specify the // name of the RSA key. // Create a new KeyInfo element. edElement.KeyInfo = new KeyInfo(); // Create a new KeyInfoName element. KeyInfoName kin = new KeyInfoName(); // Specify a name for the key. kin.Value = KeyName; // Add the KeyInfoName element to the // EncryptedKey object. ek.KeyInfo.AddClause(kin); // Add the encrypted element data to the // EncryptedData object. edElement.CipherData.CipherValue = encryptedElement; //////////////////////////////////////////////////// // Replace the element from the original XmlDocument // object with the EncryptedData element. //////////////////////////////////////////////////// EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false); } catch (Exception e) { // re-throw the exception. throw e; } finally { if (sessionKey != null) { sessionKey.Clear(); } } }
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; } } }