| File: System\Security\Cryptography\Xml\EncryptedData.cs | Web Access |
| Project: src\runtime\src\libraries\System.Security.Cryptography.Xml\src\System.Security.Cryptography.Xml.csproj (System.Security.Cryptography.Xml) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics.CodeAnalysis; using System.Xml; namespace System.Security.Cryptography.Xml { public sealed class EncryptedData : EncryptedType { [RequiresDynamicCode(CryptoHelpers.XsltRequiresDynamicCodeMessage)] [RequiresUnreferencedCode(CryptoHelpers.CreateFromNameUnreferencedCodeMessage)] public override void LoadXml(XmlElement value) { ArgumentNullException.ThrowIfNull(value); IncrementLoadXmlCurrentThreadDepth(); try { 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; } finally { DecrementLoadXmlCurrentThreadDepth(); } } public override XmlElement GetXml() { if (CacheValid) return _cachedXml; XmlDocument document = new XmlDocument(); document.PreserveWhitespace = true; return GetXml(document); } internal XmlElement GetXml(XmlDocument document) { // Create the EncryptedData element XmlElement encryptedDataElement = (XmlElement)document.CreateElement("EncryptedData", EncryptedXml.XmlEncNamespaceUrl); // Deal with attributes if (!string.IsNullOrEmpty(Id)) encryptedDataElement.SetAttribute("Id", Id); if (!string.IsNullOrEmpty(Type)) encryptedDataElement.SetAttribute("Type", Type); if (!string.IsNullOrEmpty(MimeType)) encryptedDataElement.SetAttribute("MimeType", MimeType); if (!string.IsNullOrEmpty(Encoding)) encryptedDataElement.SetAttribute("Encoding", Encoding); // EncryptionMethod if (EncryptionMethod != null) encryptedDataElement.AppendChild(EncryptionMethod.GetXml(document)); // KeyInfo if (KeyInfo.Count > 0) encryptedDataElement.AppendChild(KeyInfo.GetXml(document)); // CipherData is required. if (CipherData == null) throw new CryptographicException(SR.Cryptography_Xml_MissingCipherData); encryptedDataElement.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)); } encryptedDataElement.AppendChild(encryptionPropertiesElement); } return encryptedDataElement; } } }