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 Task Handle(ClaimAddedNotification notification, CancellationToken cancellationToken) { return(Task.Run(() => { var claim = notification.Claim; if (claim.Issuer.Id != claim.Subject.Id) { return; // Not the same issuer and subject } if (!PackageBuilder.ALIAS_IDENTITY_DTP1.EqualsIgnoreCase(claim.Type)) { return; } var metadataId = claim.Issuer.Id; var entry = _trustDBContext.IdentityMetadata.Find(metadataId); if (entry == null) { entry = new IdentityMetadata { Id = metadataId, Title = claim.Value }; _trustDBContext.Add(entry); } else { entry.Title = claim.Value; } // The entry will be updated in database by a SaveChanges() some where else. })); }
private void ValidateIdentityMetadata(string name, SubjectIdentity subject, Claim claim, string location) { IdentityMetadata metadata = subject.Meta; if (metadata == null) { return; } result.MaxRangeCheck($"{name} Title", metadata.Title, location, SchemaValidationResult.DEFAULT_MAX_LENGTH); result.MaxRangeCheck($"{name} Data", metadata.Data, location, SchemaValidationResult.MAX_URL_LENGTH); //result.MaxRangeCheck($"{name} Href", metadata.Href, location, SchemaValidationResult.MAX_URL_LENGTH); result.MaxRangeCheck($"{name} Type", metadata.Type, location, SchemaValidationResult.LENGTH20); var data = metadata.Title.ToBytes().Combine(metadata.Data?.ToBytes()); var hash = Hashes.Hash160(data); var prefix = new byte[] { 30 }; var predixData = prefix.Combine(hash.ToBytes()); var address = Encoders.Base58Check.EncodeData(predixData); if (address != subject.Id) { result.Errors.Add(string.Format("{0}{1} data hash {2} do not match Subject ID: {3}.", location, $"{name} Data", address, subject.Meta)); } }
private void ValidateIdentityMetadata(string name, SubjectIdentity subject, Claim claim, string location) { IdentityMetadata meta = subject.Meta; if (meta == null) { return; } result.MaxRangeCheck($"{name} Title", meta.Title, location, SchemaValidationResult.DEFAULT_TITLE_LENGTH); result.MaxRangeCheck($"{name} Data", meta.Data, location, SchemaValidationResult.DEFAULT_CONTENT_LENGTH); //result.MaxRangeCheck($"{name} Href", metadata.Href, location, SchemaValidationResult.MAX_URL_LENGTH); result.MaxRangeCheck($"{name} Type", meta.Type, location, SchemaValidationResult.LENGTH20); result.MaxRangeCheck($"{name} Icon", meta.Icon, location, SchemaValidationResult.DEFAULT_URL_LENGTH); if (meta.Type == "url") { var eb = meta.EntityID?.ToBytes(); var mt = SanitizeContent(meta.Title).ToBytes(); var md = meta.Data?.ToBytes(); var td = mt.Combine(md); var ff = eb.Combine(td); var contentId = GetDTPaddress(ff); if (contentId != subject.Id) { result.Errors.Add(string.Format("{0}{1} data hash {2} do not match Subject ID: {3}.", location, $"{name} Data", contentId, subject.Meta.Id)); } } if (meta.Type == "alias") { result.Errors.Add(string.Format("{0}{1} - meta type {2} is not allowed.", location, $"{name}", meta.Type)); //var contentId = GetDTPaddress(meta.EntityID?.ToBytes().Combine(meta.Title?.ToBytes())); //if (contentId != subject.Id) //{ // result.Errors.Add(string.Format("{0}{1} hash of EntityID + Title = {2} do not match Subject ID: {3}.", location, $"{name}", contentId, subject.Meta.Id)); //} } if (meta.Type == "content") { var eb = meta.EntityID?.ToBytes(); var mt = SanitizeContent(meta.Title).ToBytes(); var md = SanitizeContent(meta.Data).ToBytes(); var td = mt.Combine(md); var ff = eb.Combine(td); var contentId = GetDTPaddress(ff); if (contentId != subject.Id) { result.Errors.Add(string.Format("{0}{1} hash of EntityID + Title + Data = {2} do not match Subject ID: {3}.", location, $"{name}", contentId, subject.Meta.Id)); } } }
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); }
private void UpdateEntry(Claim claim, Action <IdentityMetadata> callback) { var metadataId = claim.Issuer.Id; var entry = _trustDBContext.IdentityMetadata.Find(metadataId); if (entry == null) { entry = new IdentityMetadata { Id = metadataId, }; callback(entry); _trustDBContext.Add(entry); } else { callback(entry); } }
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 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."); }