示例#1
0
        public async Task Timestamp_Verify_WithOfflineRevocation_ReturnsCorrectFlagsAndLogsAsync()
        {
            var nupkg = new SimpleTestPackageContext();

            using (var testServer = await SigningTestServer.CreateAsync())
                using (var responders = new DisposableList <IDisposable>())
                    using (var packageStream = await nupkg.CreateAsStreamAsync())
                        using (var testCertificate = new X509Certificate2(_trustedTestCert.Source.Cert))
                        {
                            CertificateAuthority rootCa         = CertificateAuthority.Create(testServer.Url);
                            CertificateAuthority intermediateCa = rootCa.CreateIntermediateCertificateAuthority();

                            responders.Add(testServer.RegisterResponder(intermediateCa));
                            responders.Add(testServer.RegisterResponder(rootCa));

                            using (var trustedServerRoot = TrustedTestCert.Create(
                                       new X509Certificate2(rootCa.Certificate.GetEncoded()),
                                       StoreName.Root,
                                       StoreLocation.LocalMachine))
                            {
                                var timestampService = TimestampService.Create(intermediateCa);

                                responders.Add(testServer.RegisterResponder(timestampService));

                                var timestampProvider = new Rfc3161TimestampProvider(timestampService.Url);

                                var signature = await SignedArchiveTestUtility.CreateAuthorSignatureForPackageAsync(testCertificate, packageStream, timestampProvider);

                                var timestamp = signature.Timestamps.First();

                                var settings = new SignedPackageVerifierSettings(
                                    allowUnsigned: false,
                                    allowUntrusted: false,
                                    allowIllegal: false,
                                    allowIgnoreTimestamp: false,
                                    allowMultipleTimestamps: false,
                                    allowNoTimestamp: false,
                                    allowUnknownRevocation: false,
                                    reportUnknownRevocation: true,
                                    verificationTarget: VerificationTarget.All,
                                    signaturePlacement: SignaturePlacement.Any,
                                    repositoryCountersignatureVerificationBehavior: SignatureVerificationBehavior.Always,
                                    revocationMode: RevocationMode.Online);

                                var logs = new List <SignatureLog>();

                                var result = timestamp.Verify(signature, settings, HashAlgorithmName.SHA256, logs);

                                result.HasFlag(SignatureVerificationStatusFlags.UnknownRevocation).Should().BeTrue();

                                var errors = logs.Where(l => l.Level == LogLevel.Error);
                                errors.Count().Should().Be(RuntimeEnvironmentHelper.IsWindows ? 2 : 1);

                                if (RuntimeEnvironmentHelper.IsWindows)
                                {
                                    errors.Should().Contain(w => w.Code == NuGetLogCode.NU3028 && w.Message.Contains("The revocation function was unable to check revocation because the revocation server could not be reached."));
                                    errors.Should().Contain(w => w.Code == NuGetLogCode.NU3028 && w.Message.Contains("The revocation function was unable to check revocation for the certificate."));
                                }
                                else
                                {
                                    errors.Should().Contain(w => w.Code == NuGetLogCode.NU3028 && w.Message.Contains("unable to get certificate CRL"));
                                }
                            }
                        }
        }
示例#2
0
        public async Task Restore_PackageWithCompressedSignature_RequireMode_FailsAndDoesNotExpandAsync()
        {
            // Arrange
            var packageX = new SimpleTestPackageContext();

            using (var pathContext = new SimpleTestPathContext())
                using (var packageStream = await packageX.CreateAsStreamAsync())
                    using (var testCertificate = new X509Certificate2(_trustedTestCert.Source.Cert))
                    {
                        AuthorPrimarySignature signature = await SignedArchiveTestUtility.CreateAuthorSignatureForPackageAsync(testCertificate, packageStream);

                        using (var package = new ZipArchive(packageStream, ZipArchiveMode.Update, leaveOpen: true))
                        {
                            var signatureEntry = package.CreateEntry(SigningSpecifications.V1.SignaturePath);
                            using (var signatureStream = new MemoryStream(signature.GetBytes()))
                                using (var signatureEntryStream = signatureEntry.Open())
                                {
                                    signatureStream.CopyTo(signatureEntryStream);
                                }
                        }

                        var packagePath = Path.Combine(pathContext.PackageSource, $"{packageX.ToString()}.nupkg");
                        packageStream.Seek(offset: 0, loc: SeekOrigin.Begin);

                        using (var fileStream = File.OpenWrite(packagePath))
                        {
                            packageStream.CopyTo(fileStream);
                        }

                        // Set up solution, project, and packages
                        var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot);

                        var propsFile = Path.Combine(pathContext.SolutionRoot, "NuGet.Config");

                        using (var stream = File.OpenWrite(propsFile))
                            using (var textWritter = new StreamWriter(stream))
                            {
                                textWritter.Write(@"<configuration><config><add key=""signatureValidationMode"" value=""require"" /></config></configuration>");
                            }

                        var projectA = SimpleTestProjectContext.CreateNETCore(
                            "a",
                            pathContext.SolutionRoot,
                            NuGetFramework.Parse("NETStandard2.0"));

                        projectA.AddPackageToAllFrameworks(packageX);
                        solution.Projects.Add(projectA);
                        solution.Create(pathContext.SolutionRoot);

                        var args = new string[]
                        {
                            projectA.ProjectPath
                        };

                        // Act
                        var result          = RunRestore(_nugetExePath, pathContext, expectedExitCode: 1, additionalArgs: args);
                        var assetFileReader = new LockFileFormat();
                        var assetsFile      = assetFileReader.Read(projectA.AssetsFileOutputPath);
                        var errors          = assetsFile.LogMessages.Where(m => m.Level == LogLevel.Error);
                        var warnings        = assetsFile.LogMessages.Where(m => m.Level == LogLevel.Warning);

                        // Assert
                        result.ExitCode.Should().Be(1);
                        result.Errors.Should().Contain(string.Format(NU3005, SigningTestUtility.AddSignatureLogPrefix(NU3005CompressedMessage, packageX.Identity, pathContext.PackageSource)));

                        errors.Count().Should().Be(1);
                        errors.First().Code.Should().Be(NuGetLogCode.NU3005);
                        errors.First().Message.Should().Be(SigningTestUtility.AddSignatureLogPrefix(NU3005CompressedMessage, packageX.Identity, pathContext.PackageSource));
                        errors.First().LibraryId.Should().Be(packageX.Identity.Id.ToString());

                        warnings.Count().Should().Be(0);

                        var installedPackageDir = Path.Combine(pathContext.UserPackagesFolder, packageX.Identity.Id);
                        Directory.Exists(installedPackageDir).Should().BeFalse();
                    }
        }