public void SignStaticResponseWithWrongRequestIdTest() { // Response has additional unknown non-ciritcal payload. Ksi ksi = GetStaticKsi(Resources.KsiService_AggregationResponsePdu_RequestId_1584727637, 1234567890); KsiServiceException ex = Assert.Throws <KsiServiceException>(delegate { ksi.Sign(new DataHash(Base16.Decode("019f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"))); }); Assert.That(ex.Message.StartsWith("Invalid response PDU. Could not find a valid payload."), "Unexpected exception message: " + ex.Message); }
public void SignStaticResponseHasOnlyUnknownNonCriticalPayloadTest() { // Response has only unknown non-ciritcal payload. Ksi ksi = GetStaticKsi(Resources.KsiService_AggregationResponseUnknownNonCriticalPayload, 1234567890); KsiServiceException ex = Assert.Throws <KsiServiceException>(delegate { ksi.Sign(new DataHash(Base16.Decode("019f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"))); }); Assert.That(ex.Message.StartsWith("Could not parse response message"), "Unexpected exception message: " + ex.Message); }
public void ParallelSigningTest(Ksi ksi) { ManualResetEvent waitHandle = new ManualResetEvent(false); int doneCount = 0; int runCount = 7; string errorMessage = null; for (int i = 0; i < runCount; i++) { Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " Start " + i); int k = i; long start = DateTime.Now.Ticks; new Thread(() => { try { Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " Start signing " + k); ksi.Sign(new DataHash(HashAlgorithm.Sha2256, Base16.Decode("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"))); } catch (Exception ex) { Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " Error " + k + ". " + ex); if (errorMessage == null) { errorMessage = ex.ToString(); } } finally { Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "\t Done! " + k + "\t It took: " + (DateTime.Now.Ticks - start) / 10000 + " ms"); doneCount++; if (doneCount == runCount) { waitHandle.Set(); } } }).Start(); } 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."); }
public void LegacySignStaticInvalidSignatureTest() { Ksi ksi = GetStaticKsi(Resources.KsiService_LegacyAggregationResponsePdu, 318748698, null, PduVersion.v1); KsiSignatureInvalidContentException ex = Assert.Throws <KsiSignatureInvalidContentException>(delegate { ksi.Sign(new DataHash(Base16.Decode("01A1A700B0C8066C47ECBA05ED37BC14DCADB238552D86C659342D1D7E87B8772D"))); }); Assert.That(ex.Message.StartsWith("Signature verification failed"), "Unexpected exception message: " + ex.Message); Assert.IsNotNull(ex.Signature); Assert.AreEqual(VerificationError.Gen01.Code, ex.VerificationResult.VerificationError.Code); }
public void SignStaticStreamEmptyTest() { Ksi ksi = GetStaticKsi(Resources.KsiService_AggregationResponsePdu_SignedZeroBytes, 6607061513599596791); IKsiSignature signature; using (MemoryStream stream = new MemoryStream()) { signature = ksi.Sign(stream); } byte[] documentBytes = new byte[] { }; Verify(signature, KsiProvider.CreateDataHasher(HashAlgorithm.Default).AddData(documentBytes).GetHash()); }
public void SignStaticStreamClosedTest() { Ksi ksi = GetStaticKsi(Resources.KsiService_AggregationResponsePdu_SignedZeroBytes, 6607061513599596791); MemoryStream stream = new MemoryStream(); stream.Close(); ObjectDisposedException ex = Assert.Throws <ObjectDisposedException>(delegate { ksi.Sign(stream); }); Assert.That(ex.Message.StartsWith("Cannot access a closed Stream."), "Unexpected exception message: " + ex.Message); }
private VerificationResult SignHash(Ksi ksi) { DataHash hash = new DataHash(HashAlgorithm.Sha2256, Base16.Decode("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")); IKsiSignature signature = ksi.Sign(hash); VerificationContext verificationContext = new VerificationContext(signature) { DocumentHash = hash, PublicationsFile = ksi.GetPublicationsFile() }; KeyBasedVerificationPolicy policy = new KeyBasedVerificationPolicy(); return(policy.Verify(verificationContext)); }
public void HttpSignSm3HashTest(Ksi ksi) { DataHash hash = new DataHash(HashAlgorithm.Sm3, Base16.Decode("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")); IKsiSignature signature = ksi.Sign(hash); VerificationContext verificationContext = new VerificationContext(signature) { DocumentHash = hash, PublicationsFile = ksi.GetPublicationsFile() }; KeyBasedVerificationPolicy policy = new KeyBasedVerificationPolicy(); VerificationResult verificationResult = policy.Verify(verificationContext); Assert.AreEqual(VerificationResultCode.Ok, verificationResult.ResultCode, "Signature should verify with key based policy"); }
public void SignByteArrayTest(Ksi ksi) { byte[] data = Encoding.UTF8.GetBytes("This is my document"); IKsiSignature signature = ksi.Sign(data); VerificationContext verificationContext = new VerificationContext(signature) { DocumentHash = new DataHash(HashAlgorithm.Sha2256, Base16.Decode("D439459856BEF5ED25772646F73A70A841FC078D3CBBC24AB7F47C464683768D")), PublicationsFile = ksi.GetPublicationsFile() }; KeyBasedVerificationPolicy policy = new KeyBasedVerificationPolicy(); VerificationResult verificationResult = policy.Verify(verificationContext); Assert.AreEqual(VerificationResultCode.Ok, verificationResult.ResultCode, "Signature should verify with key based policy"); }
public void HttpSignHashWithLevelTest(Ksi ksi) { DataHash documentHash = new DataHash(HashAlgorithm.Sha2256, Base16.Decode("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")); IKsiSignature signature = ksi.Sign(documentHash, 3); Assert.LessOrEqual(3, signature.GetAggregationHashChains()[0].GetChainLinks()[0].LevelCorrection, "Level correction is invalid."); VerificationContext verificationContext = new VerificationContext(signature) { DocumentHash = documentHash, PublicationsFile = ksi.GetPublicationsFile() }; KeyBasedVerificationPolicy policy = new KeyBasedVerificationPolicy(); VerificationResult verificationResult = policy.Verify(verificationContext); Assert.AreEqual(VerificationResultCode.Ok, verificationResult.ResultCode, "Signature should verify with key based policy"); }
public void SignWithStreamTest(Ksi ksi) { IKsiSignature signature; using (MemoryStream stream = new MemoryStream()) { byte[] data = Encoding.UTF8.GetBytes("This is my document"); stream.Write(data, 0, data.Length); stream.Seek(0, SeekOrigin.Begin); signature = ksi.Sign(stream); } VerificationContext verificationContext = new VerificationContext(signature) { DocumentHash = new DataHash(HashAlgorithm.Sha2256, Base16.Decode("D439459856BEF5ED25772646F73A70A841FC078D3CBBC24AB7F47C464683768D")), PublicationsFile = ksi.GetPublicationsFile() }; KeyBasedVerificationPolicy policy = new KeyBasedVerificationPolicy(); VerificationResult verificationResult = policy.Verify(verificationContext); Assert.AreEqual(VerificationResultCode.Ok, verificationResult.ResultCode, "Signature should verify with key based policy"); }
public void VerifySignedHashWithInvalidHashTest(Ksi ksi) { VerificationResult verificationResult; using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes("test"))) { IDataHasher dataHasher = CryptoTestFactory.CreateDataHasher(HashAlgorithm.Sha2256); dataHasher.AddData(memoryStream); IKsiSignature signature = ksi.Sign(dataHasher.GetHash()); VerificationContext verificationContext = new VerificationContext(signature) { DocumentHash = new DataHash(HashAlgorithm.Sha2256, Base16.Decode("1f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")), PublicationsFile = ksi.GetPublicationsFile() }; KeyBasedVerificationPolicy policy = new KeyBasedVerificationPolicy(); verificationResult = policy.Verify(verificationContext); } Assert.AreEqual(VerificationResultCode.Fail, verificationResult.ResultCode, "Invalid hash should not verify with key based policy"); Assert.AreEqual(VerificationError.Gen01, verificationResult.VerificationError); }