Merge pull request #5636 from BrzVlad/fix-xmm-scan
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / RSACryptoServiceProviderTest.cs
index e3186127c9dfa19278355774edaf9add0f8e43ae..c9011cb41f6f9a37736eb9d0ba9e89d39e203b67 100644 (file)
@@ -93,9 +93,7 @@ public class RSACryptoServiceProviderTest {
                // test default key size
                Assert.AreEqual (1024, rsa.KeySize, "ConstructorEmpty");
                Assert.IsFalse (rsa.PersistKeyInCsp, "PersistKeyInCsp");
-#if NET_2_0
                Assert.IsFalse (rsa.PublicOnly, "PublicOnly");
-#endif
        }
 
        [Test]
@@ -105,13 +103,10 @@ public class RSACryptoServiceProviderTest {
                // test default key size
                Assert.AreEqual (minKeySize, rsa.KeySize, "ConstructorKeySize");
                Assert.IsFalse (rsa.PersistKeyInCsp, "PersistKeyInCsp");
-#if NET_2_0
                Assert.IsFalse (rsa.PublicOnly, "PublicOnly");
-#endif
        }
 
        [Test]
-       [Category ("TargetJvmNotSupported")]
        public void ConstructorCspParameters ()
        {
                CspParameters csp = new CspParameters (1, null, "Mono1024");
@@ -120,13 +115,10 @@ public class RSACryptoServiceProviderTest {
                // test default key size
                Assert.AreEqual (1024, rsa.KeySize, "ConstructorCspParameters");
                Assert.IsTrue (rsa.PersistKeyInCsp, "PersistKeyInCsp");
-#if NET_2_0
                Assert.IsFalse (rsa.PublicOnly, "PublicOnly");
-#endif
        }
 
        [Test]
-       [Category ("TargetJvmNotSupported")]
        public void ConstructorKeySizeCspParameters ()
        {
                int keySize = 512;
@@ -134,9 +126,7 @@ public class RSACryptoServiceProviderTest {
                rsa = new RSACryptoServiceProvider (keySize, csp);
                Assert.AreEqual (keySize, rsa.KeySize, "ConstructorCspParameters");
                Assert.IsTrue (rsa.PersistKeyInCsp, "PersistKeyInCsp");
-#if NET_2_0
                Assert.IsFalse (rsa.PublicOnly, "PublicOnly");
-#endif
        }
 
        [Test]
@@ -165,13 +155,11 @@ public class RSACryptoServiceProviderTest {
        public void TooSmallKeyPair () 
        {
                rsa = new RSACryptoServiceProvider (256);
-#if NET_2_0
                // in 2.0 MS delay the creation of the key pair until it is required
                // (same trick that Mono almost always used ;-) but they also delay
                // the parameter validation (what Mono didn't). So here we must "get"
                // the key (export) to trigger the exception
                rsa.ToXmlString (true);
-#endif
        }
 
        [Test]
@@ -179,13 +167,11 @@ public class RSACryptoServiceProviderTest {
        public void TooBigKeyPair () 
        {
                rsa = new RSACryptoServiceProvider (32768);
-#if NET_2_0
                // in 2.0 MS delay the creation of the key pair until it is required
                // (same trick that Mono almost always used ;-) but they also delay
                // the parameter validation (what Mono didn't). So here we must "get"
                // the key (export) to trigger the exception
                rsa.ToXmlString (true);
-#endif
        }
 
        [Test]
@@ -262,6 +248,26 @@ public class RSACryptoServiceProviderTest {
                rsa.SignData (data, MD5.Create ());
        }
 
+       [Test]
+       [ExpectedException (typeof (ArgumentException))]
+       public void SignDataWithInvalidOid ()
+       {
+               byte[] data = new byte [5];
+               rsa = new RSACryptoServiceProvider (minKeySize);
+
+               rsa.SignData (data, "1.2.3");
+       }
+
+       [Test]
+       public void SignDataWithOid ()
+       {
+               string oid  = CryptoConfig.MapNameToOID ("SHA256");
+               byte[] data = new byte [5];
+               rsa = new RSACryptoServiceProvider (minKeySize);
+
+               rsa.SignData (data, oid);
+       }
+
        [Test]
        [ExpectedException (typeof (ArgumentNullException))]
        public void SignHashNullValue () 
@@ -414,7 +420,7 @@ public class RSACryptoServiceProviderTest {
                rsa.VerifyHash (hash, "1.3.14.3.2.26", null);
        }
 
-#if NET_2_0 && !NET_2_1
+#if !MOBILE
        [Test]
        [Category ("NotWorking")]
        public void ImportDisposed ()
@@ -837,6 +843,20 @@ public class RSACryptoServiceProviderTest {
                rsa.ToXmlString (true);
        }
 
+       [Test]
+       [ExpectedException (typeof (CryptographicException))]
+       public void ExportWithoutCRT_2 () 
+       {
+               try {
+                       rsa = new RSACryptoServiceProvider ();
+                       rsa.FromXmlString (MonoXml384woCRT);
+               }
+               catch {
+               }
+               // exception is HERE!
+               rsa.ExportParameters (true);
+       }
+
        // Validate that we can sign with every keypair and verify the signature
        // With Windows this means that we can use Mono keypairs to sign and verify.
        // For Mono this doesn't mean much.
@@ -981,7 +1001,6 @@ public class RSACryptoServiceProviderTest {
        //      http://msdn.microsoft.com/library/en-us/cpguide/html/cpcongeneratingkeysforencryptiondecryption.asp
 
        [Test]
-       [Category ("TargetJvmNotSupported")]
        public void Persistence_PersistKeyInCsp_False () 
        {
                CspParameters csp = new CspParameters (1, null, "Persistence_PersistKeyInCsp_False");
@@ -1006,7 +1025,6 @@ public class RSACryptoServiceProviderTest {
        }
 
        [Test]
-       [Category ("TargetJvmNotSupported")]
        public void Persistence_PersistKeyInCsp_True () 
        {
                CspParameters csp = new CspParameters (1, null, "Persistence_PersistKeyInCsp_True");
@@ -1027,7 +1045,6 @@ public class RSACryptoServiceProviderTest {
        }
 
        [Test]
-       [Category ("TargetJvmNotSupported")]
        public void Persistence_Delete () 
        {
                CspParameters csp = new CspParameters (1, null, "Persistence_Delete");
@@ -1049,7 +1066,6 @@ public class RSACryptoServiceProviderTest {
                Assert.IsTrue ((original != newKeyPair), "Key Pair Deleted");
        }
 
-#if NET_1_1
        [Test]
        public void UseMachineKeyStore_Default ()
        {
@@ -1057,7 +1073,6 @@ public class RSACryptoServiceProviderTest {
        }
 
        [Test]
-       [Category ("TargetJvmNotSupported")]
        public void UseMachineKeyStore () 
        {
                // note only applicable when CspParameters isn't used - which don't
@@ -1086,7 +1101,6 @@ public class RSACryptoServiceProviderTest {
                catch (UnauthorizedAccessException) {
                }
        }
-#endif
 
        [Test]
        public void PKCS1 () 
@@ -1119,9 +1133,7 @@ public class RSACryptoServiceProviderTest {
        {
                rsa = new RSACryptoServiceProvider ();
                rsa.FromXmlString ("<RSAKeyValue><Modulus>iSObDmmhDgrl4NiLaviFcpv4NdysBWJcqiVz3AQbPdajtXaQQ8VJdfRkixah132yKOFGCWZhHS3EuPMh8dcNwGwta2nh+m2IV6ktzI4+mZ7CSNAsmlDY0JI+H8At1vKvNArlC5jkVGuliYroJeSU/NLPLNYgspi7TtXGy9Rfug8=</Modulus><Exponent>EQ==</Exponent></RSAKeyValue>");
-#if NET_2_0
                Assert.IsTrue (rsa.PublicOnly, "PublicOnly");
-#endif
                string b64 = @"YgyAhscnTTIcDeLJTZcOYYyHVxNhV6d03jeZYjq0sPMEsfCCbE/NcFyYHD9BTuiduqPplCLbGpfZIZYJ6vAP9m5z4Q9eEw79kmEFCsm8wSKEo/gKiptVpwQ78VOPrWd/wEkTTeeg2nVim3JIsTKGFlV7rKxIWQhGN9aAqgP8nZI=";
                byte [] bytes = Convert.FromBase64String (b64);
                rsa.Decrypt (bytes, true);
@@ -1149,8 +1161,7 @@ public class RSACryptoServiceProviderTest {
                Assert.IsNotNull (r.Decrypt (bytes, true));
        }
 
-#if NET_2_0
-#if !NET_2_1
+#if !MOBILE
        [Test]
        [Category ("NotWorking")]
        public void CspKeyContainerInfo_NewKeypair ()
@@ -1296,6 +1307,74 @@ public class RSACryptoServiceProviderTest {
                        Assert.AreEqual (blob [i], keypair [i], i.ToString ());
        }
 
+       [Test]
+       public void ImportCspBlob_Signature_Keypair ()
+       {
+               // from bug #5299
+               byte[] blob = new byte [] {  
+                       0x07, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 
+                       0x32, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0xCB, 0xF3, 
+                       0xF0, 0x0C, 0xD7, 0xC1, 0xA8, 0x06, 0x1A, 0xF5, 0x49, 0x4E, 0x7A, 
+                       0x02, 0x4A, 0x46, 0xB2, 0x8F, 0xE0, 0x01, 0x5C, 0x89, 0x01, 0x2D, 
+                       0x42, 0x5A, 0xEA, 0x16, 0x11, 0x66, 0x48, 0x26, 0x29, 0xAE, 0x2A, 
+                       0xAA, 0xD4, 0x3C, 0x27, 0xF6, 0x48, 0x0E, 0x09, 0x25, 0xD0, 0x63, 
+                       0x80, 0x74, 0xDA, 0x1B, 0x57, 0x1D, 0x62, 0x83, 0xB9, 0x58, 0x5D, 
+                       0x09, 0x4D, 0x0B, 0x1F, 0x3F, 0xC8, 0xB8, 0x99, 0x3B, 0x9A, 0x57, 
+                       0x16, 0x18, 0xE8, 0x73, 0x2F, 0x34, 0x96, 0x5C, 0xE6, 0x8F, 0x1E, 
+                       0xF8, 0x46, 0x4A, 0x90, 0x16, 0x3F, 0x40, 0x18, 0x53, 0x62, 0x7F, 
+                       0x24, 0xB2, 0x3A, 0xFB, 0xA2, 0x80, 0x39, 0x1C, 0x08, 0x6D, 0xFC, 
+                       0x6B, 0xCA, 0x0A, 0x14, 0xDD, 0xA9, 0x41, 0x57, 0x25, 0x49, 0x3A, 
+                       0x27, 0x9C, 0x25, 0xC4, 0x5E, 0xC5, 0x7B, 0x83, 0x1B, 0x9D, 0xDF, 
+                       0x03, 0x24, 0x94, 0x5D, 0x90, 0xD5, 0xEC, 0x7B, 0xD5, 0xCF, 0x66, 
+                       0x3D, 0x93, 0x63, 0xD5, 0x1A, 0x9A, 0x9B, 0x46, 0x78, 0x89, 0xD1, 
+                       0xC3, 0x38, 0x9A, 0x69, 0x61, 0xFE, 0xA2, 0x0C, 0xD9, 0x73, 0x81, 
+                       0x11, 0x28, 0x71, 0x06, 0x69, 0x3D, 0x1A, 0x4E, 0xF4, 0x9D, 0x8A, 
+                       0x5C, 0x9A, 0x2F, 0x71, 0x9F, 0x2B, 0x9F, 0xE6, 0xB1, 0xBF, 0x4E, 
+                       0x8C, 0xFA, 0x93, 0x04, 0x29, 0xD2, 0x4A, 0x73, 0x85, 0x7A, 0x91, 
+                       0x8D, 0x19, 0xCA, 0x1F, 0x2E, 0x5D, 0xD1, 0xAD, 0x70, 0xB5, 0x64, 
+                       0x69, 0x5B, 0x09, 0x84, 0x35, 0xB1, 0x31, 0x15, 0x40, 0x6C, 0x0D, 
+                       0x31, 0xF3, 0xEA, 0xE4, 0xD7, 0x6D, 0x42, 0xD0, 0xBB, 0x84, 0xDB, 
+                       0x73, 0x10, 0x16, 0x0B, 0xDC, 0xE2, 0x62, 0x32, 0xEB, 0x23, 0x19, 
+                       0x38, 0x64, 0x6A, 0x3D, 0x5E, 0x65, 0xAD, 0x7B, 0xEC, 0xB6, 0xC0, 
+                       0x00, 0x4A, 0x4F, 0x98, 0x35, 0xF3, 0xD7, 0x73, 0xD3, 0x31, 0xDE, 
+                       0xB6, 0x85, 0xDA, 0x4C, 0x3D, 0x79, 0x5A, 0x96, 0x07, 0x63, 0x70, 
+                       0x34, 0x45, 0xF4, 0x78, 0x25, 0x78, 0x92, 0x3C, 0x56, 0x38, 0xF9, 
+                       0xFA, 0x0D, 0xF4, 0x00, 0xD3, 0xD7, 0xBB, 0xA3, 0x97, 0xB4, 0x26, 
+                       0xEB, 0x25, 0xA3, 0x1E, 0x34, 0xFC, 0x0E, 0x7F, 0x6A, 0x12, 0x25, 
+                       0x25, 0x50, 0x26, 0x8D, 0x10, 0x80, 0xC7, 0xEB, 0x4B, 0x4B, 0x74, 
+                       0x09, 0x89, 0x2F, 0xBB, 0x03, 0x02, 0x17, 0xFD, 0x13, 0x71, 0x33, 
+                       0xB4, 0x46, 0x49, 0xDD, 0xBE, 0x34, 0x01, 0x37, 0xC3, 0x63, 0x4D, 
+                       0xBE, 0x76, 0xAD, 0x62, 0x11, 0x6C, 0xDD, 0x64, 0x47, 0x73, 0x95, 
+                       0x92, 0x58, 0xBF, 0xFF, 0xE3, 0x20, 0xD2, 0xB1, 0xEC, 0xA8, 0x03, 
+                       0xB8, 0xA7, 0x0E, 0xA5, 0xAE, 0xAF, 0x47, 0x45, 0xED, 0x5F, 0xE1, 
+                       0x0B, 0xA8, 0xA3, 0x03, 0xB7, 0x93, 0xA6, 0xD8, 0xAC, 0x71, 0xAB, 
+                       0x77, 0x8B, 0xEC, 0x6F, 0x16, 0x0E, 0x1A, 0x2B, 0x2D, 0x31, 0xBD, 
+                       0x69, 0xD4, 0x9E, 0x9E, 0x0F, 0xA2, 0xED, 0x94, 0x59, 0x1D, 0x61, 
+                       0x0E, 0xE5, 0xD3, 0x19, 0x2B, 0xAD, 0x70, 0x90, 0xAF, 0x51, 0x7F, 
+                       0x56, 0x53, 0xC2, 0x86, 0xB4, 0x24, 0xBC, 0xD0, 0x63, 0xAC, 0x4B, 
+                       0xE0, 0xE0, 0x6C, 0xF5, 0xF6, 0x21, 0x7D, 0xE9, 0x7C, 0x45, 0x13, 
+                       0xE7, 0x87, 0x11, 0x09, 0xCC, 0xA6, 0xB1, 0xCD, 0x49, 0x10, 0x33, 
+                       0xBC, 0x07, 0xC8, 0x56, 0xA9, 0x19, 0xC1, 0x86, 0xDF, 0x63, 0xDF, 
+                       0xE6, 0x6D, 0xFB, 0x46, 0x31, 0x93, 0x36, 0x3C, 0x8E, 0x6E, 0xB0, 
+                       0xC7, 0x66, 0xFC, 0x6C, 0x85, 0x5B, 0xF4, 0xEE, 0x1D, 0x3B, 0xE3, 
+                       0x8B, 0xF5, 0xB9, 0x88, 0x48, 0x2F, 0x77, 0x56, 0x82, 0x85, 0x7C, 
+                       0xF9, 0xE9, 0x15, 0xA5, 0x8E, 0x46, 0xEA, 0x08, 0xB9, 0xD0, 0x8F, 
+                       0x0F, 0x28, 0x1C, 0x96, 0xA1, 0xB3, 0x00, 0x6E, 0x9B, 0x81, 0xBD, 
+                       0xD4, 0x54, 0xA4, 0xFD, 0xD5, 0xA7, 0x4B, 0x2E, 0x17, 0x10, 0xED, 
+                       0xD2, 0xAA, 0x38, 0x2E, 0x24, 0x7C, 0x59, 0xF4, 0x2D, 0x08, 0x3B, 
+                       0x15, 0x05, 0x6A, 0xD8, 0x61, 0x8A, 0xAC, 0xCD, 0x5E, 0x77, 0x4C, 
+                       0x8E, 0x0C, 0xEE, 0xD8, 0xEF, 0xD0, 0xBC, 0x1B, 0x14, 0x17, 0xE3, 
+                       0x38, 0x27, 0xA1, 0x70, 0x5B, 0x5C, 0xC3, 0xE3, 0x91, 0x1E, 0x01, 
+                       0xE3, 0x9A, 0x16, 0x1A, 0x5C, 0x4D, 0xD9, 0x3B, 0x36, 0x7F, 0x0B, 
+                       0x93, 0x16
+               };
+               rsa = new RSACryptoServiceProvider ();
+               rsa.ImportCspBlob (blob);
+
+               byte[] keypair = rsa.ExportCspBlob (false);
+               Assert.AreEqual (keypair [5], blob [5], "0x24 signature");
+       }
+
        [Test]
        public void ExportCspBlob_PublicKey ()
        {
@@ -1324,7 +1403,16 @@ public class RSACryptoServiceProviderTest {
                rsa = new RSACryptoServiceProvider (minKeySize);
                rsa.ImportCspBlob (blob);
        }
-#endif
+
+       [Test] //bug 38054
+       public void NonExportableKeysAreNonExportable ()
+       {
+               var cspParams = new CspParameters();
+               cspParams.KeyContainerName = "TestRSAKey";
+               cspParams.Flags = CspProviderFlags.UseNonExportableKey;
+               var rsa = new RSACryptoServiceProvider(cspParams);
+               Assert.Throws<CryptographicException>(() => rsa.ExportParameters(true));
+       }
 }
 
 }