Пример #1
0
 private string GetShaFromSamplePackage(string samplePackage)
 {
     using (var packageReader = new PackageArchiveReader(samplePackage))
     {
         PrimarySignature primarySignature = packageReader.GetPrimarySignatureAsync(CancellationToken.None).GetAwaiter().GetResult();
         using (IX509CertificateChain certificateChain = SignatureUtility.GetCertificateChain(primarySignature))
         {
             return(certificateChain.First().GetCertHashString(HashAlgorithmName.SHA256));
         }
     }
 }
        public async Task GetTimestampCertificateChain_WithMismatchedEssCertIdCertificateHash_ReturnsChain(
            SigningCertificateUsage signingCertificateUsage)
        {
            ISigningTestServer testServer = await _fixture.GetSigningTestServerAsync();

            CertificateAuthority rootCa = await _fixture.GetDefaultTrustedCertificateAuthorityAsync();

            var options = new TimestampServiceOptions()
            {
                SigningCertificateUsage  = signingCertificateUsage,
                SigningCertificateV1Hash = new byte[SHA1HashLength]
            };
            TimestampService timestampService = TimestampService.Create(rootCa, options);

            using (testServer.RegisterResponder(timestampService))
            {
                var nupkg = new SimpleTestPackageContext();

                using (var certificate = new X509Certificate2(_fixture.TrustedTestCertificate.Source.Cert))
                    using (var directory = TestDirectory.Create())
                    {
                        var signedPackagePath = await SignedArchiveTestUtility.AuthorSignPackageAsync(
                            certificate,
                            nupkg,
                            directory,
                            timestampService.Url);

                        using (FileStream stream = File.OpenRead(signedPackagePath))
                            using (var reader = new PackageArchiveReader(stream))
                            {
                                PrimarySignature signature = await reader.GetPrimarySignatureAsync(CancellationToken.None);

                                using (IX509CertificateChain actualChain = SignatureUtility.GetTimestampCertificateChain(signature))
                                {
                                    Assert.NotEmpty(actualChain);

                                    IReadOnlyList <Org.BouncyCastle.X509.X509Certificate> expectedChain = GetExpectedCertificateChain(timestampService);

                                    Assert.Equal(expectedChain.Count, actualChain.Count);

                                    for (var i = 0; i < expectedChain.Count; ++i)
                                    {
                                        Org.BouncyCastle.X509.X509Certificate expectedCertificate = expectedChain[i];
                                        X509Certificate2 actualCertificate = actualChain[i];

                                        Assert.True(
                                            expectedCertificate.GetEncoded().SequenceEqual(actualCertificate.RawData),
                                            $"The certificate at index {i} in the chain is unexpected.");
                                    }
                                }
                            }
                    }
            }
        }
Пример #3
0
        private string GetShaFromSamplePackage(string samplePackage)
        {
            var packageReader = new PackageArchiveReader(samplePackage);

            Directory.CreateDirectory(_tempDirectory.Value);
            FilePath targetFilePath = _tempDirectory.WithFile(Path.GetRandomFileName());

            packageReader.ExtractFile(".signature.p7s", targetFilePath.Value, _logger);
            using var fs = new FileStream(targetFilePath.Value, FileMode.Open);
            PrimarySignature      primarySignature = PrimarySignature.Load(fs);
            IX509CertificateChain certificateChain = SignatureUtility.GetCertificateChain(primarySignature);
            var shaFromPackage = certificateChain[0].GetCertHashString(HashAlgorithmName.SHA256);

            return(shaFromPackage);
        }