44using System ;
55using System . IO ;
66using System . Security . Cryptography . X509Certificates ;
7- using System . Security . Principal ;
87using System . Threading ;
98using System . Threading . Tasks ;
109using NuGet . Common ;
1110using NuGet . Packaging . Signing ;
1211using NuGet . Test . Utility ;
13- using Org . BouncyCastle . Asn1 ;
14- using Org . BouncyCastle . Asn1 . X509 ;
15- using Org . BouncyCastle . Crypto . Parameters ;
16- using Org . BouncyCastle . Security ;
17- using Test . Utility . Signing ;
18- using Validation . PackageSigning . ExtractAndValidateSignature . Tests . Support ;
19- using Xunit ;
2012using Xunit . Abstractions ;
21- using GeneralName = Org . BouncyCastle . Asn1 . X509 . GeneralName ;
2213
2314namespace Validation . PackageSigning . ExtractAndValidateSignature . Tests
2415{
16+ using CoreCertificateIntegrationTestFixture = Core . Tests . Support . CertificateIntegrationTestFixture ;
17+
2518 /// <summary>
2619 /// This test fixture trusts the root certificate of checked in signed packages. This handles adding and removing
2720 /// the root certificate from the local machine trusted roots. Any tests with this fixture require admin elevation.
2821 /// </summary>
29- public class CertificateIntegrationTestFixture : IDisposable
22+ public class CertificateIntegrationTestFixture : CoreCertificateIntegrationTestFixture
3023 {
31- private readonly Lazy < Task < SigningTestServer > > _testServer ;
32- private readonly Lazy < Task < CertificateAuthority > > _certificateAuthority ;
33- private readonly Lazy < Task < TimestampService > > _timestampService ;
34- private readonly Lazy < Task < Uri > > _timestampServiceUrl ;
35- private readonly Lazy < Task < X509Certificate2 > > _signingCertificate ;
36- private readonly Lazy < Task < string > > _signingCertificateThumbprint ;
37- private TrustedTestCert < X509Certificate2 > _trustedRoot ;
38- private readonly DisposableList _responders ;
39-
4024 private readonly SemaphoreSlim _lock = new SemaphoreSlim ( 1 ) ;
4125 private byte [ ] _signedPackageBytes1 ;
4226
43- public CertificateIntegrationTestFixture ( )
44- {
45- Assert . True (
46- IsAdministrator ( ) ,
47- "This test must be executing with administrator privileges since it installs a trusted root." ) ;
48-
49- _testServer = new Lazy < Task < SigningTestServer > > ( SigningTestServer . CreateAsync ) ;
50- _certificateAuthority = new Lazy < Task < CertificateAuthority > > ( CreateDefaultTrustedCertificateAuthorityAsync ) ;
51- _timestampService = new Lazy < Task < TimestampService > > ( CreateDefaultTrustedTimestampServiceAsync ) ;
52- _timestampServiceUrl = new Lazy < Task < Uri > > ( CreateDefaultTrustedTimestampServiceUrlAsync ) ;
53- _signingCertificate = new Lazy < Task < X509Certificate2 > > ( CreateDefaultTrustedSigningCertificateAsync ) ;
54- _signingCertificateThumbprint = new Lazy < Task < string > > ( GetDefaultTrustedSigningCertificateThumbprintAsync ) ;
55- _responders = new DisposableList ( ) ;
56- }
57-
5827 public async Task < SignedPackageArchive > GetSignedPackage1Async ( ITestOutputHelper output ) => await GetSignedPackageAsync (
5928 new Reference < byte [ ] > (
6029 ( ) => _signedPackageBytes1 ,
@@ -70,106 +39,6 @@ public async Task<MemoryStream> GetSignedPackageStream1Async(ITestOutputHelper o
7039 await GetSigningCertificateAsync ( ) ,
7140 output ) ;
7241
73- public Task < SigningTestServer > GetTestServerAsync ( ) => _testServer . Value ;
74- public Task < Uri > GetTimestampServiceUrlAsync ( ) => _timestampServiceUrl . Value ;
75- public Task < X509Certificate2 > GetSigningCertificateAsync ( ) => _signingCertificate . Value ;
76- public Task < string > GetSigningCertificateThumbprintAsync ( ) => _signingCertificateThumbprint . Value ;
77-
78- public void Dispose ( )
79- {
80- _trustedRoot ? . Dispose ( ) ;
81- _responders . Dispose ( ) ;
82-
83- if ( _testServer . IsValueCreated )
84- {
85- _testServer . Value . Result . Dispose ( ) ;
86- }
87- }
88-
89- private async Task < CertificateAuthority > CreateDefaultTrustedCertificateAuthorityAsync ( )
90- {
91- var testServer = await GetTestServerAsync ( ) ;
92- var rootCa = CertificateAuthority . Create ( testServer . Url ) ;
93- var intermediateCa = rootCa . CreateIntermediateCertificateAuthority ( ) ;
94- var rootCertificate = new X509Certificate2 ( rootCa . Certificate . GetEncoded ( ) ) ;
95-
96- _trustedRoot = new TrustedTestCert < X509Certificate2 > (
97- rootCertificate ,
98- certificate => certificate ,
99- StoreName . Root ,
100- StoreLocation . LocalMachine ) ;
101-
102- _responders . AddRange ( testServer . RegisterResponders ( intermediateCa ) ) ;
103-
104- return intermediateCa ;
105- }
106-
107- private async Task < TimestampService > CreateDefaultTrustedTimestampServiceAsync ( )
108- {
109- var testServer = await GetTestServerAsync ( ) ;
110- var ca = await _certificateAuthority . Value ;
111- var timestampService = TimestampService . Create ( ca ) ;
112-
113- _responders . Add ( testServer . RegisterResponder ( timestampService ) ) ;
114-
115- return timestampService ;
116- }
117-
118- private async Task < Uri > CreateDefaultTrustedTimestampServiceUrlAsync ( )
119- {
120- var timestampService = await _timestampService . Value ;
121- return timestampService . Url ;
122- }
123-
124- private async Task < X509Certificate2 > CreateDefaultTrustedSigningCertificateAsync ( )
125- {
126- var ca = await _certificateAuthority . Value ;
127- return CreateSigningCertificate ( ca ) ;
128- }
129-
130- public X509Certificate2 CreateSigningCertificate ( CertificateAuthority ca )
131- {
132- var keyPair = SigningTestUtility . GenerateKeyPair ( publicKeyLength : 2048 ) ;
133- var publicCertificate = ca . IssueCertificate (
134- keyPair . Public ,
135- new X509Name ( $ "C=US,ST=WA,L=Redmond,O=NuGet,CN=NuGet Test Signing Certificate ({ Guid . NewGuid ( ) } )") ,
136- generator =>
137- {
138- SigningTestUtility . CertificateModificationGeneratorForCodeSigningEkuCert ( generator ) ;
139-
140- generator . AddExtension (
141- X509Extensions . AuthorityInfoAccess ,
142- critical : false ,
143- extensionValue : new DerSequence (
144- new AccessDescription ( AccessDescription . IdADOcsp ,
145- new GeneralName ( GeneralName . UniformResourceIdentifier , ca . OcspResponderUri . OriginalString ) ) ,
146- new AccessDescription ( AccessDescription . IdADCAIssuers ,
147- new GeneralName ( GeneralName . UniformResourceIdentifier , ca . CertificateUri . OriginalString ) ) ) ) ;
148- } ,
149- notBefore : DateTime . UtcNow . AddSeconds ( - 10 ) ) ;
150-
151- var certificate = new X509Certificate2 ( publicCertificate . GetEncoded ( ) ) ;
152- certificate . PrivateKey = DotNetUtilities . ToRSA ( keyPair . Private as RsaPrivateCrtKeyParameters ) ;
153-
154- return certificate ;
155- }
156-
157- private async Task < string > GetDefaultTrustedSigningCertificateThumbprintAsync ( )
158- {
159- var certificate = await GetSigningCertificateAsync ( ) ;
160- return certificate . ComputeSHA256Thumbprint ( ) ;
161- }
162-
163- /// <summary>
164- /// Source: https://stackoverflow.com/a/11660205
165- /// </summary>
166- private static bool IsAdministrator ( )
167- {
168- var identity = WindowsIdentity . GetCurrent ( ) ;
169- var principal = new WindowsPrincipal ( identity ) ;
170- return principal . IsInRole ( WindowsBuiltInRole . Administrator ) ;
171- }
172-
17342 private async Task < MemoryStream > GetSignedPackageStreamAsync (
17443 Reference < byte [ ] > reference ,
17544 string resourceName ,
0 commit comments