Пример #1
0
        private static async Task <JObject> VerifyEncryptionSucceeded(TestDoc testDoc)
        {
            Stream encryptedStream = await EncryptionProcessor.EncryptAsync(
                testDoc.ToStream(),
                EncryptionProcessorTests.mockEncryptor.Object,
                EncryptionProcessorTests.encryptionOptions,
                new CosmosDiagnosticsContext(),
                CancellationToken.None);

            JObject encryptedDoc = EncryptionProcessor.BaseSerializer.FromStream <JObject>(encryptedStream);

            Assert.AreEqual(testDoc.Id, encryptedDoc.Property("id").Value.Value <string>());
            Assert.AreEqual(testDoc.PK, encryptedDoc.Property(nameof(TestDoc.PK)).Value.Value <string>());
            Assert.AreEqual(testDoc.NonSensitive, encryptedDoc.Property(nameof(TestDoc.NonSensitive)).Value.Value <string>());
            Assert.IsNull(encryptedDoc.Property(nameof(TestDoc.SensitiveStr)));
            Assert.IsNull(encryptedDoc.Property(nameof(TestDoc.SensitiveInt)));

            JProperty eiJProp = encryptedDoc.Property(Constants.EncryptedInfo);

            Assert.IsNotNull(eiJProp);
            Assert.IsNotNull(eiJProp.Value);
            Assert.AreEqual(JTokenType.Object, eiJProp.Value.Type);
            EncryptionProperties encryptionProperties = ((JObject)eiJProp.Value).ToObject <EncryptionProperties>();

            Assert.IsNotNull(encryptionProperties);
            Assert.AreEqual(EncryptionProcessorTests.dekId, encryptionProperties.DataEncryptionKeyId);
            Assert.AreEqual(2, encryptionProperties.EncryptionFormatVersion);
            Assert.IsNotNull(encryptionProperties.EncryptedData);

            return(encryptedDoc);
        }
        public async Task EncryptionUTCreateItem()
        {
            Container container = this.GetContainerWithMockSetup();
            MyItem    item      = await EncryptionUnitTests.CreateItemAsync(container, EncryptionUnitTests.DekId, MyItem.PathsToEncrypt);

            // Validate server state
            Assert.IsTrue(this.testHandler.Items.TryGetValue(item.Id, out JObject serverItem));
            Assert.IsNotNull(serverItem);
            Assert.AreEqual(item.Id, serverItem.Property(Constants.Properties.Id).Value.Value <string>());
            Assert.AreEqual(item.PK, serverItem.Property(nameof(MyItem.PK)).Value.Value <string>());
            Assert.IsNull(serverItem.Property(nameof(MyItem.EncStr1)));
            Assert.IsNull(serverItem.Property(nameof(MyItem.EncInt)));

            JProperty eiJProp = serverItem.Property(Constants.Properties.EncryptedInfo);

            Assert.IsNotNull(eiJProp);
            Assert.IsNotNull(eiJProp.Value);
            Assert.AreEqual(JTokenType.Object, eiJProp.Value.Type);
            EncryptionProperties encryptionPropertiesAtServer = ((JObject)eiJProp.Value).ToObject <EncryptionProperties>();

            Assert.IsNotNull(encryptionPropertiesAtServer);
            Assert.AreEqual(EncryptionUnitTests.DekId, encryptionPropertiesAtServer.DataEncryptionKeyId);
            Assert.AreEqual(2, encryptionPropertiesAtServer.EncryptionFormatVersion);
            Assert.IsNotNull(encryptionPropertiesAtServer.EncryptedData);

            JObject decryptedJObj = EncryptionUnitTests.ParseStream(new MemoryStream(EncryptionUnitTests.DecryptData(encryptionPropertiesAtServer.EncryptedData)));

            Assert.AreEqual(2, decryptedJObj.Properties().Count());
            Assert.AreEqual(item.EncStr1, decryptedJObj.Property(nameof(MyItem.EncStr1)).Value.Value <string>());
            Assert.AreEqual(item.EncInt, decryptedJObj.Property(nameof(MyItem.EncInt)).Value.Value <int>());
        }
Пример #3
0
        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);
        }
Пример #4
0
        internal XmlElement GetXml(XmlDocument document)
        {
            XmlElement encryptedDataElement = document.CreateElement("EncryptedData", XmlNameSpace.Url[NS.XmlEncNamespaceUrl]);

            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);
            }

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

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

            if (CipherData == null)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_MissingCipherData);
            }
            encryptedDataElement.AppendChild(CipherData.GetXml(document));

            if (EncryptionProperties.Count > 0)
            {
                XmlElement encryptionPropertiesElement = document.CreateElement("EncryptionProperties", XmlNameSpace.Url[NS.XmlEncNamespaceUrl]);
                for (int index = 0; index < EncryptionProperties.Count; index++)
                {
                    EncryptionProperty ep = EncryptionProperties.Item(index);
                    encryptionPropertiesElement.AppendChild(ep.GetXml(document));
                }
                encryptedDataElement.AppendChild(encryptionPropertiesElement);
            }
            return(encryptedDataElement);
        }
Пример #5
0
        public void Set(string arquivo, string senha)
        {
            var item = arquivo;


            if (!File.Exists(item))
            {
                return;
            }

            try
            {
                EncryptionProperties encryptionProperties = new EncryptionProperties();

                var senhabyte = Encoding.ASCII.GetBytes(senha);

                encryptionProperties.SetStandardEncryption(senhabyte, senhabyte, EncryptionConstants.ALLOW_PRINTING, EncryptionConstants.ENCRYPTION_AES_128);

                using (Stream input = new FileStream(item, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    var info = new FileInfo(item);

                    var nameFile = Path.GetFileName(arquivo);

                    var pdfencrypted = info.Directory + "\\encrypted.pdf";

                    using (Stream output = new FileStream(pdfencrypted, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        iText.Kernel.Pdf.PdfReader reader = new PdfReader(input);
                        PdfEncryptor.Encrypt(reader, output, encryptionProperties);
                    }

                    //  File.Delete(item);
                    // File.Move(pdfencrypted, item);
                }
            }
            catch (iText.Kernel.PdfException e)
            {
                throw new Exception(e.Message);
            }
        }
Пример #6
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", XmlNameSpace.Url[NS.XmlEncNamespaceUrl]);
            nsm.AddNamespace("ds", XmlNameSpace.Url[NS.XmlDsigNamespaceUrl]);

            Id        = ElementUtils.GetAttribute(value, "Id", NS.XmlEncNamespaceUrl);
            Type      = ElementUtils.GetAttribute(value, "Type", NS.XmlEncNamespaceUrl);
            MimeType  = ElementUtils.GetAttribute(value, "MimeType", NS.XmlEncNamespaceUrl);
            Encoding  = ElementUtils.GetAttribute(value, "Encoding", NS.XmlEncNamespaceUrl);
            Recipient = ElementUtils.GetAttribute(value, "Recipient", NS.XmlEncNamespaceUrl);

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

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

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

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

            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);

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

            if (encryptionPropertiesNode != null)
            {
                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);
                    }
                }
            }

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

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

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

            if (referenceListNode != null)
            {
                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);
                    }
                }
                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);
                    }
                }
            }

            _cachedXml = value;
        }
        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);
        }
Пример #8
0
        public override void LoadXml(XmlElement value)
        {
            if (value is 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;
        }
Пример #9
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;
                }
            }
        }
        internal static ConfigurationStoreData DeserializeConfigurationStoreData(JsonElement element)
        {
            Optional <ResourceIdentity> identity = default;

            Models.Sku sku = default;
            IDictionary <string, string> tags                 = default;
            AzureLocation                   location          = default;
            ResourceIdentifier              id                = default;
            string                          name              = default;
            ResourceType                    type              = default;
            Optional <ProvisioningState>    provisioningState = default;
            Optional <DateTimeOffset>       creationDate      = default;
            Optional <string>               endpoint          = default;
            Optional <EncryptionProperties> encryption        = default;
            Optional <IReadOnlyList <PrivateEndpointConnectionReference> > privateEndpointConnections = default;
            Optional <PublicNetworkAccess> publicNetworkAccess = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("identity"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    identity = JsonSerializer.Deserialize <ResourceIdentity>(property.Value.ToString());
                    continue;
                }
                if (property.NameEquals("sku"))
                {
                    sku = Models.Sku.DeserializeSku(property.Value);
                    continue;
                }
                if (property.NameEquals("tags"))
                {
                    Dictionary <string, string> dictionary = new Dictionary <string, string>();
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        dictionary.Add(property0.Name, property0.Value.GetString());
                    }
                    tags = dictionary;
                    continue;
                }
                if (property.NameEquals("location"))
                {
                    location = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("id"))
                {
                    id = new ResourceIdentifier(property.Value.GetString());
                    continue;
                }
                if (property.NameEquals("name"))
                {
                    name = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("type"))
                {
                    type = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("properties"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        if (property0.NameEquals("provisioningState"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            provisioningState = new ProvisioningState(property0.Value.GetString());
                            continue;
                        }
                        if (property0.NameEquals("creationDate"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            creationDate = property0.Value.GetDateTimeOffset("O");
                            continue;
                        }
                        if (property0.NameEquals("endpoint"))
                        {
                            endpoint = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("encryption"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            encryption = EncryptionProperties.DeserializeEncryptionProperties(property0.Value);
                            continue;
                        }
                        if (property0.NameEquals("privateEndpointConnections"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                privateEndpointConnections = null;
                                continue;
                            }
                            List <PrivateEndpointConnectionReference> array = new List <PrivateEndpointConnectionReference>();
                            foreach (var item in property0.Value.EnumerateArray())
                            {
                                array.Add(PrivateEndpointConnectionReference.DeserializePrivateEndpointConnectionReference(item));
                            }
                            privateEndpointConnections = array;
                            continue;
                        }
                        if (property0.NameEquals("publicNetworkAccess"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            publicNetworkAccess = new PublicNetworkAccess(property0.Value.GetString());
                            continue;
                        }
                    }
                    continue;
                }
            }
            return(new ConfigurationStoreData(id, name, type, tags, location, identity, sku, Optional.ToNullable(provisioningState), Optional.ToNullable(creationDate), endpoint.Value, encryption.Value, Optional.ToList(privateEndpointConnections), Optional.ToNullable(publicNetworkAccess)));
        }
Пример #11
0
        /// <summary>Loads the specified XML information into the <see langword="&lt;EncryptedKey&gt;" /> element in XML encryption.</summary>
        /// <param name="value">An <see cref="T:System.Xml.XmlElement" /> representing an XML element to use for the <see langword="&lt;EncryptedKey&gt;" /> element.</param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="value" /> parameter is <see langword="null" />.</exception>
        /// <exception cref="T:System.Security.Cryptography.CryptographicException">The <paramref name="value" /> parameter does not contain a <see cref="T:System.Security.Cryptography.Xml.CipherData" />  element.</exception>
        public override void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(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#");
            Id        = Exml.GetAttribute(value, "Id", "http://www.w3.org/2001/04/xmlenc#");
            Type      = Exml.GetAttribute(value, "Type", "http://www.w3.org/2001/04/xmlenc#");
            MimeType  = Exml.GetAttribute(value, "MimeType", "http://www.w3.org/2001/04/xmlenc#");
            Encoding  = Exml.GetAttribute(value, "Encoding", "http://www.w3.org/2001/04/xmlenc#");
            Recipient = Exml.GetAttribute(value, "Recipient", "http://www.w3.org/2001/04/xmlenc#");
            XmlNode xmlNode1 = value.SelectSingleNode("enc:EncryptionMethod", nsmgr);

            EncryptionMethod = new EncryptionMethod();
            if (xmlNode1 != null)
            {
                EncryptionMethod.LoadXml(xmlNode1 as XmlElement);
            }
            KeyInfo = new KeyInfo();
            XmlNode xmlNode2 = value.SelectSingleNode("ds:KeyInfo", nsmgr);

            if (xmlNode2 != null)
            {
                KeyInfo.LoadXml(xmlNode2 as XmlElement);
            }
            XmlNode xmlNode3 = value.SelectSingleNode("enc:CipherData", nsmgr);

            if (xmlNode3 == null)
            {
                throw new CryptographicException("Cryptography_Xml_MissingCipherData");
            }
            CipherData = new CipherData();
            CipherData.LoadXml(xmlNode3 as XmlElement);
            XmlNode xmlNode4 = value.SelectSingleNode("enc:EncryptionProperties", nsmgr);

            if (xmlNode4 != null)
            {
                XmlNodeList xmlNodeList = xmlNode4.SelectNodes("enc:EncryptionProperty", nsmgr);
                if (xmlNodeList != null)
                {
                    foreach (XmlNode xmlNode5 in xmlNodeList)
                    {
                        EncryptionProperty encryptionProperty = new EncryptionProperty();
                        encryptionProperty.LoadXml(xmlNode5 as XmlElement);
                        EncryptionProperties.Add(encryptionProperty);
                    }
                }
            }
            XmlNode xmlNode6 = value.SelectSingleNode("enc:CarriedKeyName", nsmgr);

            if (xmlNode6 != null)
            {
                CarriedKeyName = xmlNode6.InnerText;
            }
            XmlNode xmlNode7 = value.SelectSingleNode("enc:ReferenceList", nsmgr);

            if (xmlNode7 != null)
            {
                XmlNodeList xmlNodeList1 = xmlNode7.SelectNodes("enc:DataReference", nsmgr);
                if (xmlNodeList1 != null)
                {
                    foreach (XmlNode xmlNode5 in xmlNodeList1)
                    {
                        DataReference dataReference = new DataReference();
                        dataReference.LoadXml(xmlNode5 as XmlElement);
                        ReferenceList.Add((object)dataReference);
                    }
                }
                XmlNodeList xmlNodeList2 = xmlNode7.SelectNodes("enc:KeyReference", nsmgr);
                if (xmlNodeList2 != null)
                {
                    foreach (XmlNode xmlNode5 in xmlNodeList2)
                    {
                        KeyReference keyReference = new KeyReference();
                        keyReference.LoadXml(xmlNode5 as XmlElement);
                        ReferenceList.Add((object)keyReference);
                    }
                }
            }
            m_cachedXml = value;
        }
Пример #12
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;
                }
            }
        }
 public void AddProperty(EncryptionProperty ep)
 {
     EncryptionProperties.Add(ep);
 }
Пример #14
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;
        }