@@ -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 (
0 commit comments