@@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Certificates.Generation;
2020/// </remarks>
2121internal sealed partial class UnixCertificateManager : CertificateManager
2222{
23- private const UnixFileMode DirectoryPermissions = UnixFileMode . UserRead | UnixFileMode . UserWrite | UnixFileMode . UserExecute ;
23+ private const UnixFileMode DirectoryPermissions = UnixFileMode . UserRead | UnixFileMode . UserWrite | UnixFileMode . UserExecute ;
2424
2525 /// <summary>The name of an environment variable consumed by OpenSSL to locate certificates.</summary>
2626 private const string OpenSslCertificateDirectoryVariableName = "SSL_CERT_DIR" ;
@@ -62,18 +62,32 @@ public override TrustLevel GetTrustLevel(X509Certificate2 certificate)
6262 // Building the chain will check whether dotnet trusts the cert. We could, instead,
6363 // enumerate the Root store and/or look for the file in the OpenSSL directory, but
6464 // 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
7067 {
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+ }
7280 }
73- else
81+ finally
7482 {
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 ( ) ;
7791 }
7892
7993 // 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)
94108 var certPath = Path . Combine ( sslCertDir , certificateNickname + ".pem" ) ;
95109 if ( File . Exists ( certPath ) )
96110 {
97- var candidate = new X509Certificate2 ( certPath ) ;
111+ using var candidate = new X509Certificate2 ( certPath ) ;
98112 if ( AreCertificatesEqual ( certificate , candidate ) )
99113 {
100114 foundCert = true ;
@@ -161,7 +175,7 @@ protected override X509Certificate2 SaveCertificateCore(X509Certificate2 certifi
161175 store . Open ( OpenFlags . ReadWrite ) ;
162176 store . Add ( certificate ) ;
163177 store . Close ( ) ;
164- } ;
178+ }
165179
166180 return certificate ;
167181 }
0 commit comments