@@ -283,6 +283,8 @@ internal bool PostgresCancellationPerformed
283
283
internal bool AttemptPostgresCancellation { get ; private set ; }
284
284
static readonly TimeSpan _cancelImmediatelyTimeout = TimeSpan . FromMilliseconds ( - 1 ) ;
285
285
286
+ X509Certificate2 ? _certificate ;
287
+
286
288
internal NpgsqlLoggingConfiguration LoggingConfiguration { get ; }
287
289
288
290
internal ILogger ConnectionLogger { get ; }
@@ -756,7 +758,6 @@ async ValueTask<string> GetUsernameAsyncInternal()
756
758
757
759
async Task RawOpen( SslMode sslMode , NpgsqlTimeout timeout , bool async , CancellationToken cancellationToken , bool isFirstAttempt = true)
758
760
{
759
- var cert = default ( X509Certificate2 ? ) ;
760
761
try
761
762
{
762
763
if ( async)
@@ -815,15 +816,15 @@ async Task RawOpen(SslMode sslMode, NpgsqlTimeout timeout, bool async, Cancellat
815
816
#if NET5_0_OR_GREATER
816
817
// It's PEM time
817
818
var keyPath = Settings . SslKey ?? PostgresEnvironment . SslKey ?? PostgresEnvironment . SslKeyDefault ;
818
- cert = string . IsNullOrEmpty ( password )
819
+ _certificate = string . IsNullOrEmpty ( password )
819
820
? X509Certificate2 . CreateFromPemFile ( certPath , keyPath )
820
821
: X509Certificate2 . CreateFromEncryptedPemFile ( certPath , password , keyPath ) ;
821
822
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
822
823
{
823
824
// Windows crypto API has a bug with pem certs
824
825
// See #3650
825
- using var previousCert = cert ;
826
- cert = new X509Certificate2 ( cert . Export ( X509ContentType . Pkcs12 ) ) ;
826
+ using var previousCert = _certificate ;
827
+ _certificate = new X509Certificate2 ( _certificate . Export ( X509ContentType . Pkcs12 ) ) ;
827
828
}
828
829
#else
829
830
// Technically PEM certificates are supported as of .NET 5 but we don't build for the net5.0
@@ -833,8 +834,8 @@ async Task RawOpen(SslMode sslMode, NpgsqlTimeout timeout, bool async, Cancellat
833
834
#endif
834
835
}
835
836
836
- cert ??= new X509Certificate2 ( certPath , password ) ;
837
- clientCertificates. Add ( cert ) ;
837
+ _certificate ??= new X509Certificate2 ( certPath , password ) ;
838
+ clientCertificates. Add ( _certificate ) ;
838
839
}
839
840
840
841
ClientCertificatesCallback? . Invoke ( clientCertificates ) ;
@@ -851,10 +852,10 @@ async Task RawOpen(SslMode sslMode, NpgsqlTimeout timeout, bool async, Cancellat
851
852
throw new ArgumentException( string . Format ( NpgsqlStrings . CannotUseSslVerifyWithUserCallback , sslMode ) ) ;
852
853
853
854
if ( Settings . RootCertificate is not null )
854
- throw new ArgumentException( string . Format ( NpgsqlStrings . CannotUseSslRootCertificateWithUserCallback ) ) ;
855
+ throw new ArgumentException( NpgsqlStrings . CannotUseSslRootCertificateWithUserCallback ) ;
855
856
856
857
if ( DataSource . RootCertificateCallback is not null )
857
- throw new ArgumentException( string . Format ( NpgsqlStrings . CannotUseValidationRootCertificateCallbackWithUserCallback ) ) ;
858
+ throw new ArgumentException( NpgsqlStrings . CannotUseValidationRootCertificateCallbackWithUserCallback ) ;
858
859
859
860
certificateValidationCallback = UserCertificateValidationCallback;
860
861
}
@@ -921,7 +922,8 @@ async Task RawOpen(SslMode sslMode, NpgsqlTimeout timeout, bool async, Cancellat
921
922
}
922
923
catch
923
924
{
924
- cert? . Dispose ( ) ;
925
+ _certificate? . Dispose ( ) ;
926
+ _certificate = null ;
925
927
926
928
_stream? . Dispose ( ) ;
927
929
_stream = null ! ;
@@ -2179,6 +2181,12 @@ void Cleanup()
2179
2181
Connection = null ;
2180
2182
PostgresParameters . Clear ( ) ;
2181
2183
_currentCommand = null ;
2184
+
2185
+ if ( _certificate is not null )
2186
+ {
2187
+ _certificate . Dispose ( ) ;
2188
+ _certificate = null ;
2189
+ }
2182
2190
}
2183
2191
2184
2192
void GenerateResetMessage ( )
0 commit comments