+2003-07-05 Sebastien Pouliot <spouliot@videotron.ca>
+
+ * DSACryptoServiceProviderTest.cs: Added news unit tests for Dispose/Clear
+ and better test coverage (based on Zoltan's logs).
+ * DSATest.cs: Converted to NUnit2 format. Added new tests for better unit
+ test coverage (import incomplete public keys).
+ * RSACryptoServiceProviderTest.cs: Added news unit tests for Dispose/Clear
+ and better test coverage (based on Zoltan's logs).
+ * RSATest.cs: Converted to NUnit2 format. Added new tests for better unit
+ test coverage (import incomplete public keys).
+
2003-06-23 Nick Drochak <ndrochak@gol.com>
* MACTripleDESTest.cs: Adjust for .NET 1.1.
// Author:
// Sebastien Pouliot (spouliot@motus.com)
//
-// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
+// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
//
using NUnit.Framework;
namespace MonoTests.System.Security.Cryptography {
-public class DSACryptoServiceProviderTest : TestCase {
- protected DSA dsa;
+[TestFixture]
+public class DSACryptoServiceProviderTest : Assertion {
+
+ protected DSACryptoServiceProvider dsa;
+ protected DSACryptoServiceProvider disposed;
// static string xmlPrivate = "<DSAKeyValue><P>s/Oc0t4gj0NRqkCKi4ynJnOAEukNhjkHJPOzNsHP69kyHMUwZ3AzOkLGYOWlOo2zlYKzSbZygDDI5dCWA5gQF2ZGHEUlWJMgUyHmkybOi44cyHaX9yeGfbnoc3xF9sYgkA3vPUZaJuYMOsBp3pyPdeN8/mLU8n0ivURyP+3Ge9M=</P><Q>qkcTW+Ce0L5k8OGTUMkRoGKDc1E=</Q><G>PU/MeGp6I/FBduuwD9UPeCFzg8Ib9H5osku5nT8AhHTY8zGqetuvHhxbESt4lLz8aXzX0oIiMsusBr6E/aBdooBI36fHwW8WndCmwkB1kv7mhRIB4302UrfvC2KWQuBypfl0++a1whBMCh5VTJYH1sBkFIaVNeUbt5Q6/UdiZVY=</G><Y>shJRUdGxEYxSKM5JVol9HAdQwIK+wF9X4n9SAD++vfZOMOYi+M1yuvQAlQvnSlTTWr7CZPRVAICLgDBbqi9iN+Id60ccJ+hw3pGDfLpJ7IdFPszJEeUO+SZBwf8njGXULqSODs/NTciiX7E07rm+KflxFOg0qtWAhmYLxIkDx7s=</Y><J>AAAAAQ6LSuRiYdsocZ6rgyqIOpE1/uCO1PfEn758Lg2VW6OHJTYHNC30s0gSTG/Jt3oHYX+S8vrtNYb8kRJ/ipgcofGq2Qo/cYKP7RX2K6EJwSfWInhsNMr1JmzuK0lUKkXXXVo15fL8O2/16uEWMg==</J><Seed>uYM5b20luvbuyevi9TXHwekbr5s=</Seed><PgenCounter>4A==</PgenCounter><X>fAOytZttUZFzt/AvwRinmvYKL7E=</X></DSAKeyValue>";
// static string xmlPublic = "<DSAKeyValue><P>s/Oc0t4gj0NRqkCKi4ynJnOAEukNhjkHJPOzNsHP69kyHMUwZ3AzOkLGYOWlOo2zlYKzSbZygDDI5dCWA5gQF2ZGHEUlWJMgUyHmkybOi44cyHaX9yeGfbnoc3xF9sYgkA3vPUZaJuYMOsBp3pyPdeN8/mLU8n0ivURyP+3Ge9M=</P><Q>qkcTW+Ce0L5k8OGTUMkRoGKDc1E=</Q><G>PU/MeGp6I/FBduuwD9UPeCFzg8Ib9H5osku5nT8AhHTY8zGqetuvHhxbESt4lLz8aXzX0oIiMsusBr6E/aBdooBI36fHwW8WndCmwkB1kv7mhRIB4302UrfvC2KWQuBypfl0++a1whBMCh5VTJYH1sBkFIaVNeUbt5Q6/UdiZVY=</G><Y>shJRUdGxEYxSKM5JVol9HAdQwIK+wF9X4n9SAD++vfZOMOYi+M1yuvQAlQvnSlTTWr7CZPRVAICLgDBbqi9iN+Id60ccJ+hw3pGDfLpJ7IdFPszJEeUO+SZBwf8njGXULqSODs/NTciiX7E07rm+KflxFOg0qtWAhmYLxIkDx7s=</Y><J>AAAAAQ6LSuRiYdsocZ6rgyqIOpE1/uCO1PfEn758Lg2VW6OHJTYHNC30s0gSTG/Jt3oHYX+S8vrtNYb8kRJ/ipgcofGq2Qo/cYKP7RX2K6EJwSfWInhsNMr1JmzuK0lUKkXXXVo15fL8O2/16uEWMg==</J><Seed>uYM5b20luvbuyevi9TXHwekbr5s=</Seed><PgenCounter>4A==</PgenCounter></DSAKeyValue>";
- protected override void SetUp () {}
+ static int minKeySize = 512;
- protected override void TearDown () {}
+ [SetUp]
+ void Setup ()
+ {
+ if (disposed == null) {
+ disposed = new DSACryptoServiceProvider (minKeySize);
+ disposed.Clear ();
+ }
+ }
public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
AssertEquals (message + " X", expectedKey.X, actualKey.X);
}
- public void TestConstructors ()
+ [Test]
+ public void ConstructorEmpty ()
{
- CspParameters csp = new CspParameters ();
- DSACryptoServiceProvider dsa1 = new DSACryptoServiceProvider ();
- DSACryptoServiceProvider dsa2 = new DSACryptoServiceProvider (512);
-// DSACryptoServiceProvider dsa3 = new DSACryptoServiceProvider (csp);
-// DSACryptoServiceProvider dsa4 = new DSACryptoServiceProvider (1024, csp);
+ // under Mono:: a new key pair isn't generated
+ dsa = new DSACryptoServiceProvider ();
+ // test default key size
+ AssertEquals ("DSA ConstructorEmpty", 1024, dsa.KeySize);
}
- public void TestKeyGeneration ()
+ [Test]
+ public void ConstructorKeySize ()
+ {
+ dsa = new DSACryptoServiceProvider (minKeySize);
+ // test default key size
+ AssertEquals ("DSA ConstructorKeySize", minKeySize, dsa.KeySize);
+ }
+
+ [Test]
+ public void ConstructorCspParameters ()
+ {
+ CspParameters csp = new CspParameters (13, null, "Mono1024");
+ // under MS a new keypair will only be generated the first time
+ dsa = new DSACryptoServiceProvider (csp);
+ // test default key size
+ AssertEquals ("DSA ConstructorCspParameters", 1024, dsa.KeySize);
+ }
+
+ [Test]
+ public void ConstructorKeySizeCspParameters ()
+ {
+ CspParameters csp = new CspParameters (13, null, "Mono512");
+ dsa = new DSACryptoServiceProvider (minKeySize, csp);
+ AssertEquals ("DSA ConstructorCspParameters", minKeySize, dsa.KeySize);
+ }
+
+ [Test]
+ [Ignore ("Much too long (with MS as Mono doesn't generates the keypair unless it need it)")]
+ public void KeyGeneration ()
{
- DSACryptoServiceProvider dsa = new DSACryptoServiceProvider ();
// Test every valid key size
- KeySizes LegalKeySize = dsa.LegalKeySizes[0];
-/* TOO LONG
+ KeySizes LegalKeySize = dsa.LegalKeySizes [0];
for (int i = LegalKeySize.MinSize; i <= LegalKeySize.MaxSize; i += LegalKeySize.SkipSize) {
- DSACryptoServiceProvider key = new DSACryptoServiceProvider (i);
- AssertEquals ("KeySize", i, key.KeySize);
- }*/
- // Test invalid keypair (too small)
- try {
- DSACryptoServiceProvider tooSmallKeyPair = new DSACryptoServiceProvider (384);
- Fail ("Expected CryptographicException but got none");
- }
- catch (CryptographicException) {
- // this is what we expect
- }
- catch (Exception e) {
- Fail ("Expected CryptographicException but got: " + e.ToString ());
- }
- // Test invalid keypair (too big)
- try {
- DSACryptoServiceProvider tooBigKeyPair = new DSACryptoServiceProvider (2048);
- Fail ("Expected CryptographicException but got none");
+ dsa = new DSACryptoServiceProvider (i);
+ AssertEquals ("DSA.KeySize", i, dsa.KeySize);
}
- catch (CryptographicException) {
- // this is what we expect
- }
- catch (Exception e) {
- Fail ("Expected CryptographicException but got: " + e.ToString ());
+ }
+
+ [Test]
+ public void LimitedKeyGeneration ()
+ {
+ // Test smallest valid key size (performance issue)
+ using (dsa = new DSACryptoServiceProvider (minKeySize)) { // MS generates keypair here
+ AssertEquals ("BeforeMonoKeyGeneration.KeySize", minKeySize, dsa.KeySize);
+ byte[] hash = new byte [20];
+ dsa.CreateSignature (hash); // mono generates keypair here
+ AssertEquals ("AfterMonoKeyGeneration.KeySize", minKeySize, dsa.KeySize);
}
+ // here Dispose is called (with true)
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void TooSmallKeyPair ()
+ {
+ dsa = new DSACryptoServiceProvider (384);
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void TooBigKeyPair ()
+ {
+ dsa = new DSACryptoServiceProvider (2048);
+ }
+
+ [Test]
+ public void Properties ()
+ {
+ dsa = new DSACryptoServiceProvider (minKeySize);
+ AssertEquals ("LegalKeySize", 1, dsa.LegalKeySizes.Length);
+ AssertEquals ("LegalKeySize.MinSize", minKeySize, dsa.LegalKeySizes [0].MinSize);
+ AssertEquals ("LegalKeySize.MaxSize", 1024, dsa.LegalKeySizes [0].MaxSize);
+ AssertEquals ("LegalKeySize.SkipSize", 64, dsa.LegalKeySizes [0].SkipSize);
+ AssertNull ("KeyExchangeAlgorithm", dsa.KeyExchangeAlgorithm);
+ AssertEquals ("SignatureAlgorithm", "http://www.w3.org/2000/09/xmldsig#dsa-sha1", dsa.SignatureAlgorithm);
+ dsa.Clear ();
+ AssertEquals ("LegalKeySize(disposed)", 1, dsa.LegalKeySizes.Length);
+ AssertEquals ("LegalKeySize.MinSize(disposed)", minKeySize, dsa.LegalKeySizes [0].MinSize);
+ AssertEquals ("LegalKeySize.MaxSize(disposed)", 1024, dsa.LegalKeySizes [0].MaxSize);
+ AssertEquals ("LegalKeySize.SkipSize(disposed)", 64, dsa.LegalKeySizes [0].SkipSize);
+ AssertNull ("KeyExchangeAlgorithm(disposed)", dsa.KeyExchangeAlgorithm);
+ AssertEquals ("SignatureAlgorithm(disposed)", "http://www.w3.org/2000/09/xmldsig#dsa-sha1", dsa.SignatureAlgorithm);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void CreateSignatureDisposed ()
+ {
+ byte[] hash = new byte [20];
+ disposed.CreateSignature (hash);
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void CreateSignatureInvalidHashLength ()
+ {
+ // Small because MS will generate a new keypair
+ dsa = new DSACryptoServiceProvider (minKeySize);
+ byte[] hash = new byte [19];
+ dsa.CreateSignature (hash);
+ }
+
+ [Test]
+ public void CreateSignature ()
+ {
+ // Small because MS will generate a new keypair
+ dsa = new DSACryptoServiceProvider (minKeySize);
+ byte[] hash = new byte [20];
+ // for Mono, no keypair has yet been generated before calling CreateSignature
+ dsa.CreateSignature (hash);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void SignDataDisposed ()
+ {
+ byte[] data = new byte [20];
+ disposed.SignData (data);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void SignHashDisposed ()
+ {
+ byte[] hash = new byte [20];
+ disposed.SignHash (hash, "SHA1");
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void VerifyDataDisposed ()
+ {
+ byte[] data = new byte [20];
+ byte[] sign = new byte [40];
+ disposed.VerifyData (data, sign);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void VerifyHashDisposed ()
+ {
+ byte[] hash = new byte [20];
+ byte[] sign = new byte [40];
+ disposed.VerifyHash (hash, "SHA1", sign);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void VerifySignatureDisposed ()
+ {
+ byte[] hash = new byte [20];
+ byte[] sign = new byte [40];
+ disposed.VerifySignature (hash, sign);
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void VerifySignatureInvalidHashLength ()
+ {
+ // Small because MS will generate a new keypair
+ dsa = new DSACryptoServiceProvider (minKeySize);
+ byte[] hash = new byte [19];
+ byte[] sign = new byte [40];
+ dsa.VerifySignature (hash, sign);
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void VerifySignatureInvalidSignatureLength ()
+ {
+ dsa = new DSACryptoServiceProvider (minKeySize);
+ byte[] hash = new byte [20];
+ byte[] sign = new byte [39];
+ dsa.VerifySignature (hash, sign);
+ }
+
+ [Test]
+ [Ignore ("Running this test breaks all further DSA tests on MS runtime!")]
+ public void VerifySignatureWithoutKey ()
+ {
+ dsa = new DSACryptoServiceProvider (minKeySize);
+ byte[] hash = new byte [20];
+ byte[] sign = new byte [40];
+ // Mono hasn't generated a keypair - but it's impossible to
+ // verify a signature based on a new just generated keypair
+ Assert ("VerifySignature(WithoutKey)", !dsa.VerifySignature (hash, sign));
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void ImportDisposed ()
+ {
+ disposed.ImportParameters (AllTests.GetKey (false));
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void ExportDisposed ()
+ {
+ DSAParameters param = disposed.ExportParameters (false);
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void DSAImportMissingP ()
+ {
+ DSAParameters input = AllTests.GetKey (false);
+ input.P = null;
+ dsa = new DSACryptoServiceProvider (1024);
+ dsa.ImportParameters (input);
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void DSAImportMissingQ ()
+ {
+ DSAParameters input = AllTests.GetKey (false);
+ input.Q = null;
+ dsa = new DSACryptoServiceProvider (1024);
+ dsa.ImportParameters (input);
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void DSAImportMissingG ()
+ {
+ DSAParameters input = AllTests.GetKey (false);
+ input.G = null;
+ dsa = new DSACryptoServiceProvider (1024);
+ dsa.ImportParameters (input);
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ [Ignore ("MS runtime throws a System.ExecutionEngineException then exit the application")]
+ public void DSAImportMissingY ()
+ {
+ DSAParameters input = AllTests.GetKey (false);
+ input.Y = null;
+ dsa = new DSACryptoServiceProvider (1024);
+ dsa.ImportParameters (input);
+ }
+
+ [Test]
+ public void DSAImportMissingJ ()
+ {
+ DSAParameters input = AllTests.GetKey (false);
+ input.J = null;
+ dsa = new DSACryptoServiceProvider (1024);
+ dsa.ImportParameters (input);
+ AssertEquals ("MissingJ.KeySize", 1024, dsa.KeySize);
+ }
+
+ [Test]
+ public void DSAImportMissingSeed ()
+ {
+ DSAParameters input = AllTests.GetKey (false);
+ input.Seed = null;
+ dsa = new DSACryptoServiceProvider (1024);
+ dsa.ImportParameters (input);
+ AssertEquals ("MissingSeed.KeySize", 1024, dsa.KeySize);
}
// all keypairs generated by CryptoAPI on Windows
// import/export XML keypairs
// so we know that Mono can use keypairs generated by CryptoAPI
- public void TestCapiXmlImportExport ()
+ [Test]
+ public void CapiXmlImportExport ()
{
DSACryptoServiceProvider dsa = new DSACryptoServiceProvider ();
// Validate that we can sign with every keypair and verify the signature
// With Mono this means that we can use CAPI keypair to sign and verify.
// For Windows this doesn't mean much.
- public void TestCapiSignature ()
+ [Test]
+ public void CapiSignature ()
{
DSACryptoServiceProvider dsa = new DSACryptoServiceProvider ();
// Validate that we can verify a signature made with CAPI
// With Mono this means that we can verify CAPI signatures.
// For Windows this doesn't mean much.
- public void TestCapiVerify ()
+ [Test]
+ public void CapiVerify ()
{
byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
DSACryptoServiceProvider dsa = new DSACryptoServiceProvider ();
// import/export XML keypairs
// so we know that Windows (original MS Framework) can use keypairs generated by Mono
- public void TestMonoXmlImportExport ()
+ [Test]
+ public void MonoXmlImportExport ()
{
DSACryptoServiceProvider dsa = new DSACryptoServiceProvider ();
// 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.
- public void TestMonoSignature ()
+ [Test]
+ public void MonoSignature ()
{
DSACryptoServiceProvider dsa = new DSACryptoServiceProvider ();
// Validate that we can verify a signature made with Mono
// With Windows this means that we can verify Mono signatures.
// For Mono this doesn't mean much.
- public void TestMonoVerify ()
+ [Test]
+ public void MonoVerify ()
{
byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
DSACryptoServiceProvider dsa = new DSACryptoServiceProvider ();
protected override void Dispose (bool disposing) {}
}*/
-public class DSATest : TestCase {
+[TestFixture]
+public class DSATest : Assertion {
+
protected DSA dsa;
static string xmlPrivate = "<DSAKeyValue><P>s/Oc0t4gj0NRqkCKi4ynJnOAEukNhjkHJPOzNsHP69kyHMUwZ3AzOkLGYOWlOo2zlYKzSbZygDDI5dCWA5gQF2ZGHEUlWJMgUyHmkybOi44cyHaX9yeGfbnoc3xF9sYgkA3vPUZaJuYMOsBp3pyPdeN8/mLU8n0ivURyP+3Ge9M=</P><Q>qkcTW+Ce0L5k8OGTUMkRoGKDc1E=</Q><G>PU/MeGp6I/FBduuwD9UPeCFzg8Ib9H5osku5nT8AhHTY8zGqetuvHhxbESt4lLz8aXzX0oIiMsusBr6E/aBdooBI36fHwW8WndCmwkB1kv7mhRIB4302UrfvC2KWQuBypfl0++a1whBMCh5VTJYH1sBkFIaVNeUbt5Q6/UdiZVY=</G><Y>shJRUdGxEYxSKM5JVol9HAdQwIK+wF9X4n9SAD++vfZOMOYi+M1yuvQAlQvnSlTTWr7CZPRVAICLgDBbqi9iN+Id60ccJ+hw3pGDfLpJ7IdFPszJEeUO+SZBwf8njGXULqSODs/NTciiX7E07rm+KflxFOg0qtWAhmYLxIkDx7s=</Y><J>AAAAAQ6LSuRiYdsocZ6rgyqIOpE1/uCO1PfEn758Lg2VW6OHJTYHNC30s0gSTG/Jt3oHYX+S8vrtNYb8kRJ/ipgcofGq2Qo/cYKP7RX2K6EJwSfWInhsNMr1JmzuK0lUKkXXXVo15fL8O2/16uEWMg==</J><Seed>uYM5b20luvbuyevi9TXHwekbr5s=</Seed><PgenCounter>4A==</PgenCounter><X>fAOytZttUZFzt/AvwRinmvYKL7E=</X></DSAKeyValue>";
static string xmlPublic = "<DSAKeyValue><P>s/Oc0t4gj0NRqkCKi4ynJnOAEukNhjkHJPOzNsHP69kyHMUwZ3AzOkLGYOWlOo2zlYKzSbZygDDI5dCWA5gQF2ZGHEUlWJMgUyHmkybOi44cyHaX9yeGfbnoc3xF9sYgkA3vPUZaJuYMOsBp3pyPdeN8/mLU8n0ivURyP+3Ge9M=</P><Q>qkcTW+Ce0L5k8OGTUMkRoGKDc1E=</Q><G>PU/MeGp6I/FBduuwD9UPeCFzg8Ib9H5osku5nT8AhHTY8zGqetuvHhxbESt4lLz8aXzX0oIiMsusBr6E/aBdooBI36fHwW8WndCmwkB1kv7mhRIB4302UrfvC2KWQuBypfl0++a1whBMCh5VTJYH1sBkFIaVNeUbt5Q6/UdiZVY=</G><Y>shJRUdGxEYxSKM5JVol9HAdQwIK+wF9X4n9SAD++vfZOMOYi+M1yuvQAlQvnSlTTWr7CZPRVAICLgDBbqi9iN+Id60ccJ+hw3pGDfLpJ7IdFPszJEeUO+SZBwf8njGXULqSODs/NTciiX7E07rm+KflxFOg0qtWAhmYLxIkDx7s=</Y><J>AAAAAQ6LSuRiYdsocZ6rgyqIOpE1/uCO1PfEn758Lg2VW6OHJTYHNC30s0gSTG/Jt3oHYX+S8vrtNYb8kRJ/ipgcofGq2Qo/cYKP7RX2K6EJwSfWInhsNMr1JmzuK0lUKkXXXVo15fL8O2/16uEWMg==</J><Seed>uYM5b20luvbuyevi9TXHwekbr5s=</Seed><PgenCounter>4A==</PgenCounter></DSAKeyValue>";
- protected override void SetUp ()
+ [SetUp]
+ void SetUp ()
{
//dsa = new NonAbstractDSAForUnitTests ();
dsa = new DSACryptoServiceProvider ();
}
- protected override void TearDown () {}
-
public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
AllTests.AssertEquals (msg, array1, array2);
// LAMESPEC: ImportParameters inverse the byte arrays inside DSAParameters !!!
// importing and exporting a DSA key (including private key)
- public void TestDSAImportPrivateExportPrivate()
+ [Test]
+ public void DSAImportPrivateExportPrivate()
{
DSAParameters input = AllTests.GetKey (true);
dsa.ImportParameters (input);
}
// importing and exporting a DSA key (without private key)
- public void TestDSAImportPrivateExportPublic()
+ [Test]
+ public void DSAImportPrivateExportPublic()
{
DSAParameters input = AllTests.GetKey (true);
dsa.ImportParameters (input);
}
// importing and exporting a DSA key (without private key)
- public void TestDSAImportPublicExportPublic()
+ [Test]
+ public void DSAImportPublicExportPublic()
{
DSAParameters input = AllTests.GetKey (false);
dsa.ImportParameters (input);
DSAParameters output = dsa.ExportParameters (false);
AssertEquals ("DSA Import Public Export Public (binary)", AllTests.GetKey (false), output, true);
}
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void FromXmlStringNull ()
+ {
+ dsa.FromXmlString (null);
+ }
}
}
using NUnit.Framework;
using System;
+using System.IO;
using System.Security.Cryptography;
namespace MonoTests.System.Security.Cryptography {
public class RSACryptoServiceProviderTest : Assertion {
protected RSACryptoServiceProvider rsa;
+ protected RSACryptoServiceProvider disposed;
private string sha1OID;
- // one-time initialization
- public RSACryptoServiceProviderTest () : base ()
+ static int minKeySize = 384;
+
+ [SetUp]
+ void Setup ()
{
- sha1OID = CryptoConfig.MapNameToOID ("SHA1");
+ if (disposed == null) {
+ sha1OID = CryptoConfig.MapNameToOID ("SHA1");
+ disposed = new RSACryptoServiceProvider (minKeySize);
+ disposed.FromXmlString ("<RSAKeyValue><Modulus>vtXAf62+o50prNCTiVGTMzdhm4sMjK0QVDkKQLFGu2fJQCULt9NZBab14PiWfG1t</Modulus><Exponent>AQAB</Exponent><P>5y2AHOzIhTChIFzLsgZQAGfy3U8OPwFh</P><Q>01NUVJJv+hhIsnbFiSi24FLRrfr/qYuN</Q><DP>HKLAOdUCyazKaK3V9Yleo448wTkntJpB</DP><DQ>AH5MTxo8arAN02TVlzliG+n1lVtlp2at</DQ><InverseQ>ZpgJwTxSYpT81sQCuVUvX0AYrvSziNIw</InverseQ><D>CStiJYBmsZvincAj5qw5w3M8yGmE/9ls4yv7wenozzC4kZshpI2MuON0d2Z8f4aB</D></RSAKeyValue>");
+ disposed.Clear ();
+ }
}
public void AssertEquals (string msg, byte[] array1, byte[] array2)
[Test]
public void ConstructorKeySize ()
{
- int minimalKeySize = 384;
- rsa = new RSACryptoServiceProvider (minimalKeySize);
+ rsa = new RSACryptoServiceProvider (minKeySize);
// test default key size
- AssertEquals ("ConstructorKeySize", minimalKeySize, rsa.KeySize);
+ AssertEquals ("ConstructorKeySize", minKeySize, rsa.KeySize);
}
[Test]
}
}
+ [Test]
+ public void LimitedKeyGeneration ()
+ {
+ // Test smallest valid key size
+ rsa = new RSACryptoServiceProvider (384); // MS generates keypair here
+ byte[] hash = new byte [20];
+ rsa.SignData (hash, SHA1.Create ()); // mono generates keypair here
+ }
+
[Test]
[ExpectedException (typeof (CryptographicException))]
public void TooSmallKeyPair ()
[Test]
public void Properties ()
{
- rsa = new RSACryptoServiceProvider ();
+ rsa = new RSACryptoServiceProvider (minKeySize);
AssertEquals ("LegalKeySize", 1, rsa.LegalKeySizes.Length);
- AssertEquals ("LegalKeySize.MinSize", 384, rsa.LegalKeySizes [0].MinSize);
+ AssertEquals ("LegalKeySize.MinSize", minKeySize, rsa.LegalKeySizes [0].MinSize);
AssertEquals ("LegalKeySize.MaxSize", 16384, rsa.LegalKeySizes [0].MaxSize);
AssertEquals ("LegalKeySize.SkipSize", 8, rsa.LegalKeySizes [0].SkipSize);
AssertEquals ("KeyExchangeAlgorithm", "RSA-PKCS1-KeyEx", rsa.KeyExchangeAlgorithm);
AssertEquals ("SignatureAlgorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1", rsa.SignatureAlgorithm);
+ rsa.Clear ();
+ AssertEquals ("LegalKeySize(disposed)", 1, rsa.LegalKeySizes.Length);
+ AssertEquals ("LegalKeySize.MinSize(disposed)", minKeySize, rsa.LegalKeySizes [0].MinSize);
+ AssertEquals ("LegalKeySize.MaxSize(disposed)", 16384, rsa.LegalKeySizes [0].MaxSize);
+ AssertEquals ("LegalKeySize.SkipSize(disposed)", 8, rsa.LegalKeySizes [0].SkipSize);
+ AssertEquals ("KeyExchangeAlgorithm(disposed)", "RSA-PKCS1-KeyEx", rsa.KeyExchangeAlgorithm);
+ AssertEquals ("SignatureAlgorithm(disposed)", "http://www.w3.org/2000/09/xmldsig#rsa-sha1", rsa.SignatureAlgorithm);
+ }
+
+ [Test]
+ //[ExpectedException (typeof (ObjectDisposedException))]
+ // LAMESPEC/BUG: Disposed object can still be used (but original keypair seems lost)
+ [ExpectedException (typeof (CryptographicException))]
+ public void DecryptDisposed ()
+ {
+ byte[] encdata = { 0x4C, 0xBF, 0xFD, 0xD9, 0xAD, 0xDB, 0x65, 0x15, 0xB3, 0xE8, 0xE6, 0xD3, 0x22, 0x99, 0x69, 0x56, 0xD3, 0x1F, 0x1D, 0x2A, 0x66, 0x07, 0x00, 0xBB, 0x77, 0x47, 0xB6, 0x6F, 0x8E, 0x3A, 0xBA, 0x37, 0xA3, 0x0F, 0x0A, 0xC8, 0x8D, 0x1F, 0x8D, 0xAB, 0xAC, 0xFD, 0x82, 0x6F, 0x7F, 0x88, 0x3B, 0xA1, 0x0F, 0x9B, 0x4B, 0x8A, 0x27, 0x3B, 0xEC, 0xFF, 0x69, 0x20, 0x57, 0x64, 0xE1, 0xD8, 0x9E, 0x96, 0x7A, 0x53, 0x6A, 0x80, 0x63, 0xB0, 0xEE, 0x84, 0xA7, 0x67, 0x38, 0xA5, 0x30, 0x06, 0xA8, 0xBB, 0x16, 0x77, 0x49, 0x67, 0x0F, 0x90, 0x67, 0xD5, 0xC5, 0x12, 0x92, 0x5A, 0xDA, 0xC3, 0xFD, 0xC4, 0x8A, 0x89, 0x77, 0x79, 0x11, 0xEC, 0x95, 0xF6, 0x6A, 0x3B, 0xAD, 0xA8, 0xDF, 0xA1, 0xB0, 0x51, 0x34, 0xE8, 0xC1, 0x05, 0xB9, 0x09, 0x23, 0x33, 0x2A, 0x3E, 0xE7, 0x6A, 0x77, 0x6F, 0xBD, 0x21 };
+ byte[] data = disposed.Decrypt (encdata, false);
+ int total = 0;
+ for (int i=0; i < data.Length; i++)
+ total += data [i];
+ AssertEquals ("DecryptDisposed", 0, total);
+ }
+
+ [Test]
+ //[ExpectedException (typeof (ObjectDisposedException))]
+ // LAMESPEC/BUG: Disposed object can still be used (but original keypair seems lost)
+ public void EncryptDisposed ()
+ {
+ byte[] data = new byte [20];
+ try {
+ byte[] encdata = disposed.Encrypt (data, false);
+ }
+ catch (ObjectDisposedException) {
+ // on Mono we, indirectly, throw an ObjectDisposedException
+ // because we're calling other public methods to do the job
+ }
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void SignDataDisposed ()
+ {
+ byte[] data = new byte [20];
+ disposed.SignData (data, MD5.Create ());
+ }
+
+ [Test]
+ //[ExpectedException (typeof (ArgumentNullException))]
+ [ExpectedException (typeof (NullReferenceException))]
+ public void SignDataByteArrayNull ()
+ {
+ rsa = new RSACryptoServiceProvider (minKeySize);
+ byte[] data = null; // do not confuse compiler
+ rsa.SignData (data, MD5.Create ());
+ }
+
+ [Test]
+ //[ExpectedException (typeof (ArgumentNullException))]
+ [ExpectedException (typeof (NullReferenceException))]
+ public void SignDataStreamNull ()
+ {
+ rsa = new RSACryptoServiceProvider (minKeySize);
+ Stream data = null; // do not confuse compiler
+ rsa.SignData (data, MD5.Create ());
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void SignHashDisposed ()
+ {
+ byte[] hash = new byte [20];
+ disposed.SignHash (hash, "1.3.14.3.2.26"); // SHA-1
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void SignHashInvalidLength ()
+ {
+ byte[] hash = new byte [19];
+ rsa = new RSACryptoServiceProvider (minKeySize);
+ rsa.SignHash (hash, "1.3.14.3.2.26"); // SHA-1
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void VerifyDataDisposed ()
+ {
+ byte[] data = new byte [20];
+ byte[] sign = new byte [(minKeySize << 3) - 1];
+ disposed.VerifyData (data, SHA1.Create(), sign);
+ }
+
+ [Test]
+ //[ExpectedException (typeof (ArgumentNullException))]
+ [ExpectedException (typeof (NullReferenceException))]
+ public void VerifyDataNullData ()
+ {
+ byte[] sign = new byte [(minKeySize << 3)];
+ rsa = new RSACryptoServiceProvider (minKeySize);
+ rsa.VerifyData (null, SHA1.Create(), sign);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void VerifyDataNullSignature ()
+ {
+ byte[] data = new byte [20];
+ rsa = new RSACryptoServiceProvider (minKeySize);
+ rsa.VerifyData (data, SHA1.Create(), null);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void VerifyHashDisposed ()
+ {
+ byte[] hash = new byte [20];
+ byte[] sign = new byte [(minKeySize << 3) - 1];
+ disposed.VerifyHash (hash, "1.3.14.3.2.26", sign);
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void VerifyHashInvalidHashLength ()
+ {
+ byte[] hash = new byte [19];
+ byte[] sign = new byte [(minKeySize << 3)];
+ rsa = new RSACryptoServiceProvider (minKeySize);
+ rsa.VerifyHash (hash, "1.3.14.3.2.26", sign);
+ // note: invalid signature length doesn't throw an exception (but returns false)
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void VerifyHashNullHash ()
+ {
+ byte[] sign = new byte [(minKeySize << 3)];
+ rsa = new RSACryptoServiceProvider (minKeySize);
+ rsa.VerifyHash (null, "1.3.14.3.2.26", sign);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void VerifyHashNullSignature ()
+ {
+ byte[] hash = new byte [20];
+ rsa = new RSACryptoServiceProvider (minKeySize);
+ rsa.VerifyHash (hash, "1.3.14.3.2.26", null);
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void ImportDisposed ()
+ {
+ disposed.ImportParameters (AllTests.GetRsaKey (false));
+ }
+
+ [Test]
+ [ExpectedException (typeof (ObjectDisposedException))]
+ public void ExportDisposed ()
+ {
+ RSAParameters param = disposed.ExportParameters (false);
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void RSAImportMissingExponent ()
+ {
+ RSAParameters input = AllTests.GetRsaKey (false);
+ input.Exponent = null;
+ rsa = new RSACryptoServiceProvider (minKeySize);
+ rsa.ImportParameters (input);
+ }
+
+ [Test]
+ [ExpectedException (typeof (CryptographicException))]
+ public void RSAImportMissingModulus ()
+ {
+ RSAParameters input = AllTests.GetRsaKey (false);
+ input.Modulus = null;
+ rsa = new RSACryptoServiceProvider (minKeySize);
+ rsa.ImportParameters (input);
}
// all keypairs generated by CryptoAPI on Windows
// basic implementation for tests
public override void ImportParameters (RSAParameters parameters)
{
- rsaParams = parameters;
+ rsaParams = new RSAParameters ();
+ if (parameters.D != null)
+ rsaParams.D = (byte[]) parameters.D.Clone ();
+ if (parameters.DP != null)
+ rsaParams.DP = (byte[]) parameters.DP.Clone ();
+ if (parameters.DQ != null)
+ rsaParams.DQ = (byte[]) parameters.DQ.Clone ();
+ if (parameters.P != null)
+ rsaParams.P = (byte[]) parameters.P.Clone ();
+ if (parameters.Q != null)
+ rsaParams.Q = (byte[]) parameters.Q.Clone ();
+ if (parameters.InverseQ != null)
+ rsaParams.InverseQ = (byte[]) parameters.InverseQ.Clone ();
+ // public key
+ rsaParams.Exponent = (byte[]) parameters.Exponent.Clone ();
+ rsaParams.Modulus = (byte[]) parameters.Modulus.Clone ();
}
// not tested here - but we must implemented all abstract methods
protected override void Dispose (bool disposing) {}
}
-
-public class RSATest : TestCase {
+[TestFixture]
+public class RSATest : Assertion {
protected RSA rsa;
static byte[] rsaModulus = { 0xbb, 0xf8, 0x2f, 0x09, 0x06, 0x82, 0xce, 0x9c, 0x23, 0x38, 0xac, 0x2b, 0x9d, 0xa8, 0x71, 0xf7,
static string xmlPublic = "<RSAKeyValue><Modulus>u/gvCQaCzpwjOKwrnahx9zaNB+7UEEOkQNa28HRU9R+437qvA1wCq2HqSM7rb81Idu1SDWDh7EYZcZ2KW4uAf6+44KPfxzdyPua0t9k6JYTuamSdBglTdIg0skVFmDlO4KqxLXthpR9SeppB9sFof+JTcpjKKo9ZRvjl/Qkdvcs=</Modulus><Exponent>EQ==</Exponent></RSAKeyValue>";
- protected override void SetUp ()
+ [SetUp]
+ void SetUp ()
{
rsa = new NonAbstractRSAForUnitTests ();
}
- protected override void TearDown () {}
-
public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
AllTests.AssertEquals (msg, array1, array2);
}
// importing RSA keypair and exporting a RSA keypair
- public void TestRSAImportPrivateExportPrivate()
+ [Test]
+ public void RSAImportPrivateExportPrivate ()
{
RSAParameters input = GetKey (true);
rsa.ImportParameters (input);
}
// importing RSA keypair and exporting a RSA public key
- public void TestRSAImportPrivateExportPublic()
+ [Test]
+ public void RSAImportPrivateExportPublic ()
{
RSAParameters input = GetKey (true);
rsa.ImportParameters (input);
}
// importing RSA public key and exporting a RSA keypair (including private key!)
- public void TestRSAImportPublicExportPrivate()
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void RSAImportPublicExportPrivate ()
{
RSAParameters input = GetKey (false);
rsa.ImportParameters (input);
- string xmlRSA = null;
- try {
- xmlRSA = rsa.ToXmlString (true);
- Fail ("Expected ArgumentNullException but got no exception");
- }
- catch (ArgumentNullException) {
- // expected as we cannot get a private key
- // from a public key (at least not in our lifetime)
- }
- catch (Exception e) {
- Fail ("Expected ArgumentNullException but got " + e.ToString ());
- }
+ string xmlRSA = rsa.ToXmlString (true);
//rsa.FromXmlString (xmlRSA);
//RSAParameters output = rsa.ExportParameters (true);
//AssertEquals ("RSA Import Public Export Private", input, output, true);
}
// importing RSA public key and exporting a RSA public key
- public void TestRSAImportPublicExportPublic()
+ [Test]
+ public void RSAImportPublicExportPublic ()
{
RSAParameters input = GetKey (false);
rsa.ImportParameters (input);
RSAParameters output = rsa.ExportParameters (false);
AssertEquals ("RSA Import Public Export Public (binary)", input, output, true);
}
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void FromXmlStringNull ()
+ {
+ rsa.FromXmlString (null);
+ }
}
}