| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- //
- // CryptoConfigTest.cs - NUnit Test Cases for CryptoConfig
- //
- // Author:
- // Sebastien Pouliot ([email protected])
- //
- // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
- //
- using NUnit.Framework;
- using System;
- using System.Security.Cryptography;
- namespace MonoTests.System.Security.Cryptography
- {
- public class CryptoConfigTest : TestCase
- {
- public CryptoConfigTest () : base ("System.Security.Cryptography.CryptoConfig testsuite") {}
- public CryptoConfigTest (string name) : base (name) {}
- protected override void SetUp () {}
- protected override void TearDown () {}
- public static ITest Suite {
- get {
- return new TestSuite (typeof (CryptoConfigTest));
- }
- }
- public void AssertEquals (string msg, byte[] array1, byte[] array2)
- {
- AllTests.AssertEquals (msg, array1, array2);
- }
- void CreateFromName (string name, string objectname)
- {
- object o = CryptoConfig.CreateFromName (name);
- AssertEquals (name, objectname, o.ToString());
- }
- // validate that CryptoConfig create the exact same implementation between mono and MS
- public void TestCreateFromName ()
- {
- try {
- object o = CryptoConfig.CreateFromName (null);
- }
- catch (ArgumentNullException) {
- // do nothing, this is what we expect
- }
- catch (Exception e) {
- Fail ("ArgumentNullException not thrown: " + e.ToString());
- }
- CreateFromName ("SHA", "System.Security.Cryptography.SHA1CryptoServiceProvider");
- // FIXME: We need to support the machine.config file to get exact same results
- // with the MS .NET Framework
- CreateFromName ("SHA1", "System.Security.Cryptography.SHA1CryptoServiceProvider");
- CreateFromName( "System.Security.Cryptography.SHA1", "System.Security.Cryptography.SHA1CryptoServiceProvider");
- // after installing the WSDK - changes to the machine.config file (not documented)
- // CreateFromName ("SHA1", "System.Security.Cryptography.SHA1Managed");
- // CreateFromName ("System.Security.Cryptography.SHA1", "System.Security.Cryptography.SHA1Managed");
- CreateFromName ("System.Security.Cryptography.HashAlgorithm", "System.Security.Cryptography.SHA1CryptoServiceProvider");
- CreateFromName ("MD5", "System.Security.Cryptography.MD5CryptoServiceProvider");
- CreateFromName ("System.Security.Cryptography.MD5", "System.Security.Cryptography.MD5CryptoServiceProvider");
- CreateFromName ("SHA256", "System.Security.Cryptography.SHA256Managed");
- CreateFromName ("SHA-256", "System.Security.Cryptography.SHA256Managed");
- CreateFromName ("System.Security.Cryptography.SHA256", "System.Security.Cryptography.SHA256Managed");
- CreateFromName ("SHA384", "System.Security.Cryptography.SHA384Managed");
- CreateFromName ("SHA-384", "System.Security.Cryptography.SHA384Managed");
- CreateFromName ("System.Security.Cryptography.SHA384", "System.Security.Cryptography.SHA384Managed");
- CreateFromName ("SHA512", "System.Security.Cryptography.SHA512Managed");
- CreateFromName ("SHA-512", "System.Security.Cryptography.SHA512Managed");
- CreateFromName ("System.Security.Cryptography.SHA512", "System.Security.Cryptography.SHA512Managed");
- /*
- //FIXME: these algorithms are not in corlib yet. re-enable when they are
- CreateFromName ("System.Security.Cryptography.KeyedHashAlgorithm", "System.Security.Cryptography.HMACSHA1");
- CreateFromName ("HMACSHA1", "System.Security.Cryptography.HMACSHA1");
- CreateFromName ("System.Security.Cryptography.HMACSHA1", "System.Security.Cryptography.HMACSHA1");
- CreateFromName ("MACTripleDES", "System.Security.Cryptography.MACTripleDES");
- CreateFromName ("System.Security.Cryptography.MACTripleDES", "System.Security.Cryptography.MACTripleDES");
- CreateFromName ("RSA", "System.Security.Cryptography.RSACryptoServiceProvider");
- CreateFromName ("System.Security.Cryptography.RSA", "System.Security.Cryptography.RSACryptoServiceProvider");
- CreateFromName ("System.Security.Cryptography.AsymmetricAlgorithm", "System.Security.Cryptography.RSACryptoServiceProvider");
- CreateFromName ("DSA", "System.Security.Cryptography.DSACryptoServiceProvider");
- CreateFromName ("System.Security.Cryptography.DSA", "System.Security.Cryptography.DSACryptoServiceProvider");
- CreateFromName ("DES", "System.Security.Cryptography.DESCryptoServiceProvider");
- CreateFromName ("System.Security.Cryptography.DES", "System.Security.Cryptography.DESCryptoServiceProvider");
- CreateFromName ("3DES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
- CreateFromName ("TripleDES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
- CreateFromName ("Triple DES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
- CreateFromName ("System.Security.Cryptography.TripleDES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
- */
- // LAMESPEC SymmetricAlgorithm documented as TripleDESCryptoServiceProvider
- CreateFromName ("System.Security.Cryptography.SymmetricAlgorithm", "System.Security.Cryptography.RijndaelManaged");
- CreateFromName ("RC2", "System.Security.Cryptography.RC2CryptoServiceProvider");
- CreateFromName ("System.Security.Cryptography.RC2", "System.Security.Cryptography.RC2CryptoServiceProvider");
- CreateFromName ("Rijndael", "System.Security.Cryptography.RijndaelManaged");
- CreateFromName ("System.Security.Cryptography.Rijndael", "System.Security.Cryptography.RijndaelManaged");
- // LAMESPEC Undocumented Names in CryptoConfig
- CreateFromName ("RandomNumberGenerator", "System.Security.Cryptography.RNGCryptoServiceProvider");
- CreateFromName ("System.Security.Cryptography.RandomNumberGenerator", "System.Security.Cryptography.RNGCryptoServiceProvider");
- // non existing algo should return null (without exception)
- AssertNull ("NonExistingAlgorithm", CryptoConfig.CreateFromName("NonExistingAlgorithm"));
- }
- // Tests created using "A Layer Man Guide to ASN.1" from RSA, page 19-20
- // Need to find an OID ? goto http://www.alvestrand.no/~hta/objectid/top.html
- static byte[] oidETSI = { 0x06, 0x03, 0x04, 0x00, 0x00 };
- static byte[] oidSHA1 = { 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A };
- static byte[] oidASN1CharacterModule = { 0x06, 0x04, 0x51, 0x00, 0x00, 0x00 };
- static byte[] oidmd5withRSAEncryption = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x04 };
- // LAMESPEC NullReferenceException is thrown (not ArgumentNullException) if parameter is NULL
- public void TestEncodeOID ()
- {
- try {
- byte[] o = CryptoConfig.EncodeOID (null);
- }
- catch (NullReferenceException) {
- // do nothing, this is what we expect
- }
- catch (Exception e) {
- Fail ("NullReferenceException not thrown: " + e.ToString());
- }
- // OID starts with 0, 1 or 2
- AssertEquals ("OID starting with 0.", oidETSI, CryptoConfig.EncodeOID ("0.4.0.0"));
- AssertEquals ("OID starting with 1.", oidSHA1, CryptoConfig.EncodeOID ("1.3.14.3.2.26"));
- AssertEquals ("OID starting with 2.", oidASN1CharacterModule, CryptoConfig.EncodeOID ("2.1.0.0.0"));
- // OID numbers can span multiple bytes
- AssertEquals ("OID with numbers spanning multiple bytes", oidmd5withRSAEncryption, CryptoConfig.EncodeOID ("1.2.840.113549.1.1.4"));
-
- // "ms"-invalid OID - greater than 127 bytes (length encoding)
- // OID longer than 127 bytes (so length must be encoded on multiple bytes)
- // LAMESPEC: OID greater that 0x7F (127) bytes aren't supported by the MS Framework
- string baseOID = "1.3.6.1.4.1.11071.0.";
- string lastPart = "1111111111"; // must fit in int32
- for (int i = 1; i < 30; i++)
- {
- baseOID += lastPart + ".";
- }
- baseOID += "0";
- try {
- byte[] tooLongOID = CryptoConfig.EncodeOID (baseOID);
- }
- catch (CryptographicUnexpectedOperationException) {
- // do nothing, this is what we expect
- }
- catch (Exception e) {
- Fail ("CryptographicUnexpectedOperationException not thrown: " + e.ToString());
- }
-
- // "ms"-invalid OID - where a number of the OID > Int32
- // LAMESPEC: OID with numbers > Int32 aren't supported by the MS BCL
- try {
- byte[] tooLongOID = CryptoConfig.EncodeOID ("1.1.4294967295");
- }
- catch (OverflowException) {
- // do nothing, this is what we expect
- }
- catch (Exception e) {
- Fail( "OverflowException not thrown: " + e.ToString ());
- }
- // 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 };
- AssertEquals ("OID: 3.0", res3, oid3);
- // invalid OID - must have at least 2 parts (according to X.208)
- try {
- byte[] tooShortOID = CryptoConfig.EncodeOID ("0");
- }
- catch (CryptographicUnexpectedOperationException) {
- // do nothing, this is what we expect
- }
- catch (Exception e) {
- Fail("CryptographicUnexpectedOperationException not thrown: " + e.ToString());
- }
- // 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 };
- AssertEquals ("OID: 0.40", tooBigSecondPartRes, tooBigSecondPartOID);
- }
- private void MapNameToOID (string name, string oid)
- {
- AssertEquals ("oid(" + name + ")", oid, CryptoConfig.MapNameToOID (name));
- }
- // LAMESPEC doesn't support all names defined in CryptoConfig
- // non supported names (in MSFW) are commented or null-ed
- public void TestMapNameToOID()
- {
- try {
- CryptoConfig.MapNameToOID (null);
- }
- catch (ArgumentNullException) {
- // do nothing, this is what we expect
- }
- catch (Exception e) {
- Fail( "ArgumentNullException not thrown: " + e.ToString ());
- }
- // MapNameToOID ("SHA", "1.3.14.3.2.26");
- MapNameToOID ("SHA1", "1.3.14.3.2.26");
- MapNameToOID ("System.Security.Cryptography.SHA1", "1.3.14.3.2.26");
- // MapNameToOID ("System.Security.Cryptography.HashAlgorithm", "1.3.14.3.2.26");
- MapNameToOID ("MD5", "1.2.840.113549.2.5");
- MapNameToOID ("System.Security.Cryptography.MD5", "1.2.840.113549.2.5");
- MapNameToOID ("SHA256", "2.16.840.1.101.3.4.1");
- // MapNameToOID ("SHA-256", "2.16.840.1.101.3.4.1");
- MapNameToOID ("System.Security.Cryptography.SHA256", "2.16.840.1.101.3.4.1");
- MapNameToOID ("SHA384", "2.16.840.1.101.3.4.2");
- // MapNameToOID ("SHA-384", "2.16.840.1.101.3.4.2");
- MapNameToOID ("System.Security.Cryptography.SHA384", "2.16.840.1.101.3.4.2");
- MapNameToOID ("SHA512", "2.16.840.1.101.3.4.3");
- // MapNameToOID ("SHA-512", "2.16.840.1.101.3.4.3");
- MapNameToOID ("System.Security.Cryptography.SHA512", "2.16.840.1.101.3.4.3");
- // no OID defined ?
- MapNameToOID ("RSA", null);
- MapNameToOID ("System.Security.Cryptography.RSA", null);
- MapNameToOID ("System.Security.Cryptography.AsymmetricAlgorithm", null);
- MapNameToOID ("DSA", null);
- MapNameToOID ("System.Security.Cryptography.DSA", null);
- MapNameToOID ("DES", null);
- MapNameToOID ("System.Security.Cryptography.DES", null);
- MapNameToOID ("3DES", null);
- MapNameToOID ("TripleDES", null);
- MapNameToOID ("Triple DES", null);
- MapNameToOID ("System.Security.Cryptography.TripleDES", null);
- MapNameToOID ("RC2", null);
- MapNameToOID ("System.Security.Cryptography.RC2", null);
- MapNameToOID ("Rijndael", null);
- MapNameToOID ("System.Security.Cryptography.Rijndael", null);
- MapNameToOID ("System.Security.Cryptography.SymmetricAlgorithm", null);
- // LAMESPEC Undocumented Names in CryptoConfig
- MapNameToOID ("RandomNumberGenerator", null);
- MapNameToOID ("System.Security.Cryptography.RandomNumberGenerator", null);
- MapNameToOID ("System.Security.Cryptography.KeyedHashAlgorithm", null);
- 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);
- }
- public void TestToString()
- {
- // under normal circumstance there are no need to create a CryptoConfig object
- // because all interesting stuff are in static methods
- CryptoConfig cc = new CryptoConfig ();
- AssertEquals ("System.Security.Cryptography.CryptoConfig", cc.ToString ());
- }
- }
- }
|