public void AsyncExtendTest(KsiService service) { ManualResetEvent waitHandle = new ManualResetEvent(false); CalendarHashChain cal = null; object testObject = new object(); bool isAsyncCorrect = false; service.BeginExtend(1455400000, delegate(IAsyncResult ar) { try { isAsyncCorrect = ar.AsyncState == testObject; cal = service.EndExtend(ar); } catch (Exception ex) { Assert.Fail("Unexpected exception: " + ex); } finally { waitHandle.Set(); } }, testObject); Assert.IsTrue(waitHandle.WaitOne(10000), "Wait handle timed out."); Assert.IsNotNull(cal, "Calendar hash chain should not be null."); Assert.AreEqual(true, isAsyncCorrect, "Unexpected async state."); }
public void TcpExtendWithReusedSocketTest() { KsiService service = GetTcpKsiService(); ulong aggregationTime = 1455478441; IAsyncResult ar1 = service.BeginExtend(aggregationTime, null, null); IAsyncResult ar2 = service.BeginExtend(aggregationTime, null, null); CalendarHashChain cal1 = service.EndExtend(ar1); Assert.AreEqual(aggregationTime, cal1.AggregationTime, "Unexpected calendar aggregation time"); CalendarHashChain cal2 = service.EndExtend(ar2); Assert.AreEqual(aggregationTime, cal2.AggregationTime, "Unexpected calendar aggregation time"); Socket socket1 = GetExtendingSocket(service); IAsyncResult ar3 = service.BeginExtend(aggregationTime, null, null); IAsyncResult ar4 = service.BeginExtend(aggregationTime, null, null); CalendarHashChain cal3 = service.EndExtend(ar3); Assert.AreEqual(aggregationTime, cal3.AggregationTime, "Unexpected calendar aggregation time"); CalendarHashChain cal4 = service.EndExtend(ar4); Assert.AreEqual(aggregationTime, cal4.AggregationTime, "Unexpected calendar aggregation time"); Socket socket2 = GetExtendingSocket(service); Assert.AreEqual(socket1, socket2, "Sockets should be equal"); }
public void HttpAsyncGetPublicationsFileTest() { KsiService service = GetHttpKsiService(); ManualResetEvent waitHandle = new ManualResetEvent(false); IPublicationsFile pubFile = null; object testObject = new object(); bool isAsyncCorrect = false; service.BeginGetPublicationsFile(delegate(IAsyncResult ar) { try { isAsyncCorrect = ar.AsyncState == testObject; pubFile = service.EndGetPublicationsFile(ar); } catch (Exception ex) { Assert.Fail("Unexpected exception: " + ex); } finally { waitHandle.Set(); } }, testObject); Assert.IsTrue(waitHandle.WaitOne(10000), "Wait handle timed out."); Assert.IsNotNull(pubFile, "Publications file should not be null."); Assert.AreEqual(true, isAsyncCorrect, "Unexpected async state."); }
public void AsyncExtendWithInvalidPassTest(KsiService service) { ManualResetEvent waitHandle = new ManualResetEvent(false); Exception ex = null; CalendarHashChain cal = null; service.BeginExtend(1455400000, delegate(IAsyncResult ar) { try { cal = service.EndExtend(ar); } catch (Exception e) { ex = e; } finally { waitHandle.Set(); } }, null); Assert.IsTrue(waitHandle.WaitOne(10000), "Wait handle timed out."); Assert.IsNull(cal, "Calendar hash chain should be null."); Assert.IsNotNull(ex, "Exception should not be null."); Assert.AreEqual("Server responded with error message. Status: 258; Message: Failed hmac check.", ex.Message); }
public void TcpSignHashesWithSocketReuseAndTimeoutTest() { KsiService service = GetTcpKsiService(); DataHash hash1 = new DataHash(HashAlgorithm.Sha2256, Base16.Decode("1f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")); DataHash hash2 = new DataHash(HashAlgorithm.Sha2256, Base16.Decode("1f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")); IAsyncResult ar1 = service.BeginSign(hash1, null, null); IKsiSignature sig1 = service.EndSign(ar1); Socket socket1 = GetSigningSocket(service); Assert.AreEqual(hash1, sig1.InputHash, "Unexpected signature input hash"); Socket socket2 = GetSigningSocket(service); Assert.AreEqual(socket1, socket2, "Sockets should be equal"); // after 20 sec server will close connection Thread.Sleep(22000); IAsyncResult ar2 = service.BeginSign(hash2, null, null); IKsiSignature sig2 = service.EndSign(ar2); Assert.AreEqual(hash2, sig2.InputHash, "Unexpected signature input hash"); socket2 = GetSigningSocket(service); Assert.AreNotEqual(socket1, socket2, "Sockets should not be equal"); }
public void AsyncSignWithInvalidPassTest(KsiService service) { byte[] data = Encoding.UTF8.GetBytes("This is my document"); IDataHasher dataHasher = KsiProvider.CreateDataHasher(); dataHasher.AddData(data); DataHash dataHash = dataHasher.GetHash(); ManualResetEvent waitHandle = new ManualResetEvent(false); Exception ex = null; IKsiSignature signature = null; service.BeginSign(dataHash, delegate(IAsyncResult ar) { try { signature = service.EndSign(ar); } catch (Exception e) { ex = e; } finally { waitHandle.Set(); } }, null); Assert.IsTrue(waitHandle.WaitOne(10000), "Wait handle timed out."); Assert.IsNull(signature, "Signature should be null."); Assert.IsNotNull(ex, "Exception should not be null."); Assert.AreEqual("Server responded with error message. Status: 258; Message: The request could not be authenticated.", ex.Message); }
public void TcpSignHashWithReusedSocketTest() { KsiService service = GetTcpKsiService(); DataHash hash1 = new DataHash(HashAlgorithm.Sha2256, Base16.Decode("1f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")); DataHash hash2 = new DataHash(HashAlgorithm.Sha2256, Base16.Decode("1f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")); DataHash hash3 = new DataHash(HashAlgorithm.Sha2256, Base16.Decode("1f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")); DataHash hash4 = new DataHash(HashAlgorithm.Sha2256, Base16.Decode("1f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")); IAsyncResult ar1 = service.BeginSign(hash1, null, null); IAsyncResult ar2 = service.BeginSign(hash2, null, null); IKsiSignature sig1 = service.EndSign(ar1); Assert.AreEqual(hash1, sig1.InputHash, "Unexpected signature input hash"); IKsiSignature sig2 = service.EndSign(ar2); Assert.AreEqual(hash2, sig2.InputHash, "Unexpected signature input hash"); Socket socket1 = GetSigningSocket(service); IAsyncResult ar3 = service.BeginSign(hash3, null, null); IAsyncResult ar4 = service.BeginSign(hash4, null, null); IKsiSignature sig3 = service.EndSign(ar3); Assert.AreEqual(hash3, sig3.InputHash, "Unexpected signature input hash"); IKsiSignature sig4 = service.EndSign(ar4); Assert.AreEqual(hash4, sig4.InputHash, "Unexpected signature input hash"); Socket socket2 = GetSigningSocket(service); Assert.AreEqual(socket1, socket2, "Sockets should be equal"); }
public void TcpExtendesWithSocketReuseAndTimeoutTest() { KsiService service = GetTcpKsiService(); ulong aggregationTime = 1455478441; IAsyncResult ar1 = service.BeginExtend(aggregationTime, null, null); CalendarHashChain cal1 = service.EndExtend(ar1); Socket socket1 = GetExtendingSocket(service); Assert.AreEqual(aggregationTime, cal1.AggregationTime, "Unexpected calendar aggregation time"); Socket socket2 = GetExtendingSocket(service); Assert.AreEqual(socket1, socket2, "Sockets should be equal"); // after 20 sec server will close connection Thread.Sleep(23000); IAsyncResult ar2 = service.BeginExtend(aggregationTime, null, null); CalendarHashChain cal2 = service.EndExtend(ar2); Assert.AreEqual(aggregationTime, cal2.AggregationTime, "Unexpected calendar aggregation time"); socket2 = GetExtendingSocket(service); Assert.AreNotEqual(socket1, socket2, "Sockets should not be equal"); }
public void TcpExtendesWithDisposedServiceProtocolTest() { KsiService service = GetTcpKsiService(); ulong aggregationTime = 1455478441; IAsyncResult ar1 = service.BeginExtend(aggregationTime, null, null); IAsyncResult ar2 = service.BeginExtend(aggregationTime, null, null); service.EndExtend(ar1); TcpKsiExtendingServiceProtocol tcp = GetTcpProtocol(service); Assert.IsNotNull(GetExtendingSocket(tcp), "Socket should not be null"); tcp.Dispose(); Assert.IsNull(GetExtendingSocket(tcp), "Socket should be null"); KsiServiceProtocolException ex = Assert.Throws <KsiServiceProtocolException>(delegate { service.EndExtend(ar2); }); Assert.That(ex.Message.StartsWith("TCP KSI service protocol is disposed."), "Unexpected exception message: " + ex.Message); ex = Assert.Throws <KsiServiceProtocolException>(delegate { service.BeginExtend(aggregationTime, null, null); }); Assert.That(ex.Message.StartsWith("TCP KSI service protocol is disposed."), "Unexpected exception message: " + ex.Message); }
public void AsyncGetExtenderConfigTest(KsiService service) { if (TestSetup.PduVersion == PduVersion.v1) { return; } ManualResetEvent waitHandle = new ManualResetEvent(false); ExtenderConfig config = null; object testObject = new object(); bool isAsyncCorrect = false; service.BeginGetExtenderConfig(delegate(IAsyncResult ar) { try { isAsyncCorrect = ar.AsyncState == testObject; config = service.EndGetExtenderConfig(ar); } finally { waitHandle.Set(); } }, testObject); Assert.IsTrue(waitHandle.WaitOne(10000), "Wait handle timed out."); Assert.IsNotNull(config, "Extender configuration should not be null."); Assert.AreEqual(true, isAsyncCorrect, "Unexpected async state."); }
public void EndGetExtenderConfigWithoutExtendingServiceProtocol() { KsiService service = new KsiService(null, null, null, null, null, null); KsiServiceException ex = Assert.Throws <KsiServiceException>(delegate { service.EndGetExtenderConfig(new TestAsyncResult()); }); Assert.That(ex.Message.StartsWith("Extending service protocol is missing from service"), "Unexpected exception message: " + ex.Message); }
public void EndSignWithSigningServiceProtocolResultNull() { KsiService service = new KsiService(new TestKsiServiceProtocol(), new ServiceCredentials(TestConstants.ServiceUser, TestConstants.ServicePass), null, null, null, null); ArgumentNullException ex = Assert.Throws <ArgumentNullException>(delegate { service.Sign(new DataHash(Base16.Decode("0111A700B0C8066C47ECBA05ED37BC14DCADB238552D86C659342D1D7E87B8772D"))); }); Assert.AreEqual("data", ex.ParamName); }
public void EndGetPublicationsFileWithoutPublicationsFileServiceProtocol() { KsiService service = new KsiService(null, null, null, null, null, null); KsiServiceException ex = Assert.Throws <KsiServiceException>(delegate { service.EndGetPublicationsFile(new TestAsyncResult()); }); Assert.That(ex.Message.StartsWith("Publications file service protocol is missing from service"), "Unexpected exception message: " + ex.Message); }
public void BeginGetAggregatorConfigWithoutSigningServiceProtocol() { KsiService service = new KsiService(null, null, null, null, null, null); KsiServiceException ex = Assert.Throws <KsiServiceException>(delegate { service.BeginGetAggregatorConfig(null, null); }); Assert.That(ex.Message.StartsWith("Signing service protocol is missing from service"), "Unexpected exception message: " + ex.Message); }
public void BeginSignWithoutSigningServiceProtocol() { KsiService service = new KsiService(null, null, null, null, null, null); KsiServiceException ex = Assert.Throws <KsiServiceException>(delegate { service.BeginSign(new DataHash(Base16.Decode("0111A700B0C8066C47ECBA05ED37BC14DCADB238552D86C659342D1D7E87B8772D")), null, null); }); Assert.That(ex.Message.StartsWith("Signing service protocol is missing from service"), "Unexpected exception message: " + ex.Message); }
public void EndGetPublicationsFileInvalidAsyncResultTest() { KsiService service = GetKsiService(); KsiServiceException ex = Assert.Throws <KsiServiceException>(delegate { service.EndGetPublicationsFile(new TestAsyncResult()); }); Assert.That(ex.Message.StartsWith("Invalid asyncResult type:"), "Unexpected exception message: " + ex.Message); }
public void EndGetPublicationsFileWithAsyncResultNullTest() { KsiService service = GetKsiService(); ArgumentNullException ex = Assert.Throws <ArgumentNullException>(delegate { service.EndGetPublicationsFile(null); }); Assert.AreEqual("asyncResult", ex.ParamName); }
public void BeginExtendWithoutExtendingServiceProtocol() { KsiService service = new KsiService(null, null, null, null, null, null); KsiServiceException ex = Assert.Throws <KsiServiceException>(delegate { service.BeginExtend(1, null, null); }); Assert.That(ex.Message.StartsWith("Extending service protocol is missing from service"), "Unexpected exception message: " + ex.Message); }
private static TcpKsiExtendingServiceProtocol GetTcpProtocol(KsiService service) { string fieldName = "_extendingServiceProtocol"; FieldInfo memberInfo = typeof(KsiService).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); if (memberInfo != null) { return((TcpKsiExtendingServiceProtocol)memberInfo.GetValue(service)); } throw new Exception("Could not get field info: " + fieldName); }
public void Verify() { KsiService ksiService = GetHttpKsiService(); VerificationResult result = new DefaultVerificationPolicy().Verify(new VerificationContext(TestUtil.GetSignature()) { IsExtendingAllowed = true, KsiService = ksiService, PublicationsFile = ksiService.GetPublicationsFile() }); Assert.AreEqual(VerificationResultCode.Ok, result.ResultCode, "Unexpected verification result"); }
public void LergacyUseDeprecatedHmacAlgoTest() { KsiService service = GetService(PduVersion.v1, HashAlgorithm.Sha2256, HashAlgorithm.Sha1); HashingException ex = Assert.Throws <HashingException>(delegate { service.Extend(1510056000L); }); Assert.That(ex.Message.StartsWith("Hash algorithm SHA1 is deprecated since 2016-07-01 and can not be used for HMAC"), "Unexpected inner exception message: " + ex.Message); }
public void BeginSignWithHashNullTest() { TestKsiServiceProtocol protocol = new TestKsiServiceProtocol(); KsiService service = new KsiService(protocol, new ServiceCredentials(TestConstants.ServiceUser, TestConstants.ServicePass), null, null, null, null); ArgumentNullException ex = Assert.Throws <ArgumentNullException>(delegate { service.BeginSign(null, null, null); }); Assert.AreEqual("hash", ex.ParamName); }
public void BeginGetExtenderConfigWithoutExtendingServiceCredentials() { TestKsiServiceProtocol protocol = new TestKsiServiceProtocol(); KsiService service = new KsiService(null, null, protocol, null, null, null); KsiServiceException ex = Assert.Throws <KsiServiceException>(delegate { service.BeginGetExtenderConfig(null, null); }); Assert.That(ex.Message.StartsWith("Extending service credentials are missing."), "Unexpected exception message: " + ex.Message); }
public void UseDeprecatedHmacAlgoTest() { KsiService service = GetService(PduVersion.v2, HashAlgorithm.Sha1, HashAlgorithm.Sha2256); Ksi ksi = new Ksi(service); HashingException ex = Assert.Throws <HashingException>(delegate { SignHash(ksi); }); Assert.That(ex.Message.StartsWith("Hash algorithm SHA1 is deprecated since 2016-07-01 and can not be used for HMAC"), "Unexpected inner exception message: " + ex.Message); }
public void EndSignWithoutSigningServiceProtocol() { IKsiService serviceBegin = GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1584727637); IAsyncResult asyncResult = serviceBegin.BeginSign(new DataHash(Base16.Decode("0111A700B0C8066C47ECBA05ED37BC14DCADB238552D86C659342D1D7E87B8772D")), null, null); KsiService serviceEnd = new KsiService(null, null, null, null, null, null); KsiServiceException ex = Assert.Throws <KsiServiceException>(delegate { serviceEnd.EndSign(asyncResult); }); Assert.That(ex.Message.StartsWith("Signing service protocol is missing from service"), "Unexpected exception message: " + ex.Message); }
/// <summary> /// Get extended calendar hash chain from given publication time. /// </summary> /// <param name="publicationTime">publication time</param> /// <returns>extended calendar hash chain</returns> public CalendarHashChain GetExtendedCalendarHashChain(ulong?publicationTime) { if (KsiService == null) { throw new KsiVerificationException("Invalid KSI service in context: null."); } if (Signature == null) { throw new KsiVerificationException("Invalid KSI signature in context: null."); } ulong cacheKey = publicationTime ?? 0; lock (_cacheLock) { if (_calendarHashChainCache == null) { _calendarHashChainCache = new Dictionary <ulong, CalendarHashChain>(); } else if (_calendarHashChainCache.ContainsKey(cacheKey)) { // when getting latest calendar hash chain and last extend is more than 1 sec ago then do not take from cache // otherwise take from cache if (publicationTime != null || _latestCalendarGetTime + 10000000 > DateTime.Now.Ticks) { return(_calendarHashChainCache[cacheKey]); } _calendarHashChainCache.Remove(cacheKey); } } CalendarHashChain calendarHashChain = publicationTime == null ? KsiService.Extend(Signature.AggregationTime) : KsiService.Extend(Signature.AggregationTime, publicationTime.Value); lock (_cacheLock) { _calendarHashChainCache[cacheKey] = calendarHashChain; if (publicationTime == null) { _latestCalendarGetTime = DateTime.Now.Ticks; } } return(calendarHashChain); }
public void SignInvalidPduFormatTest() { KsiService service = GetHttpKsiService(PduVersion.v2); // if new aggregator then no exception try { service.Sign(new DataHash(HashAlgorithm.Sha2256, Base16.Decode("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"))); } catch (Exception ex) { Assert.That(ex.Message.StartsWith("Received PDU v1 response to PDU v2 request. Configure the SDK to use PDU v1 format for the given Aggregator"), "Unexpected exception message: " + ex.Message); } }
public void ExtendInvalidPduFormatTest() { KsiService service = GetHttpKsiService(PduVersion.v2); try { service.Extend(1455494400); } // if new aggregator then no exception catch (Exception ex) { Assert.That(ex.Message.StartsWith("Received PDU v1 response to PDU v2 request. Configure the SDK to use PDU v1 format for the given Extender"), "Unexpected exception message: " + ex.Message); } }
public void AsyncSignHashTest(KsiService service) { byte[] data = Encoding.UTF8.GetBytes("This is my document"); IDataHasher dataHasher = KsiProvider.CreateDataHasher(); dataHasher.AddData(data); DataHash dataHash = dataHasher.GetHash(); ManualResetEvent waitHandle = new ManualResetEvent(false); IKsiSignature signature = null; object testObject = new object(); bool isAsyncStateCorrect = false; service.BeginSign(dataHash, delegate(IAsyncResult ar) { try { isAsyncStateCorrect = ar.AsyncState == testObject; signature = service.EndSign(ar); } catch (Exception ex) { Assert.Fail("Unexpected exception: " + ex); } finally { waitHandle.Set(); } }, testObject); Assert.IsTrue(waitHandle.WaitOne(10000), "Wait handle timed out."); Assert.IsNotNull(signature, "Signature should not be null."); Assert.AreEqual(true, isAsyncStateCorrect, "Unexpected async state."); VerificationContext verificationContext = new VerificationContext(signature) { DocumentHash = dataHash }; InternalVerificationPolicy policy = new InternalVerificationPolicy(); VerificationResult verificationResult = policy.Verify(verificationContext); Assert.AreEqual(VerificationResultCode.Ok, verificationResult.ResultCode, "Signature should verify with internal policy"); }
public void GetExtenderrConfigUsingEventHandlerTest(KsiService service) { service.ExtenderConfigChanged += Service_ExtenderConfigChanged; _waitHandle = new ManualResetEvent(false); if (TestSetup.PduVersion == PduVersion.v1) { Exception ex = Assert.Throws <KsiServiceException>(delegate { service.GetExtenderConfig(); }); Assert.That(ex.Message.StartsWith("Extender config request is not supported using PDU version v1"), "Unexpected exception message: " + ex.Message); return; } service.GetExtenderConfig(); Assert.IsTrue(_waitHandle.WaitOne(10000), "Wait handle timed out."); Assert.IsNotNull(_extenderConfig, "Could not get extender config using event handler."); }