public void BlockSignerAddWithTreeTooHighTest(Ksi ksi) { BlockSigner blockSigner = new BlockSigner(GetHttpKsiService(), null, null, 1); bool result = blockSigner.Add(new DataHash(Base16.Decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2"))); Assert.IsTrue(result, "Unexpected add method result"); result = blockSigner.Add(new DataHash(Base16.Decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2"))); Assert.IsTrue(result, "Unexpected add method result"); result = blockSigner.Add(new DataHash(Base16.Decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2"))); Assert.IsFalse(result, "Unexpected add method result"); }
public void BlockSignerCannotAddHashAfterSignedTest(Ksi ksi) { BlockSigner blockSigner = new BlockSigner(GetHttpKsiService()); blockSigner.Add(new DataHash(Base16.Decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2"))); blockSigner.Sign(); BlockSigningException ex = Assert.Throws <BlockSigningException>(delegate { blockSigner.Add(new DataHash(Base16.Decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2"))); }); Assert.That(ex.Message, Does.StartWith("Signing process is started. Cannot add new items."), "Unexpected exception message"); }
public void BlockSignerGetUniSignaturesOfManyRandomHashesTest(Ksi ksi) { int k = 7; Random random = new Random(); IdentityMetadata metadata = new IdentityMetadata("test client id", "test machine id"); List <DataHash> hashes = new List <DataHash>(); byte[] buffer = new byte[10]; for (int i = 0; i < k; i++) { IDataHasher hasher = KsiProvider.CreateDataHasher(); random.NextBytes(buffer); hasher.AddData(buffer); hashes.Add(hasher.GetHash()); } BlockSigner blockSigner = new BlockSigner(GetHttpKsiService()); foreach (DataHash hash in hashes) { blockSigner.Add(hash, metadata); } IEnumerable <IKsiSignature> uniSignatures = blockSigner.Sign(); int n = 0; foreach (IKsiSignature signature in uniSignatures) { Verify(ksi, signature, hashes[n++]); } }
public void LinkUserIdToSignature() { BlockSigner ksiBlockSigner = new BlockSigner(GetKsiService()); IDataHasher dh = KsiProvider.CreateDataHasher(HashAlgorithm.Sha2256); // This is the data we are signing string data = "data"; dh.AddData(Encoding.UTF8.GetBytes(data)); // Suppose that this is the user that initiated the signing // and it has been verified using a 3rd party authentication provider (e.g. LDAP) string userId = "john.smith"; // Add both, the data and the user to the block signer ksiBlockSigner.Add(dh.GetHash(), new IdentityMetadata(userId)); IKsiSignature[] signatures = ksiBlockSigner.Sign().ToArray(); // We should get only one signature as we only had one item that we signed Assert.AreEqual(1, signatures.Length); // Print the last part of the identity to show john.smith is there IIdentity[] identity = signatures[0].GetIdentity().ToArray(); Console.WriteLine("User: " + identity[identity.Length - 1].ClientId); // Store the signature as needed // ... }
public void BlockSignerAddWithoutHashTest(Ksi ksi) { BlockSigner blockSigner = new BlockSigner(GetHttpKsiService()); ArgumentNullException ex = Assert.Throws <ArgumentNullException>(delegate { blockSigner.Add(null); }); Assert.AreEqual("dataHash", ex.ParamName, "Unexpected exception message"); }
public void BlockSignerSignOneHashWithBlindingMaskTest(Ksi ksi) { BlockSigner blockSigner = new BlockSigner(GetHttpKsiService(), true, new byte[] { 1, 2, 3 }); DataHash hash = new DataHash(Base16.Decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2")); blockSigner.Add(hash); IEnumerator <IKsiSignature> signatures = blockSigner.Sign().GetEnumerator(); Assert.True(signatures.MoveNext(), "Invalid signature count: 0"); IKsiSignature signature = signatures.Current; Assert.False(signatures.MoveNext(), "Invalid signature count: > 1"); Assert.Less(0, signature.GetAggregationHashChains()[0].GetChainLinks().Count, "Invalid links count."); Assert.IsNull(signature.GetAggregationHashChains()[0].GetChainLinks()[0].Metadata, "Unexpected right sibling type."); VerifyChainAlgorithm(signature, HashAlgorithm.Default); Verify(ksi, signature, hash); }
public void BlockSignerSignOneWithMetadaHashTest(Ksi ksi) { BlockSigner blockSigner = new BlockSigner(GetHttpKsiService()); DataHash hash = new DataHash(Base16.Decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2")); IdentityMetadata metadata = new IdentityMetadata("test client id"); blockSigner.Add(hash, metadata); IEnumerator <IKsiSignature> signatures = blockSigner.Sign().GetEnumerator(); Assert.True(signatures.MoveNext(), "Invalid signature count: 0"); IKsiSignature signature = signatures.Current; Assert.False(signatures.MoveNext(), "Invalid signature count: > 1"); Assert.AreEqual(1, signature.GetAggregationHashChains()[0].GetChainLinks().Count, "Invalid links count."); Assert.AreEqual(metadata.ClientId, signature.GetAggregationHashChains()[0].GetChainLinks()[0].Metadata.ClientId, "Invalid metadata."); VerifyChainAlgorithm(signature, HashAlgorithm.Default); Verify(ksi, signature, hash); }
public void BlockSignerSignOneHashWithLevelTest(Ksi ksi) { BlockSigner blockSigner = new BlockSigner(GetHttpKsiService()); DataHash hash = new DataHash(Base16.Decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2")); blockSigner.Add(hash, null, 2); IEnumerator <IKsiSignature> signatures = blockSigner.Sign().GetEnumerator(); Assert.True(signatures.MoveNext(), "Invalid signature count: 0"); IKsiSignature signature = signatures.Current; Assert.False(signatures.MoveNext(), "Invalid signature count: > 1"); Assert.Less(0, signature.GetAggregationHashChains()[0].GetChainLinks().Count, "Invalid links count."); Assert.AreEqual(Properties.Settings.Default.HttpSigningServiceUser, signature.GetAggregationHashChains()[0].GetChainLinks()[0].Metadata.ClientId, "Unexpected metadata."); Assert.LessOrEqual(2, signature.GetAggregationHashChains()[0].GetChainLinks()[0].LevelCorrection, "Level correction is invalid."); VerifyChainAlgorithm(signature, HashAlgorithm.Default); Verify(ksi, signature, hash); }
public void BlockSignerCustomSignatureFactoryTest(Ksi ksi) { BlockSigner blockSigner = new BlockSigner(GetHttpKsiService(), null, new KsiSignatureFactory( new PublicationBasedVerificationPolicy(), new TestVerificationContext() { UserPublication = new PublicationData("AAAAAA-CVZ2AQ-AANGVK-SV7GJL-36LN65-AVJYZR-6XRZSL-HIMRH3-6GU7WR-YNRY7C-X2XECY-WFQXRB") })); IdentityMetadata metadata = new IdentityMetadata("test client id"); DataHash documentHash = new DataHash(Base16.Decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2")); blockSigner.Add(documentHash, metadata); KsiSignatureInvalidContentException ex = Assert.Throws <KsiSignatureInvalidContentException>(delegate { blockSigner.Sign().GetEnumerator().MoveNext(); }, "Automatic verification should fail."); Assert.That(ex.Message, Does.StartWith("Signature verification failed")); }
public void BlockSignerGetUniSignaturesOfGivenHashesWithLevelTest(Ksi ksi) { BlockSigner blockSigner = new BlockSigner(GetHttpKsiService()); IdentityMetadata metadata = new IdentityMetadata("test client id"); List <DataHash> hashes = new List <DataHash> { new DataHash(Base16.Decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2")), new DataHash(Base16.Decode("018D982C6911831201C5CF15E937514686A2169E2AD57BA36FD92CBEBD99A67E34")), new DataHash(Base16.Decode("0114F9189A45A30D856029F9537FD20C9C7342B82A2D949072AB195D95D7B32ECB")), new DataHash(Base16.Decode("01680192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F1")), new DataHash(Base16.Decode("019D982C6911831201C5CF15E937514686A2169E2AD57BA36FD92CBEBD99A67E32")), new DataHash(Base16.Decode("0124F9189A45A30D856029F9537FD20C9C7342B82A2D949072AB195D95D7B32EC3")), new DataHash(Base16.Decode("0134F9189A45A30D856029F9537FD20C9C7342B82A2D949072AB195D95D7B32EC4")) }; List <uint> levels = new List <uint>() { 1, 2, 3, 0, 4, 2, 2 }; List <bool> hasMetadata = new List <bool>() { true, false, false, false, true, true, true, false }; int i = 0; foreach (DataHash hash in hashes) { blockSigner.Add(hash, hasMetadata[i] ? metadata : null, levels[i++]); } i = 0; foreach (IKsiSignature ksiSignature in blockSigner.Sign()) { VerifyChainAlgorithm(ksiSignature, HashAlgorithm.Default); Verify(ksi, ksiSignature, hashes[i]); i++; } }
public void SignMultipleItemsWithLocalAggregation() { BlockSigner ksiBlockSigner = new BlockSigner(GetKsiService()); int itemCount = 50; // Add the items that need to be signed to the block signer IDataHasher dh = KsiProvider.CreateDataHasher(HashAlgorithm.Sha2256); for (int i = 1; i <= itemCount; i++) { dh.Reset(); dh.AddData(Encoding.UTF8.GetBytes(i.ToString())); ksiBlockSigner.Add(dh.GetHash()); } // Submit the signing request IEnumerable <IKsiSignature> signatures = ksiBlockSigner.Sign(); // Just to illustrate that there are as many signatures as items Assert.AreEqual(itemCount, signatures.Count()); // Store the signatures as needed // ... }
public void BlockSignerGetUniSignaturesOfGivenHashesWithSha512Test(Ksi ksi) { BlockSigner blockSigner = new BlockSigner(GetHttpKsiService(), HashAlgorithm.Sha2512); IdentityMetadata metadata = new IdentityMetadata("test client id"); List <DataHash> hashes = new List <DataHash> { new DataHash(Base16.Decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2")), new DataHash(Base16.Decode("018D982C6911831201C5CF15E937514686A2169E2AD57BA36FD92CBEBD99A67E34")), new DataHash(Base16.Decode("0114F9189A45A30D856029F9537FD20C9C7342B82A2D949072AB195D95D7B32ECB")) }; foreach (DataHash hash in hashes) { blockSigner.Add(hash, metadata); } int i = 0; foreach (IKsiSignature ksiSignature in blockSigner.Sign()) { VerifyChainAlgorithm(ksiSignature, HashAlgorithm.Sha2512); Verify(ksi, ksiSignature, hashes[i++]); } }
public void BlockSignerTreeHeightLimitTest(Ksi ksi) { IdentityMetadata metadata = new IdentityMetadata("test client id", "test machine id"); DataHash hash = new DataHash(Base16.Decode("0114F9189A45A30D856029F9537FD20C9C7342B82A2D949072AB195D95D7B32ECB")); List <HeightTestData> list = new List <HeightTestData>() { new HeightTestData(new List <uint> { 1 }, null, 0, 0), new HeightTestData(new List <uint> { 1, 2, 1 }, null, 3, 2), new HeightTestData(new List <uint> { 1, 2, 2 }, null, 3, 2), new HeightTestData(new List <uint> { 1, 0, 1 }, null, 2, 2), new HeightTestData(new List <uint> { 0, 0, 0 }, new List <bool> { true, false, true }, 2, 2), new HeightTestData(new List <uint> { 2, 1, 1, 1 }, null, 3, 3), new HeightTestData(new List <uint> { 1, 0, 0, 0 }, null, 2, 3), new HeightTestData(new List <uint> { 0, 0, 0, 0, 0 }, new List <bool> { true, true, true, true, true }, 3, 4), new HeightTestData(new List <uint> { 1, 0, 1, 0, 0, 0 }, null, 3, 5), new HeightTestData(new List <uint> { 0, 0, 0, 1, 0, 1, 0 }, null, 3, 5), new HeightTestData(new List <uint> { 0, 0, 0, 0, 0, 0 }, new List <bool> { true, false, true, false, false, false }, 3, 5), new HeightTestData(new List <uint> { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, new List <bool> { false, false, false, false, false, false, false, false, false }, 3, 8), new HeightTestData(new List <uint> { 3, 3 }, new List <bool> { false, false }, 3, 1), new HeightTestData(new List <uint> { 3, 3 }, new List <bool> { true, true }, 3, 0), new HeightTestData(new List <uint> { 0, 3 }, new List <bool> { false, false }, 3, 1), new HeightTestData(new List <uint> { 3, 0 }, new List <bool> { false, false }, 3, 1), new HeightTestData(new List <uint> { 2, 0, 0, 0, 0, 0 }, null, 3, 5), new HeightTestData(new List <uint> { 4, 3, 2, 3, 1, 0, 3, 2, 0, 3 }, null, 6, 9), new HeightTestData(new List <uint> { 4, 3, 2, 3, 1, 0, 3, 2, 0, 1, 0, 0 }, null, 6, 10), new HeightTestData(new List <uint> { 7, 6, 5, 6, 4, 3, 6, 5, 3, 6, 8 }, null, 9, 9), }; for (int index = 0; index < list.Count; index++) { HeightTestData data = list[index]; BlockSigner blockSigner = new BlockSigner(GetHttpKsiService(), null, null, data.MaxHeight); bool success = false; Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("Test row: " + (index + 1)); Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); for (int i = 0; i < data.Levels.Count; i++) { Console.WriteLine("---------------------------------------------------------------------"); Console.WriteLine("Item: " + (i + 1) + "; Level: " + data.Levels[i] + "; Node: " + hash); Console.WriteLine("---------------------------------------------------------------------"); IdentityMetadata meta = data.MetaExists != null && data.MetaExists.Count > i && data.MetaExists[i] ? metadata : null; if (!blockSigner.Add(hash, meta, data.Levels[i])) { success = data.AllowedItemCount == i; Assert.IsTrue(success, "Invalid height calculation. Row: " + (index + 1) + "; Node count: " + (i + 1)); break; } } if (!success) { Assert.Fail("Invalid height calculation. All hashes added. Row: " + (index + 1)); } } }
public void BlockSignerGetUniSignaturesParallelTest(Ksi ksi) { ManualResetEvent waitHandle = new ManualResetEvent(false); int[] treeSizes = new[] { 1, 2, 3, 4, 5 }; int doneCount = 0; int runCount = treeSizes.Length; string errorMessage = null; Random random = new Random(); IdentityMetadata metadata = new IdentityMetadata("test client id"); foreach (int j in treeSizes) { int k = j; Task.Run(() => { Console.WriteLine("Document count: " + k); BlockSigner blockSigner = new BlockSigner(GetHttpKsiService()); List <DataHash> hashes = new List <DataHash>(); byte[] buffer = new byte[10]; for (int i = 0; i < k; i++) { IDataHasher hasher = KsiProvider.CreateDataHasher(); random.NextBytes(buffer); hasher.AddData(buffer); hashes.Add(hasher.GetHash()); } foreach (DataHash hash in hashes) { blockSigner.Add(hash, metadata); } try { int i = 0; foreach (IKsiSignature ksiSignature in blockSigner.Sign()) { Verify(ksi, ksiSignature, hashes[i++]); } } catch (Exception ex) { Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " Error " + k + ". " + ex); if (errorMessage == null) { errorMessage = ex.ToString(); } } finally { doneCount++; Console.WriteLine("Done " + k); if (doneCount == runCount) { waitHandle.Set(); } } }); } Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " Waiting ..."); Assert.IsTrue(waitHandle.WaitOne(20000), "Wait handle timed out."); if (errorMessage != null) { Assert.Fail("ERROR: " + errorMessage); } Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " All done."); }