Exemplo n.º 1
0
        public async Task GetTimestampAsync_WhenCancelled_ThrowsAsync()
        {
            var logger           = new TestLogger();
            var timestampService = await _testFixture.GetDefaultTrustedTimestampServiceAsync();

            var timestampProvider = new Rfc3161TimestampProvider(timestampService.Url);
            var content           = new SignatureContent(SigningSpecifications.V1, Common.HashAlgorithmName.SHA256, "Test data to be signed and timestamped");

            using (var authorCert = new X509Certificate2(_trustedTestCert.Source.Cert))
            {
                var timestampHashAlgorithm = Common.HashAlgorithmName.SHA256;
                var signedCms      = SigningTestUtility.GenerateSignedCms(authorCert, content.GetBytes());
                var signature      = PrimarySignature.Load(signedCms.Encode());
                var signatureValue = signature.GetSignatureValue();
                var messageHash    = timestampHashAlgorithm.ComputeHash(signatureValue);

                var request = new TimestampRequest(
                    signingSpecifications: SigningSpecifications.V1,
                    hashedMessage: messageHash,
                    hashAlgorithm: timestampHashAlgorithm,
                    target: SignaturePlacement.PrimarySignature
                    );

                // Assert
                var exception = await Assert.ThrowsAsync <OperationCanceledException>(
                    () => timestampProvider.GetTimestampAsync(request, logger, new CancellationToken(canceled: true)));

                Assert.Equal(OperationCancelledExceptionMessage, exception.Message);
            }
        }
Exemplo n.º 2
0
        public async Task GetTimestampAsync_WithValidInput_ReturnsTimestampAsync()
        {
            var logger           = new TestLogger();
            var timestampService = await _testFixture.GetDefaultTrustedTimestampServiceAsync();

            var timestampProvider = new Rfc3161TimestampProvider(timestampService.Url);
            var content           = new SignatureContent(SigningSpecifications.V1, Common.HashAlgorithmName.SHA256, "Test data to be signed and timestamped");

            using (var authorCert = new X509Certificate2(_trustedTestCert.Source.Cert))
            {
                var signedCms              = SigningTestUtility.GenerateSignedCms(authorCert, content.GetBytes());
                var primarySignature       = PrimarySignature.Load(signedCms.Encode());
                var timestampHashAlgorithm = Common.HashAlgorithmName.SHA256;
                var signatureValue         = primarySignature.GetSignatureValue();
                var messageHash            = timestampHashAlgorithm.ComputeHash(signatureValue);

                var request = new TimestampRequest(
                    signingSpecifications: SigningSpecifications.V1,
                    hashedMessage: messageHash,
                    hashAlgorithm: timestampHashAlgorithm,
                    target: SignaturePlacement.PrimarySignature
                    );

                // Act
                var timestampedCms = await timestampProvider.GetTimestampAsync(request, logger, CancellationToken.None);

                // Assert
                timestampedCms.Should().NotBeNull();
                timestampedCms.Detached.Should().BeFalse();
                timestampedCms.ContentInfo.Should().NotBeNull();
                timestampedCms.SignerInfos.Count.Should().Be(1);
            }
        }
Exemplo n.º 3
0
        public async Task GetTimestampAsync_WhenRequestNull_ThrowsAsync()
        {
            var logger           = new TestLogger();
            var timestampService = await _testFixture.GetDefaultTrustedTimestampServiceAsync();

            var timestampProvider = new Rfc3161TimestampProvider(timestampService.Url);
            var authorCertName    = "*****@*****.**";
            var content           = new SignatureContent(SigningSpecifications.V1, Common.HashAlgorithmName.SHA256, "Test data to be signed and timestamped");

            Action <TestCertificateGenerator> modifyGenerator = delegate(TestCertificateGenerator gen)
            {
                gen.NotBefore = DateTime.UtcNow.Subtract(TimeSpan.FromDays(1)); // cert has expired
            };

            using (var authorCert = SigningTestUtility.GenerateCertificate(authorCertName, modifyGenerator: modifyGenerator))
            {
                var timestampHashAlgorithm = Common.HashAlgorithmName.SHA256;
                var signedCms      = SigningTestUtility.GenerateSignedCms(authorCert, content.GetBytes());
                var signature      = PrimarySignature.Load(signedCms.Encode());
                var signatureValue = signature.GetSignatureValue();
                var messageHash    = timestampHashAlgorithm.ComputeHash(signatureValue);

                var request = new TimestampRequest(
                    signingSpecifications: SigningSpecifications.V1,
                    hashedMessage: messageHash,
                    hashAlgorithm: timestampHashAlgorithm,
                    target: SignaturePlacement.PrimarySignature
                    );

                // Assert
                var exception = await Assert.ThrowsAsync <ArgumentNullException>(
                    () => timestampProvider.GetTimestampAsync(request: null, logger, CancellationToken.None));

                Assert.Equal("request", exception.ParamName);
                Assert.StartsWith("Value cannot be null.", exception.Message);
            }
        }