@@ -282,6 +282,8 @@ internal bool PostgresCancellationPerformed
282282 internal bool AttemptPostgresCancellation { get ; private set ; }
283283 static readonly TimeSpan _cancelImmediatelyTimeout = TimeSpan . FromMilliseconds ( - 1 ) ;
284284
285+ X509Certificate2 ? _certificate ;
286+
285287 internal NpgsqlLoggingConfiguration LoggingConfiguration { get ; }
286288
287289 internal ILogger ConnectionLogger { get ; }
@@ -756,7 +758,6 @@ async ValueTask<string> GetUsernameAsyncInternal()
756758
757759 async Task RawOpen( SslMode sslMode , NpgsqlTimeout timeout , bool async , CancellationToken cancellationToken , bool isFirstAttempt = true)
758760 {
759- var cert = default ( X509Certificate2 ? ) ;
760761 try
761762 {
762763 if ( async)
@@ -815,23 +816,23 @@ async Task RawOpen(SslMode sslMode, NpgsqlTimeout timeout, bool async, Cancellat
815816#if NET5_0_OR_GREATER
816817 // It's PEM time
817818 var keyPath = Settings . SslKey ?? PostgresEnvironment . SslKey ?? PostgresEnvironment . SslKeyDefault ;
818- cert = string . IsNullOrEmpty ( password )
819+ _certificate = string . IsNullOrEmpty ( password )
819820 ? X509Certificate2 . CreateFromPemFile ( certPath , keyPath )
820821 : X509Certificate2 . CreateFromEncryptedPemFile ( certPath , password , keyPath ) ;
821822 if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
822823 {
823824 // Windows crypto API has a bug with pem certs
824825 // 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 ) ) ;
827828 }
828829#else
829830 throw new NotSupportedException ( "PEM certificates are only supported with .NET 5 and higher" ) ;
830831#endif
831832 }
832833
833- cert ??= new X509Certificate2 ( certPath , password ) ;
834- clientCertificates. Add ( cert ) ;
834+ _certificate ??= new X509Certificate2 ( certPath , password ) ;
835+ clientCertificates. Add ( _certificate ) ;
835836 }
836837
837838 ClientCertificatesCallback? . Invoke ( clientCertificates ) ;
@@ -846,7 +847,7 @@ async Task RawOpen(SslMode sslMode, NpgsqlTimeout timeout, bool async, Cancellat
846847 throw new ArgumentException( string . Format ( NpgsqlStrings . CannotUseSslVerifyWithUserCallback , sslMode ) ) ;
847848
848849 if ( Settings . RootCertificate is not null )
849- throw new ArgumentException( string . Format ( NpgsqlStrings . CannotUseSslRootCertificateWithUserCallback ) ) ;
850+ throw new ArgumentException( NpgsqlStrings . CannotUseSslRootCertificateWithUserCallback ) ;
850851
851852 certificateValidationCallback = UserCertificateValidationCallback;
852853 }
@@ -912,7 +913,8 @@ async Task RawOpen(SslMode sslMode, NpgsqlTimeout timeout, bool async, Cancellat
912913 }
913914 catch
914915 {
915- cert? . Dispose ( ) ;
916+ _certificate? . Dispose ( ) ;
917+ _certificate = null ;
916918
917919 _stream? . Dispose ( ) ;
918920 _stream = null ! ;
@@ -2160,6 +2162,12 @@ void Cleanup()
21602162 Connection = null ;
21612163 PostgresParameters. Clear ( ) ;
21622164 _currentCommand = null ;
2165+
2166+ if ( _certificate is not null )
2167+ {
2168+ _certificate. Dispose ( ) ;
2169+ _certificate = null ;
2170+ }
21632171 }
21642172
21652173 void GenerateResetMessage( )
0 commit comments