@@ -11,68 +11,57 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests
11
11
public class ClientCertificateFixture : IDisposable
12
12
{
13
13
private X509Certificate2 _certificate ;
14
+ private const string _certIssuerPrefix = "CN=IISIntegrationTest_Root" ;
14
15
15
- public X509Certificate2 Certificate
16
+ public X509Certificate2 GetOrCreateCertificate ( )
16
17
{
17
- get
18
+ if ( _certificate != null )
18
19
{
19
- if ( _certificate != null )
20
- {
21
- return _certificate ;
22
- }
23
-
24
- using ( var store = new X509Store ( StoreName . Root , StoreLocation . LocalMachine ) )
25
- {
26
- store . Open ( OpenFlags . ReadWrite ) ;
27
-
28
- foreach ( var cert in store . Certificates )
29
- {
30
- if ( cert . Issuer != "CN=IISIntegrationTest_Root" )
31
- {
32
- continue ;
33
- }
34
- _certificate = cert ;
35
- store . Close ( ) ;
36
- return cert ;
37
- }
20
+ return _certificate ;
21
+ }
38
22
39
- var parentKey = CreateKeyMaterial ( 2048 ) ;
23
+ using ( var store = new X509Store ( StoreName . Root , StoreLocation . LocalMachine ) )
24
+ {
25
+ store . Open ( OpenFlags . ReadWrite ) ;
26
+ var parentKey = CreateKeyMaterial ( 2048 ) ;
40
27
41
- // On first run of the test, creates the certificate in the trusted root certificate authorities.
42
- var parentRequest = new CertificateRequest ( "CN=IISIntegrationTest_Root" , parentKey , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
28
+ // Create a cert name with a random guid to avoid name conflicts
29
+ var parentRequest = new CertificateRequest (
30
+ _certIssuerPrefix + Guid . NewGuid ( ) . ToString ( ) ,
31
+ parentKey , HashAlgorithmName . SHA256 ,
32
+ RSASignaturePadding . Pkcs1 ) ;
43
33
44
- parentRequest . CertificateExtensions . Add (
45
- new X509BasicConstraintsExtension (
46
- certificateAuthority : true ,
47
- hasPathLengthConstraint : false ,
48
- pathLengthConstraint : 0 ,
49
- critical : true ) ) ;
34
+ parentRequest . CertificateExtensions . Add (
35
+ new X509BasicConstraintsExtension (
36
+ certificateAuthority : true ,
37
+ hasPathLengthConstraint : false ,
38
+ pathLengthConstraint : 0 ,
39
+ critical : true ) ) ;
50
40
51
- parentRequest . CertificateExtensions . Add (
52
- new X509KeyUsageExtension ( X509KeyUsageFlags . DigitalSignature | X509KeyUsageFlags . NonRepudiation , critical : true ) ) ;
41
+ parentRequest . CertificateExtensions . Add (
42
+ new X509KeyUsageExtension ( X509KeyUsageFlags . DigitalSignature | X509KeyUsageFlags . NonRepudiation , critical : true ) ) ;
53
43
54
- parentRequest . CertificateExtensions . Add (
55
- new X509SubjectKeyIdentifierExtension ( parentRequest . PublicKey , false ) ) ;
44
+ parentRequest . CertificateExtensions . Add (
45
+ new X509SubjectKeyIdentifierExtension ( parentRequest . PublicKey , false ) ) ;
56
46
57
- var notBefore = DateTimeOffset . Now . AddDays ( - 1 ) ;
58
- var notAfter = DateTimeOffset . Now . AddYears ( 5 ) ;
47
+ var notBefore = DateTimeOffset . Now . AddDays ( - 1 ) ;
48
+ var notAfter = DateTimeOffset . Now . AddYears ( 5 ) ;
59
49
60
- var parentCert = parentRequest . CreateSelfSigned ( notBefore , notAfter ) ;
50
+ var parentCert = parentRequest . CreateSelfSigned ( notBefore , notAfter ) ;
61
51
62
- // Need to export/import the certificate to associate the private key with the cert.
63
- var imported = parentCert ;
52
+ // Need to export/import the certificate to associate the private key with the cert.
53
+ var imported = parentCert ;
64
54
65
- var export = parentCert . Export ( X509ContentType . Pkcs12 , "" ) ;
66
- imported = new X509Certificate2 ( export , "" , X509KeyStorageFlags . PersistKeySet | X509KeyStorageFlags . Exportable ) ;
67
- Array . Clear ( export , 0 , export . Length ) ;
55
+ var export = parentCert . Export ( X509ContentType . Pkcs12 , "" ) ;
56
+ imported = new X509Certificate2 ( export , "" , X509KeyStorageFlags . PersistKeySet | X509KeyStorageFlags . Exportable ) ;
57
+ Array . Clear ( export , 0 , export . Length ) ;
68
58
69
- // Add the cert to the cert store
70
- _certificate = imported ;
59
+ // Add the cert to the cert store
60
+ _certificate = imported ;
71
61
72
- store . Add ( certificate : imported ) ;
73
- store . Close ( ) ;
74
- return imported ;
75
- }
62
+ store . Add ( certificate : imported ) ;
63
+ store . Close ( ) ;
64
+ return imported ;
76
65
}
77
66
}
78
67
@@ -86,7 +75,17 @@ public void Dispose()
86
75
using ( var store = new X509Store ( StoreName . Root , StoreLocation . LocalMachine ) )
87
76
{
88
77
store . Open ( OpenFlags . ReadWrite ) ;
89
- store . Remove ( Certificate ) ;
78
+ store . Remove ( _certificate ) ;
79
+
80
+ // Remove any extra certs that were left by previous tests.
81
+ for ( var i = store . Certificates . Count - 1 ; i >= 0 ; i -- )
82
+ {
83
+ var cert = store . Certificates [ i ] ;
84
+ if ( cert . Issuer . StartsWith ( _certIssuerPrefix ) )
85
+ {
86
+ store . Remove ( cert ) ;
87
+ }
88
+ }
90
89
store . Close ( ) ;
91
90
}
92
91
}
0 commit comments