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 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 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 TcpSignHashesWithDisposedServiceProtocolTest()
        {
            KsiService service = GetTcpKsiService();

            IAsyncResult ar1 = service.BeginSign(new DataHash(HashAlgorithm.Sha2256, Base16.Decode("1f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")), null,
                                                 null);
            IAsyncResult ar2 = service.BeginSign(new DataHash(HashAlgorithm.Sha2256, Base16.Decode("2f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")), null,
                                                 null);

            service.EndSign(ar1);

            TcpKsiSigningServiceProtocol tcp = GetTcpProtocol(service);

            Assert.IsNotNull(GetSigningSocket(tcp), "Socket should not be null");
            tcp.Dispose();

            Assert.IsNull(GetSigningSocket(tcp), "Socket should be null");

            KsiServiceProtocolException ex = Assert.Throws <KsiServiceProtocolException>(delegate
            {
                tcp.Dispose();
            });

            Assert.That(ex.Message.StartsWith("TCP KSI service protocol is already disposed."), "Unexpected exception message: " + ex.Message);

            ex = Assert.Throws <KsiServiceProtocolException>(delegate
            {
                service.EndSign(ar2);
            });

            Assert.That(ex.Message.StartsWith("TCP KSI service protocol is disposed."), "Unexpected exception message: " + ex.Message);

            ex = Assert.Throws <KsiServiceProtocolException>(delegate
            {
                service.BeginSign(new DataHash(HashAlgorithm.Sha2256, Base16.Decode("3f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")), null, null);
            });

            Assert.That(ex.Message.StartsWith("TCP KSI service protocol is disposed."), "Unexpected exception message: " + ex.Message);
        }
Ejemplo n.º 5
0
        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);
        }
        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");
        }