public void TestNewEmptyKeyNode () 
		{
			KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod ();
#if NET_1_0
			AssertEquals ("Empty", "<RetrievalElement xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (uri1.GetXml ().OuterXml));
#else
			AssertEquals ("Empty", "<RetrievalMethod xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (uri1.GetXml ().OuterXml));
#endif
		}
		public void InvalidKeyNode2 () 
		{
			string bad = "<Test></Test>";
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml (bad);

			KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod ();
			// no exception is thrown
			uri1.LoadXml (doc.DocumentElement);
			AssertCrypto.AssertXmlEquals ("invalid", "<RetrievalMethod xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (uri1.GetXml ().OuterXml));
		}
		public void TestNewKeyNode () 
		{
			string uri = "http://www.go-mono.com/";
			KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod ();
			uri1.Uri = uri;
			XmlElement xel = uri1.GetXml ();

			KeyInfoRetrievalMethod uri2 = new KeyInfoRetrievalMethod (uri1.Uri);
			uri2.LoadXml (xel);

			Assert.AreEqual ((uri1.GetXml ().OuterXml), (uri2.GetXml ().OuterXml), "uri1==uri2");
			Assert.AreEqual (uri, uri1.Uri, "uri==Uri");
		}
		public void TestImportKeyNode () 
		{
			string value = "<RetrievalMethod URI=\"http://www.go-mono.com/\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />";
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml (value);

			KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod ();
			uri1.LoadXml (doc.DocumentElement);

			// verify that proper XML is generated (equals to original)
			string s = (uri1.GetXml ().OuterXml);
			Assert.AreEqual (value, s, "Xml");

			// verify that property is parsed correctly
			Assert.AreEqual ("http://www.go-mono.com/", uri1.Uri, "Uri");
		}
		public void InvalidKeyNode2 () 
		{
			string bad = "<Test></Test>";
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml (bad);

			KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod ();
			// no exception is thrown
			uri1.LoadXml (doc.DocumentElement);
#if NET_2_0
			AssertCrypto.AssertXmlEquals ("invalid", "<RetrievalMethod xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (uri1.GetXml ().OuterXml));
#elif NET_1_1
			// note that URI="" is present (unlike a empty Uri)
			AssertEquals("invalid", "<RetrievalMethod URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (uri1.GetXml ().OuterXml));
#else
			// Fx 1.0 misnamed the tag name
			AssertEquals("invalid", "<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (uri1.GetXml ().OuterXml));
#endif
		}
		public void RetrievalMethod () 
		{
			KeyInfoRetrievalMethod retrieval = new KeyInfoRetrievalMethod ();
			retrieval.Uri = "http://www.go-mono.org/";
			info.AddClause (retrieval);
#if NET_1_0
			AssertEquals ("RetrievalMethod", "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RetrievalElement URI=\"http://www.go-mono.org/\" /></KeyInfo>", (info.GetXml ().OuterXml));
#else
			AssertEquals ("RetrievalMethod", "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RetrievalMethod URI=\"http://www.go-mono.org/\" /></KeyInfo>", (info.GetXml ().OuterXml));
#endif
			AssertEquals ("RetrievalMethod count", 1, info.Count);
		}
		public void Complex () 
		{
			KeyInfoName name = new KeyInfoName ();
			name.Value = "Mono::";
			info.AddClause (name);

			DSA keyDSA = DSA.Create ();
			keyDSA.FromXmlString (xmlDSA);
			DSAKeyValue dsa = new DSAKeyValue (keyDSA);
			info.AddClause (dsa);

			RSA keyRSA = RSA.Create ();
			keyRSA.FromXmlString (xmlRSA);
			RSAKeyValue rsa = new RSAKeyValue (keyRSA);
			info.AddClause (rsa);

			KeyInfoRetrievalMethod retrieval = new KeyInfoRetrievalMethod ();
			retrieval.Uri = "http://www.go-mono.org/";
			info.AddClause (retrieval);

			X509Certificate x509 = new X509Certificate (cert);
			KeyInfoX509Data x509data = new KeyInfoX509Data (x509);
			info.AddClause (x509data);

			string s = "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><KeyName>Mono::</KeyName><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue><P>rjxsMU368YOCTQejWkiuO9e/vUVwkLtq1jKiU3TtJ53hBJqjFRuTa228vZe+BH2su9RPn/vYFWfQDv6zgBYe3eNdu4Afw+Ny0FatX6dl3E77Ra6Tsd3MmLXBiGSQ1mMNd5G2XQGpbt9zsGlUaexXekeMLxIufgfZLwYp67M+2WM=</P><Q>tf0K9rMyvUrU4cIkwbCrDRhQAJk=</Q><G>S8Z+1pGCed00w6DtVcqZLKjfqlCJ7JsugEFIgSy/Vxtu9YGCMclV4ijGEbPo/jU8YOSMuD7E9M7UaopMRcmKQjoKZzoJjkgVFP48Ohxl1f08lERnButsxanx3+OstFwUGQ8XNaGg3KrIoZt1FUnfxN3RHHTvVhjzNSHxMGULGaU=</G><Y>LnrxxRGLYeV2XLtK3SYz8RQHlHFZYrtznDZyMotuRfO5uC5YODhSFyLXvb1qB3WeGtF4h3Eo4KzHgMgfN2ZMlffxFRhJgTtH3ctbL8lfQoDkjeiPPnYGhspdJxr0tyZmiy0gkjJG3vwHYrLnvZWx9Wm/unqiOlGBPNuxJ+hOeP8=</Y><J>9RhE5TycDtdEIXxS3HfxFyXYgpy81zY5lVjwD6E9JP37MWEi80BlX6ab1YPm6xYSEoqReMPP9RgGiW6DuACpgI7+8vgCr4i/7VhzModJAA56PwvTu6UMt9xxKU/fT672v8ucREkMWoc7lEey</J><Seed>HxW3N4RHWVgqDQKuGg7iJTUTiCs=</Seed><PgenCounter>Asw=</PgenCounter></DSAKeyValue></KeyValue>";
			s += "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>9DC4XNdQJwMRnz5pP2a6U51MHCODRilaIoVXqUPhCUb0lJdGroeqVYT84ZyIVrcarzD7Tqs3aEOIa3rKox0N1bxQpZPqayVQeLAkjLLtzJW/ScRJx3uEDJdgT1JnM1FH0GZTinmEdCUXdLc7+Y/c/qqIkTfbwHbRZjW0bBJyExM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue>";
#if NET_1_0
			s += "<RetrievalElement URI=\"http://www.go-mono.org/\" />";
#else
			s += "<RetrievalMethod URI=\"http://www.go-mono.org/\" />";
#endif
			s += "<X509Data xmlns=\"http://www.w3.org/2000/09/xmldsig#\">";
			s += "<X509Certificate>MIICHTCCAYYCARQwDQYJKoZIhvcNAQEEBQAwWDELMAkGA1UEBhMCQ0ExHzAdBgNVBAMTFktleXdpdG5lc3MgQ2FuYWRhIEluYy4xKDAmBgorBgEEASoCCwIBExhrZXl3aXRuZXNzQGtleXdpdG5lc3MuY2EwHhcNOTYwNTA3MDAwMDAwWhcNOTkwNTA3MDAwMDAwWjBYMQswCQYDVQQGEwJDQTEfMB0GA1UEAxMWS2V5d2l0bmVzcyBDYW5hZGEgSW5jLjEoMCYGCisGAQQBKgILAgETGGtleXdpdG5lc3NAa2V5d2l0bmVzcy5jYTCBnTANBgkqhkiG9w0BAQEFAAOBiwAwgYcCgYEAzSP6KuHtmPTp0JM+13qAAkzMwQKvXLYff/pXQm8w0SDFtSEHQCyphsLzZISuPYUu7YW9VLAYKO9q+BvnCxYfkyVPx/iOw7nKmIQOVdAv73h3xXIoX2C/GSvRcqK32D/glzRaAb0EnMh4Rc2TjRXydhARq7hbLp5S3YE+nGTIKZMCAQMwDQYJKoZIhvcNAQEEBQADgYEAMho1ur9DJ9a01Lh25eObTWzAhsl3NbprFi0TRkqwMlOhW1rpmeIMhogXTg3+gqxOR+/7/zms7jXI+lI3CkmtWa3iiqkcxl8f+G9zfs2gMegMvvVN2bKrihK2MHhoEXwN8UlNo/2y6f8d8JH6VIX/M5Dowb+km6RiRr1hElmYQYk=</X509Certificate></X509Data></KeyInfo>";
			AssertCrypto.AssertXmlEquals ("Complex", s, (info.GetXml ().OuterXml));
			AssertEquals ("RetrievalMethod count", 5, info.Count);
		}
		public void InvalidKeyNode1 () 
		{
			KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod ();
			uri1.LoadXml (null);
		}
		public void TestNewEmptyKeyNode () 
		{
			KeyInfoRetrievalMethod uri1 = new KeyInfoRetrievalMethod ();
			Assert.AreEqual ("<RetrievalMethod xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (uri1.GetXml ().OuterXml), "Empty");
		}
Esempio n. 10
0
        public virtual SymmetricAlgorithm GetDecryptionKey(EncryptedData encryptedData, string symmetricAlgorithmUri)
        {
            if (encryptedData == null)
            {
                throw new ArgumentNullException("encryptedData");
            }
            if (encryptedData.KeyInfo == null)
            {
                return(null);
            }
            IEnumerator  enumerator   = encryptedData.KeyInfo.GetEnumerator();
            EncryptedKey encryptedKey = null;

            while (enumerator.MoveNext())
            {
                KeyInfoName current = enumerator.Current as KeyInfoName;
                if (current != null)
                {
                    string str = current.Value;
                    if (((SymmetricAlgorithm)this.m_keyNameMapping[str]) != null)
                    {
                        return((SymmetricAlgorithm)this.m_keyNameMapping[str]);
                    }
                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(this.m_document.NameTable);
                    nsmgr.AddNamespace("enc", "http://www.w3.org/2001/04/xmlenc#");
                    XmlNodeList list = this.m_document.SelectNodes("//enc:EncryptedKey", nsmgr);
                    if (list != null)
                    {
                        foreach (XmlNode node in list)
                        {
                            XmlElement   element = node as XmlElement;
                            EncryptedKey key3    = new EncryptedKey();
                            key3.LoadXml(element);
                            if ((key3.CarriedKeyName == str) && (key3.Recipient == this.Recipient))
                            {
                                encryptedKey = key3;
                                break;
                            }
                        }
                    }
                    break;
                }
                KeyInfoRetrievalMethod method = enumerator.Current as KeyInfoRetrievalMethod;
                if (method != null)
                {
                    string idValue = System.Security.Cryptography.Xml.Utils.ExtractIdFromLocalUri(method.Uri);
                    encryptedKey = new EncryptedKey();
                    encryptedKey.LoadXml(this.GetIdElement(this.m_document, idValue));
                    break;
                }
                KeyInfoEncryptedKey key = enumerator.Current as KeyInfoEncryptedKey;
                if (key != null)
                {
                    encryptedKey = key.EncryptedKey;
                    break;
                }
            }
            if (encryptedKey == null)
            {
                return(null);
            }
            if (symmetricAlgorithmUri == null)
            {
                if (encryptedData.EncryptionMethod == null)
                {
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_MissingAlgorithm"));
                }
                symmetricAlgorithmUri = encryptedData.EncryptionMethod.KeyAlgorithm;
            }
            byte[] buffer = this.DecryptEncryptedKey(encryptedKey);
            if (buffer == null)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_MissingDecryptionKey"));
            }
            SymmetricAlgorithm algorithm = (SymmetricAlgorithm)CryptoConfig.CreateFromName(symmetricAlgorithmUri);

            algorithm.Key = buffer;
            return(algorithm);
        }
Esempio n. 11
0
 public virtual byte[] DecryptEncryptedKey(EncryptedKey encryptedKey)
 {
     if (encryptedKey == null)
     {
         throw new ArgumentNullException("encryptedKey");
     }
     if (encryptedKey.KeyInfo != null)
     {
         IEnumerator  enumerator = encryptedKey.KeyInfo.GetEnumerator();
         EncryptedKey key2       = null;
         bool         useOAEP    = false;
         while (enumerator.MoveNext())
         {
             KeyInfoName current = enumerator.Current as KeyInfoName;
             if (current != null)
             {
                 string str  = current.Value;
                 object obj2 = this.m_keyNameMapping[str];
                 if (obj2 != null)
                 {
                     if (obj2 is SymmetricAlgorithm)
                     {
                         return(DecryptKey(encryptedKey.CipherData.CipherValue, (SymmetricAlgorithm)obj2));
                     }
                     useOAEP = (encryptedKey.EncryptionMethod != null) && (encryptedKey.EncryptionMethod.KeyAlgorithm == "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");
                     return(DecryptKey(encryptedKey.CipherData.CipherValue, (RSA)obj2, useOAEP));
                 }
                 break;
             }
             KeyInfoX509Data data = enumerator.Current as KeyInfoX509Data;
             if (data != null)
             {
                 X509Certificate2Enumerator enumerator2 = System.Security.Cryptography.Xml.Utils.BuildBagOfCerts(data, CertUsageType.Decryption).GetEnumerator();
                 while (enumerator2.MoveNext())
                 {
                     RSA privateKey = enumerator2.Current.PrivateKey as RSA;
                     if (privateKey != null)
                     {
                         useOAEP = (encryptedKey.EncryptionMethod != null) && (encryptedKey.EncryptionMethod.KeyAlgorithm == "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");
                         return(DecryptKey(encryptedKey.CipherData.CipherValue, privateKey, useOAEP));
                     }
                 }
                 break;
             }
             KeyInfoRetrievalMethod method = enumerator.Current as KeyInfoRetrievalMethod;
             if (method != null)
             {
                 string idValue = System.Security.Cryptography.Xml.Utils.ExtractIdFromLocalUri(method.Uri);
                 key2 = new EncryptedKey();
                 key2.LoadXml(this.GetIdElement(this.m_document, idValue));
                 return(this.DecryptEncryptedKey(key2));
             }
             KeyInfoEncryptedKey key = enumerator.Current as KeyInfoEncryptedKey;
             if (key != null)
             {
                 key2 = key.EncryptedKey;
                 byte[] buffer = this.DecryptEncryptedKey(key2);
                 if (buffer != null)
                 {
                     SymmetricAlgorithm symmetricAlgorithm = (SymmetricAlgorithm)CryptoConfig.CreateFromName(encryptedKey.EncryptionMethod.KeyAlgorithm);
                     symmetricAlgorithm.Key = buffer;
                     return(DecryptKey(encryptedKey.CipherData.CipherValue, symmetricAlgorithm));
                 }
             }
         }
     }
     return(null);
 }
Esempio n. 12
0
		public void RetrievalMethod () 
		{
			KeyInfoRetrievalMethod retrieval = new KeyInfoRetrievalMethod ();
			retrieval.Uri = "http://www.go-mono.org/";
			info.AddClause (retrieval);
			Assert.AreEqual ("<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RetrievalMethod URI=\"http://www.go-mono.org/\" /></KeyInfo>", (info.GetXml ().OuterXml), "RetrievalMethod");
			Assert.AreEqual (1, info.Count, "RetrievalMethod count");
		}