X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FTest%2FSystem.Security.Cryptography%2FCryptoConfigTest.cs;h=3da3fe6276e9a1873c33aeb48e354bed424ba392;hb=64eff753bdeb747d6c7d35c3c7cc77fe5fd5cea4;hp=6628aee92c60795a7228477fa1abd5b72180386c;hpb=822235c4deb98ce945873fe4fd95874169c14dc0;p=mono.git diff --git a/mcs/class/corlib/Test/System.Security.Cryptography/CryptoConfigTest.cs b/mcs/class/corlib/Test/System.Security.Cryptography/CryptoConfigTest.cs index 6628aee92c6..3da3fe6276e 100644 --- a/mcs/class/corlib/Test/System.Security.Cryptography/CryptoConfigTest.cs +++ b/mcs/class/corlib/Test/System.Security.Cryptography/CryptoConfigTest.cs @@ -5,7 +5,8 @@ // Sebastien Pouliot // // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) -// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com) +// Copyright (C) 2004-2007,2011 Novell, Inc (http://www.novell.com) +// Copyright 2011 Xamarin Inc (http://www.xamarin.com). // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -41,7 +42,7 @@ public class CryptoConfigTest { object o = CryptoConfig.CreateFromName (name); if (objectname == null) Assert.IsNull (o, name); - else + else Assert.AreEqual (objectname, o.ToString (), name); } @@ -103,35 +104,42 @@ public class CryptoConfigTest { CreateFromName ("System.Security.Cryptography.HMACSHA1", "System.Security.Cryptography.HMACSHA1"); CreateFromName ("MACTripleDES", "System.Security.Cryptography.MACTripleDES"); CreateFromName ("System.Security.Cryptography.MACTripleDES", "System.Security.Cryptography.MACTripleDES"); -#if NET_2_0 - // new HMAC - new base class doesn't return anything with it's short name + + // new HMAC - new base class doesn't return anything with it's short name Assert.IsNull (CryptoConfig.CreateFromName ("HMAC"), "HMAC"); - CreateFromName ("System.Security.Cryptography.HMAC", "System.Security.Cryptography.HMACSHA1"); - CreateFromName ("HMACMD5", "System.Security.Cryptography.HMACMD5"); - CreateFromName ("System.Security.Cryptography.HMACMD5", "System.Security.Cryptography.HMACMD5"); - CreateFromName ("HMACRIPEMD160", "System.Security.Cryptography.HMACRIPEMD160"); - CreateFromName ("System.Security.Cryptography.HMACRIPEMD160", "System.Security.Cryptography.HMACRIPEMD160"); - CreateFromName ("HMACSHA256", "System.Security.Cryptography.HMACSHA256"); - CreateFromName ("System.Security.Cryptography.HMACSHA256", "System.Security.Cryptography.HMACSHA256"); - CreateFromName ("HMACSHA384", "System.Security.Cryptography.HMACSHA384"); - CreateFromName ("System.Security.Cryptography.HMACSHA384", "System.Security.Cryptography.HMACSHA384"); - CreateFromName ("HMACSHA512", "System.Security.Cryptography.HMACSHA512"); - CreateFromName ("System.Security.Cryptography.HMACSHA512", "System.Security.Cryptography.HMACSHA512"); - // new hash algorithm - CreateFromName ("RIPEMD160", "System.Security.Cryptography.RIPEMD160Managed"); - CreateFromName ("RIPEMD-160", "System.Security.Cryptography.RIPEMD160Managed"); - CreateFromName ("System.Security.Cryptography.RIPEMD160", "System.Security.Cryptography.RIPEMD160Managed"); -#endif + CreateFromName ("System.Security.Cryptography.HMAC", "System.Security.Cryptography.HMACSHA1"); + CreateFromName ("HMACMD5", "System.Security.Cryptography.HMACMD5"); + CreateFromName ("System.Security.Cryptography.HMACMD5", "System.Security.Cryptography.HMACMD5"); + CreateFromName ("HMACRIPEMD160", "System.Security.Cryptography.HMACRIPEMD160"); + CreateFromName ("System.Security.Cryptography.HMACRIPEMD160", "System.Security.Cryptography.HMACRIPEMD160"); + CreateFromName ("HMACSHA256", "System.Security.Cryptography.HMACSHA256"); + CreateFromName ("System.Security.Cryptography.HMACSHA256", "System.Security.Cryptography.HMACSHA256"); + CreateFromName ("HMACSHA384", "System.Security.Cryptography.HMACSHA384"); + CreateFromName ("System.Security.Cryptography.HMACSHA384", "System.Security.Cryptography.HMACSHA384"); + CreateFromName ("HMACSHA512", "System.Security.Cryptography.HMACSHA512"); + CreateFromName ("System.Security.Cryptography.HMACSHA512", "System.Security.Cryptography.HMACSHA512"); + // new hash algorithm + CreateFromName ("RIPEMD160", "System.Security.Cryptography.RIPEMD160Managed"); + CreateFromName ("RIPEMD-160", "System.Security.Cryptography.RIPEMD160Managed"); + CreateFromName ("System.Security.Cryptography.RIPEMD160", "System.Security.Cryptography.RIPEMD160Managed"); + // x.509 stuff +#if !MOBILE + CreateFromName ("X509Chain", "System.Security.Cryptography.X509Certificates.X509Chain"); + // note: CryptoConfig can create any object ! CreateFromName ("System.Security.Cryptography.CryptoConfig", "System.Security.Cryptography.CryptoConfig"); CreateFromName ("System.IO.MemoryStream", "System.IO.MemoryStream"); - // non existing algo should return null (without exception) +#endif + // non existing algo should return null (without exception) Assert.IsNull (CryptoConfig.CreateFromName ("NonExistingAlgorithm"), "NonExistingAlgorithm"); } // additional names (URL) used for XMLDSIG (System.Security.Cryptography.Xml) // URL taken from http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/ [Test] +#if NET_2_1 + [Ignore ("System.Security.dll is not part of Moonlight, MonoTouch and Mono for Android")] +#endif public void CreateFromURL () { // URL used in SignatureMethod element @@ -140,6 +148,7 @@ public class CryptoConfigTest { CreateFromName ("http://www.w3.org/2000/09/xmldsig#hmac-sha1", null); // URL used in DigestMethod element CreateFromName ("http://www.w3.org/2000/09/xmldsig#sha1", "System.Security.Cryptography.SHA1CryptoServiceProvider"); +#if !TARGET_JVM //TargetJvmNotWorking - algorithms from System.Security assembly // URL used in Canonicalization or Transform elements CreateFromName ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", "System.Security.Cryptography.Xml.XmlDsigC14NTransform"); CreateFromName ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", "System.Security.Cryptography.Xml.XmlDsigC14NWithCommentsTransform"); @@ -160,6 +169,57 @@ public class CryptoConfigTest { CreateFromName ("http://www.w3.org/2000/09/xmldsig# KeyValue/DSAKeyValue", "System.Security.Cryptography.Xml.DSAKeyValue"); CreateFromName ("http://www.w3.org/2000/09/xmldsig# KeyValue/RSAKeyValue", "System.Security.Cryptography.Xml.RSAKeyValue"); CreateFromName ("http://www.w3.org/2000/09/xmldsig# RetrievalMethod", "System.Security.Cryptography.Xml.KeyInfoRetrievalMethod"); +#if NET_2_0 + CreateFromName ("http://www.w3.org/2001/04/xmlenc#sha256", "System.Security.Cryptography.SHA256Managed"); + CreateFromName ("http://www.w3.org/2001/04/xmlenc#sha384", null); + CreateFromName ("http://www.w3.org/2001/04/xmlenc#sha512", "System.Security.Cryptography.SHA512Managed"); + + CreateFromName ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", "System.Security.Cryptography.HMACSHA256"); + CreateFromName ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha384", "System.Security.Cryptography.HMACSHA384"); + CreateFromName ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha512", "System.Security.Cryptography.HMACSHA512"); + CreateFromName ("http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160", "System.Security.Cryptography.HMACRIPEMD160"); +#endif +#endif + } + + [Test] + public void CreateFromName_UpperCase () + { + CreateFromName ("SHA", "System.Security.Cryptography.SHA1CryptoServiceProvider"); +#if NET_2_0 + CreateFromName ("SYSTEM.SECURITY.CRYPTOGRAPHY.TRIPLEDES", "System.Security.Cryptography.TripleDESCryptoServiceProvider"); + CreateFromName ("HTTP://WWW.W3.ORG/2000/09/XMLDSIG#DSA-SHA1", "System.Security.Cryptography.DSASignatureDescription"); +#else + CreateFromName ("SYSTEM.SECURITY.CRYPTOGRAPHY.TRIPLEDES", null); + CreateFromName ("HTTP://WWW.W3.ORG/2000/09/XMLDSIG#DSA-SHA1", null); +#endif + } + + [Test] + public void CreateFromName_LowerCase () + { +#if NET_2_0 + CreateFromName ("sha", "System.Security.Cryptography.SHA1CryptoServiceProvider"); + CreateFromName ("system.security.cryptography.tripledes", "System.Security.Cryptography.TripleDESCryptoServiceProvider"); +#else + CreateFromName ("sha", null); + CreateFromName ("system.security.cryptography.tripledes", null); +#endif + CreateFromName ("http://www.w3.org/2000/09/xmldsig#dsa-sha1", "System.Security.Cryptography.DSASignatureDescription"); + } + + [Test] + public void CreateFromName_MixedCase () + { +#if NET_2_0 + CreateFromName ("ShA", "System.Security.Cryptography.SHA1CryptoServiceProvider"); + CreateFromName ("SyStEm.SeCuRiTy.CrYpToGrApHy.TrIpLeDeS", "System.Security.Cryptography.TripleDESCryptoServiceProvider"); + CreateFromName ("hTtP://wWw.W3.oRg/2000/09/xMlDsIg#dSa-sHa1", "System.Security.Cryptography.DSASignatureDescription"); +#else + CreateFromName ("ShA", null); + CreateFromName ("SyStEm.SeCuRiTy.CrYpToGrApHy.TrIpLeDeS", null); + CreateFromName ("hTtP://wWw.W3.oRg/2000/09/xMlDsIg#dSa-sHa1", null); +#endif } // Tests created using "A Layer Man Guide to ASN.1" from RSA, page 19-20 @@ -183,11 +243,11 @@ public class CryptoConfigTest { [Test] public void EncodeOID () { - // OID starts with 0, 1 or 2 - Assert.AreEqual (oidETSI, CryptoConfig.EncodeOID ("0.4.0.0"), "OID starting with 0."); - Assert.AreEqual (oidSHA1, CryptoConfig.EncodeOID ("1.3.14.3.2.26"), "OID starting with 1."); + // OID starts with 0, 1 or 2 + Assert.AreEqual (oidETSI, CryptoConfig.EncodeOID ("0.4.0.0"), "OID starting with 0."); + Assert.AreEqual (oidSHA1, CryptoConfig.EncodeOID ("1.3.14.3.2.26"), "OID starting with 1."); Assert.AreEqual (oidASN1CharacterModule, CryptoConfig.EncodeOID ("2.1.0.0.0"), "OID starting with 2."); - // OID numbers can span multiple bytes + // OID numbers can span multiple bytes Assert.AreEqual (oidmd5withRSAEncryption, CryptoConfig.EncodeOID ("1.2.840.113549.1.1.4"), "OID with numbers spanning multiple bytes"); } @@ -222,7 +282,7 @@ public class CryptoConfigTest { // invalid OID - must start with 0, 1 or 2 // however it works with MS BCL byte[] oid3 = CryptoConfig.EncodeOID ("3.0"); - byte[] res3 = { 0x06, 0x01, 0x78 }; + byte[] res3 = { 0x06, 0x01, 0x78 }; Assert.AreEqual (res3, oid3, "OID: 3.0"); } @@ -240,7 +300,7 @@ public class CryptoConfigTest { // invalid OID - second value < 40 for 0. and 1. (modulo 40) // however it works with MS BCL byte[] tooBigSecondPartOID = CryptoConfig.EncodeOID ("0.40"); - byte[] tooBigSecondPartRes = { 0x06, 0x01, 0x28 }; + byte[] tooBigSecondPartRes = { 0x06, 0x01, 0x28 }; Assert.AreEqual (tooBigSecondPartRes, tooBigSecondPartOID, "OID: 0.40"); } @@ -252,7 +312,7 @@ public class CryptoConfigTest { } private void MapNameToOID (string name, string oid) - { + { Assert.AreEqual (oid, CryptoConfig.MapNameToOID (name), "oid(" + name + ")"); } @@ -281,6 +341,9 @@ public class CryptoConfigTest { MapNameToOID ("SHA512", "2.16.840.1.101.3.4.2.3"); MapNameToOID ("System.Security.Cryptography.SHA512", "2.16.840.1.101.3.4.2.3"); MapNameToOID ("System.Security.Cryptography.SHA512Managed", "2.16.840.1.101.3.4.2.3"); + MapNameToOID ("RIPEMD160", "1.3.36.3.2.1"); + MapNameToOID ("System.Security.Cryptography.RIPEMD160", "1.3.36.3.2.1"); + MapNameToOID ("System.Security.Cryptography.RIPEMD160Managed", "1.3.36.3.2.1"); #else MapNameToOID ("SHA256", "2.16.840.1.101.3.4.1"); // MapNameToOID ("SHA-256", "2.16.840.1.101.3.4.1"); @@ -299,21 +362,18 @@ public class CryptoConfigTest { MapNameToOID ("TripleDESKeyWrap", "1.2.840.113549.1.9.16.3.6"); #if NET_2_0 // new OID defined in Fx 2.0 -// MapNameToOID ("RSA", "1.2.840.113549.1.1.1"); - MapNameToOID ("DSA", "1.2.840.10040.4.1"); MapNameToOID ("DES", "1.3.14.3.2.7"); - MapNameToOID ("3DES", "1.2.840.113549.3.7"); MapNameToOID ("TripleDES", "1.2.840.113549.3.7"); MapNameToOID ("RC2", "1.2.840.113549.3.2"); #else // no OID defined before Fx 2.0 - MapNameToOID ("RSA", null); - MapNameToOID ("DSA", null); MapNameToOID ("DES", null); - MapNameToOID ("3DES", null); MapNameToOID ("TripleDES", null); MapNameToOID ("RC2", null); #endif + MapNameToOID ("RSA", null); + MapNameToOID ("DSA", null); + MapNameToOID ("3DES", null); // no OID defined ? MapNameToOID ("System.Security.Cryptography.RSA", null); MapNameToOID ("System.Security.Cryptography.AsymmetricAlgorithm", null); @@ -329,28 +389,63 @@ public class CryptoConfigTest { MapNameToOID ("RandomNumberGenerator", null); MapNameToOID ("System.Security.Cryptography.RandomNumberGenerator", null); MapNameToOID ("System.Security.Cryptography.KeyedHashAlgorithm", null); -#if NET_2_0 - MapNameToOID ("HMAC", null); - MapNameToOID ("System.Security.Cryptography.HMAC", null); - MapNameToOID ("HMACMD5", null); - MapNameToOID ("System.Security.Cryptography.HMACMD5", null); - MapNameToOID ("HMACRIPEMD160", null); - MapNameToOID ("System.Security.Cryptography.HMACRIPEMD160", null); - MapNameToOID ("HMACSHA256", null); - MapNameToOID ("System.Security.Cryptography.HMACSHA256", null); - MapNameToOID ("HMACSHA384", null); - MapNameToOID ("System.Security.Cryptography.HMACSHA384", null); - MapNameToOID ("HMACSHA512", null); - MapNameToOID ("System.Security.Cryptography.HMACSHA512", null); -#endif - MapNameToOID ("HMACSHA1", null); - MapNameToOID ("System.Security.Cryptography.HMACSHA1", null); +#if NET_2_0 + MapNameToOID ("HMAC", null); + MapNameToOID ("System.Security.Cryptography.HMAC", null); + MapNameToOID ("HMACMD5", null); + MapNameToOID ("System.Security.Cryptography.HMACMD5", null); + MapNameToOID ("HMACRIPEMD160", null); + MapNameToOID ("System.Security.Cryptography.HMACRIPEMD160", null); + MapNameToOID ("HMACSHA256", null); + MapNameToOID ("System.Security.Cryptography.HMACSHA256", null); + MapNameToOID ("HMACSHA384", null); + MapNameToOID ("System.Security.Cryptography.HMACSHA384", null); + MapNameToOID ("HMACSHA512", null); + MapNameToOID ("System.Security.Cryptography.HMACSHA512", null); +#endif + MapNameToOID ("HMACSHA1", null); + MapNameToOID ("System.Security.Cryptography.HMACSHA1", null); MapNameToOID ("MACTripleDES", null); MapNameToOID ("System.Security.Cryptography.MACTripleDES", null); // non existing algo should return null (without exception) MapNameToOID ("NonExistingAlgorithm", null); } + [Test] + public void MapNameToOID_UpperCase () + { + MapNameToOID ("SHA1", "1.3.14.3.2.26"); +#if NET_2_0 + MapNameToOID ("SYSTEM.SECURITY.CRYPTOGRAPHY.MD5CRYPTOSERVICEPROVIDER", "1.2.840.113549.2.5"); +#else + MapNameToOID ("SYSTEM.SECURITY.CRYPTOGRAPHY.MD5CRYPTOSERVICEPROVIDER", null); +#endif + } + + [Test] + public void MapNameToOID_LowerCase () + { +#if NET_2_0 + MapNameToOID ("sha1", "1.3.14.3.2.26"); + MapNameToOID ("system.security.cryptography.md5cryptoserviceprovider", "1.2.840.113549.2.5"); +#else + MapNameToOID ("sha1", null); + MapNameToOID ("system.security.cryptography.md5cryptoserviceprovider", null); +#endif + } + + [Test] + public void MapNameToOID_MixedCase () + { +#if NET_2_0 + MapNameToOID ("sHa1", "1.3.14.3.2.26"); + MapNameToOID ("SySteM.SeCuRiTy.CrYpToGrApHy.Md5cRyPtOsErViCePrOvIdEr", "1.2.840.113549.2.5"); +#else + MapNameToOID ("sHa1", null); + MapNameToOID ("SySteM.SeCuRiTy.CrYpToGrApHy.Md5cRyPtOsErViCePrOvIdEr", null); +#endif + } + [Test] public void CCToString () {