Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit 33df23d

Browse files
committed
Use local TSA instead of remote TSA (#320)
Progress on NuGet/Engineering#878
1 parent d436073 commit 33df23d

3 files changed

Lines changed: 61 additions & 11 deletions

File tree

src/Validation.PackageSigning.ExtractAndValidateSignature/Validation.PackageSigning.ExtractAndValidateSignature.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
<Version>1.1.2</Version>
107107
</PackageReference>
108108
<PackageReference Include="NuGet.Packaging">
109-
<Version>4.7.0-preview1-4870</Version>
109+
<Version>4.7.0-preview1-4883</Version>
110110
</PackageReference>
111111
<PackageReference Include="NuGet.Services.Configuration">
112112
<Version>2.12.0</Version>

tests/Validation.PackageSigning.ExtractAndValidateSignature.Tests/Support/CertificateIntegrationTestFixture.cs

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ namespace Validation.PackageSigning.ExtractAndValidateSignature.Tests
2222
/// </summary>
2323
public class CertificateIntegrationTestFixture : IDisposable
2424
{
25-
private static readonly string _testTimestampServer = Environment.GetEnvironmentVariable("TIMESTAMP_SERVER_URL");
25+
private readonly Lazy<Task<SigningTestServer>> _testServer;
26+
private readonly Lazy<Task<CertificateAuthority>> _defaultTrustedCertificateAuthority;
27+
private readonly Lazy<Task<TimestampService>> _defaultTrustedTimestampService;
28+
private TrustedTestCert<X509Certificate2> _trustedTimestampRoot;
29+
private readonly DisposableList _responders;
2630

2731
private readonly SemaphoreSlim _lock = new SemaphoreSlim(1);
2832
private byte[] _signedPackageBytes1;
@@ -37,10 +41,16 @@ public CertificateIntegrationTestFixture()
3741
.Generate(SigningTestUtility.CertificateModificationGeneratorForCodeSigningEkuCert)
3842
.WithPrivateKeyAndTrust(StoreName.Root, StoreLocation.LocalMachine);
3943
LeafCertificate1Thumbprint = LeafCertificate1.TrustedCert.ComputeSHA256Thumbprint();
44+
45+
_testServer = new Lazy<Task<SigningTestServer>>(SigningTestServer.CreateAsync);
46+
_defaultTrustedCertificateAuthority = new Lazy<Task<CertificateAuthority>>(CreateDefaultTrustedCertificateAuthorityAsync);
47+
_defaultTrustedTimestampService = new Lazy<Task<TimestampService>>(CreateDefaultTrustedTimestampServiceAsync);
48+
_responders = new DisposableList();
4049
}
4150

4251
public TrustedTestCert<TestCertificate> LeafCertificate1 { get; }
4352
public string LeafCertificate1Thumbprint { get; }
53+
4454
public Task<SignedPackageArchive> GetSignedPackage1Async(ITestOutputHelper output) => GetSignedPackageAsync(
4555
new Reference<byte[]>(
4656
() => _signedPackageBytes1,
@@ -59,6 +69,51 @@ public Task<MemoryStream> GetSignedPackageStream1Async(ITestOutputHelper output)
5969
public void Dispose()
6070
{
6171
LeafCertificate1?.Dispose();
72+
73+
_trustedTimestampRoot?.Dispose();
74+
_responders.Dispose();
75+
76+
if (_testServer.IsValueCreated)
77+
{
78+
_testServer.Value.Result.Dispose();
79+
}
80+
}
81+
82+
private async Task<CertificateAuthority> CreateDefaultTrustedCertificateAuthorityAsync()
83+
{
84+
var testServer = await _testServer.Value;
85+
var rootCa = CertificateAuthority.Create(testServer.Url);
86+
var intermediateCa = rootCa.CreateIntermediateCertificateAuthority();
87+
var rootCertificate = new X509Certificate2(rootCa.Certificate.GetEncoded());
88+
89+
_trustedTimestampRoot = new TrustedTestCert<X509Certificate2>(
90+
rootCertificate,
91+
certificate => certificate,
92+
StoreName.Root,
93+
StoreLocation.LocalMachine);
94+
95+
var ca = intermediateCa;
96+
97+
while (ca != null)
98+
{
99+
_responders.Add(testServer.RegisterResponder(ca));
100+
_responders.Add(testServer.RegisterResponder(ca.OcspResponder));
101+
102+
ca = ca.Parent;
103+
}
104+
105+
return intermediateCa;
106+
}
107+
108+
private async Task<TimestampService> CreateDefaultTrustedTimestampServiceAsync()
109+
{
110+
var testServer = await _testServer.Value;
111+
var ca = await _defaultTrustedCertificateAuthority.Value;
112+
var timestampService = TimestampService.Create(ca);
113+
114+
_responders.Add(testServer.RegisterResponder(timestampService));
115+
116+
return timestampService;
62117
}
63118

64119
/// <summary>
@@ -109,17 +164,12 @@ await GetSignedPackageStreamAsync(reference, resourceName, certificate, output),
109164
new MemoryStream());
110165
}
111166

112-
private static async Task<byte[]> GenerateSignedPackageBytesAsync(string resourceName, TrustedTestCert<TestCertificate> certificate, ITestOutputHelper output)
167+
private async Task<byte[]> GenerateSignedPackageBytesAsync(string resourceName, TrustedTestCert<TestCertificate> certificate, ITestOutputHelper output)
113168
{
114-
if (string.IsNullOrWhiteSpace(_testTimestampServer))
115-
{
116-
Assert.False(
117-
string.IsNullOrWhiteSpace(_testTimestampServer),
118-
"You must set a TIMESTAMP_SERVER_URL environment variable to an accessible timestamping authority URL.");
119-
}
169+
var timestampService = await _defaultTrustedTimestampService.Value;
120170

121171
var testLogger = new TestLogger(output);
122-
var timestampProvider = new Rfc3161TimestampProvider(new Uri(_testTimestampServer));
172+
var timestampProvider = new Rfc3161TimestampProvider(timestampService.Url);
123173
var signatureProvider = new X509SignatureProvider(timestampProvider);
124174

125175
var unsignedBytes = await OperateOnSignerAsync(

tests/Validation.PackageSigning.ExtractAndValidateSignature.Tests/Validation.PackageSigning.ExtractAndValidateSignature.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<Version>1.8.1.3</Version>
6161
</PackageReference>
6262
<PackageReference Include="Test.Utility">
63-
<Version>4.7.0-preview1-4870</Version>
63+
<Version>4.7.0-preview1-4883</Version>
6464
</PackageReference>
6565
<PackageReference Include="xunit">
6666
<Version>2.3.1</Version>

0 commit comments

Comments
 (0)