private void DecryptEncryptedGrants(XmlNodeList encryptedGrantList, IRelDecryptor decryptor)
        {
            XmlElement       encryptionMethod;
            XmlElement       keyInfo;
            XmlElement       cipherData;
            EncryptionMethod encryptionMethodObj;
            KeyInfo          keyInfoObj;
            CipherData       cipherDataObj;

            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
                    {
                        toDecrypt?.Close();
                        decryptedContent?.Close();
                        streamReader?.Close();
                    }
                }
            }
        }
Beispiel #2
0
        //<SNIPPET2>
        public static void CheckSignatureWithEncryptedGrant(string fileName, IRelDecryptor decryptor)
        {
            // Create a new XML document.
            XmlDocument         xmlDocument = new XmlDocument();
            XmlNamespaceManager nsManager   = new XmlNamespaceManager(xmlDocument.NameTable);

            // Format using white spaces.
            xmlDocument.PreserveWhitespace = true;

            // Load the passed XML file into the document.
            xmlDocument.Load(fileName);
            nsManager.AddNamespace("dsig", SignedXml.XmlDsigNamespaceUrl);

            // Find the "Signature" node and create a new XmlNodeList object.
            XmlNodeList nodeList = xmlDocument.SelectNodes("//dsig:Signature", nsManager);

            for (int i = 0, count = nodeList.Count; i < count; i++)
            {
                XmlDocument clone      = xmlDocument.Clone() as XmlDocument;
                XmlNodeList signatures = clone.SelectNodes("//dsig:Signature", nsManager);

                // Create a new SignedXml object and pass into it the XML document clone.
                SignedXml signedXml = new SignedXml(clone);

                // Load the signature node.
                signedXml.LoadXml((XmlElement)signatures[i]);

                // Set the context for license transform
                Transform trans = ((Reference)signedXml.SignedInfo.References[0]).TransformChain[0];

                if (trans is XmlLicenseTransform)
                {
                    // Decryptor is used to decrypt encryptedGrant elements.
                    if (decryptor != null)
                    {
                        (trans as XmlLicenseTransform).Decryptor = decryptor;
                    }
                }

                // Check the signature and display the result.
                bool result = signedXml.CheckSignature();

                if (result)
                {
                    Console.WriteLine("SUCCESS: CheckSignatureWithEncryptedGrant - issuer index #" +
                                      i.ToString());
                }
                else
                {
                    Console.WriteLine("FAILURE: CheckSignatureWithEncryptedGrant - issuer index #" +
                                      i.ToString());
                }
            }
        }
        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++;
            }
        }
        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;
            }
        }
 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++;
     }
 }