コード例 #1
0
        private async Task <IReadOnlyDictionary <string, ParentCertificate> > InitializeParentCertificatesAsync(
            IEnumerable <HashedCertificate> certificates)
        {
            var thumbprints = certificates
                              .Select(x => x.Thumbprint)
                              .Distinct()
                              .ToList();

            // Find all of the parent certificate entities that intersect with the set of certificates found in the
            // package that is currently being processed.
            var existingEntities = await _entitiesContext
                                   .ParentCertificates
                                   .Include(x => x.CertificateChainLinks)
                                   .Where(x => thumbprints.Contains(x.Thumbprint))
                                   .ToListAsync();

            var thumbprintToEntity = existingEntities.ToDictionary(x => x.Thumbprint);

            foreach (var certificate in certificates)
            {
                if (!thumbprintToEntity.TryGetValue(certificate.Thumbprint, out var entity))
                {
                    entity = new ParentCertificate
                    {
                        Thumbprint            = certificate.Thumbprint,
                        CertificateChainLinks = new List <CertificateChainLink>(),
                    };
                    _entitiesContext.ParentCertificates.Add(entity);

                    thumbprintToEntity[certificate.Thumbprint] = entity;
                }
            }

            return(thumbprintToEntity);
        }
コード例 #2
0
            public async Task DoesNotDuplicateWhenSomeDataAlreadyExist()
            {
                // Arrange
                var signature = await TestResources.LoadPrimarySignatureAsync(TestResources.SignedPackageLeaf1);

                var existingParentCertificate = new ParentCertificate
                {
                    Key                   = 1,
                    Thumbprint            = TestResources.RootThumbprint,
                    CertificateChainLinks = new List <CertificateChainLink>(),
                };
                var existingEndCertificate = new EndCertificate
                {
                    Key                   = 1,
                    Thumbprint            = TestResources.Leaf1Thumbprint,
                    Status                = EndCertificateStatus.Good, // Different than the default.
                    Use                   = EndCertificateUse.CodeSigning,
                    CertificateChainLinks = new List <CertificateChainLink>(),
                };
                var existingLink = new CertificateChainLink
                {
                    ParentCertificate    = existingParentCertificate,
                    ParentCertificateKey = existingParentCertificate.Key,
                    EndCertificate       = existingEndCertificate,
                    EndCertificateKey    = existingEndCertificate.Key,
                };

                existingParentCertificate.CertificateChainLinks.Add(existingLink);
                existingEndCertificate.CertificateChainLinks.Add(existingLink);

                var existingPackageSignature = new PackageSignature
                {
                    Key               = 1,
                    EndCertificate    = existingEndCertificate,
                    EndCertificateKey = existingEndCertificate.Key,
                    Status            = PackageSignatureStatus.Valid,
                    CreatedAt         = new DateTime(2017, 1, 1, 8, 30, 0, DateTimeKind.Utc),
                    PackageKey        = _packageKey,
                    Type              = PackageSignatureType.Author,
                    TrustedTimestamps = new List <TrustedTimestamp>(),
                };

                _entitiesContext
                .Setup(x => x.ParentCertificates)
                .Returns(DbSetMockFactory.Create(existingParentCertificate));
                _entitiesContext
                .Setup(x => x.EndCertificates)
                .Returns(DbSetMockFactory.Create(existingEndCertificate));
                _entitiesContext
                .Setup(x => x.CertificateChainLinks)
                .Returns(DbSetMockFactory.Create(existingLink));
                _entitiesContext
                .Setup(x => x.PackageSignatures)
                .Returns(DbSetMockFactory.Create(existingPackageSignature));

                // Act
                await _target.ExtractAsync(_packageKey, signature, _token);

                // Assert
                VerifyExtractedInformation(Leaf1Certificates, Leaf1TimestampValue, PackageSignatureType.Author);
                Assert.Equal(2, _entitiesContext.Object.EndCertificates.Count());
                Assert.Equal(4, _entitiesContext.Object.ParentCertificates.Count());
                Assert.Equal(4, _entitiesContext.Object.CertificateChainLinks.Count());
                Assert.Equal(1, _entitiesContext.Object.PackageSignatures.Count());
                Assert.Equal(1, _entitiesContext.Object.TrustedTimestamps.Count());
                Assert.Equal(EndCertificateStatus.Good, existingEndCertificate.Status);
                Assert.Equal(PackageSignatureStatus.Valid, existingPackageSignature.Status);
            }