@@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Certificates.Generation;
20
20
/// </remarks>
21
21
internal sealed partial class UnixCertificateManager : CertificateManager
22
22
{
23
- private const UnixFileMode DirectoryPermissions = UnixFileMode . UserRead | UnixFileMode . UserWrite | UnixFileMode . UserExecute ;
23
+ private const UnixFileMode DirectoryPermissions = UnixFileMode . UserRead | UnixFileMode . UserWrite | UnixFileMode . UserExecute ;
24
24
25
25
/// <summary>The name of an environment variable consumed by OpenSSL to locate certificates.</summary>
26
26
private const string OpenSslCertificateDirectoryVariableName = "SSL_CERT_DIR" ;
@@ -62,18 +62,32 @@ public override TrustLevel GetTrustLevel(X509Certificate2 certificate)
62
62
// Building the chain will check whether dotnet trusts the cert. We could, instead,
63
63
// enumerate the Root store and/or look for the file in the OpenSSL directory, but
64
64
// this tests the real-world behavior.
65
- using var chain = new X509Chain ( ) ;
66
- // This is just a heuristic for whether or not we should prompt the user to re-run with `--trust`
67
- // so we don't need to check revocation (which doesn't really make sense for dev certs anyway)
68
- chain . ChainPolicy . RevocationMode = X509RevocationMode . NoCheck ;
69
- if ( chain . Build ( certificate ) )
65
+ var chain = new X509Chain ( ) ;
66
+ try
70
67
{
71
- sawTrustSuccess = true ;
68
+ // This is just a heuristic for whether or not we should prompt the user to re-run with `--trust`
69
+ // so we don't need to check revocation (which doesn't really make sense for dev certs anyway)
70
+ chain . ChainPolicy . RevocationMode = X509RevocationMode . NoCheck ;
71
+ if ( chain . Build ( certificate ) )
72
+ {
73
+ sawTrustSuccess = true ;
74
+ }
75
+ else
76
+ {
77
+ sawTrustFailure = true ;
78
+ Log . UnixNotTrustedByDotnet ( ) ;
79
+ }
72
80
}
73
- else
81
+ finally
74
82
{
75
- sawTrustFailure = true ;
76
- Log . UnixNotTrustedByDotnet ( ) ;
83
+ // Disposing the chain does not dispose the elements we potentially built.
84
+ // Do the full walk manually to dispose.
85
+ for ( var i = 0 ; i < chain . ChainElements . Count ; i ++ )
86
+ {
87
+ chain . ChainElements [ i ] . Certificate . Dispose ( ) ;
88
+ }
89
+
90
+ chain . Dispose ( ) ;
77
91
}
78
92
79
93
// Will become the name of the file on disk and the nickname in the NSS DBs
@@ -94,7 +108,7 @@ public override TrustLevel GetTrustLevel(X509Certificate2 certificate)
94
108
var certPath = Path . Combine ( sslCertDir , certificateNickname + ".pem" ) ;
95
109
if ( File . Exists ( certPath ) )
96
110
{
97
- var candidate = new X509Certificate2 ( certPath ) ;
111
+ using var candidate = new X509Certificate2 ( certPath ) ;
98
112
if ( AreCertificatesEqual ( certificate , candidate ) )
99
113
{
100
114
foundCert = true ;
@@ -161,7 +175,7 @@ protected override X509Certificate2 SaveCertificateCore(X509Certificate2 certifi
161
175
store . Open ( OpenFlags . ReadWrite ) ;
162
176
store . Add ( certificate ) ;
163
177
store . Close ( ) ;
164
- } ;
178
+ }
165
179
166
180
return certificate ;
167
181
}
0 commit comments