diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs index 422c29c10e8f6..b2855084370f4 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs @@ -1433,16 +1433,18 @@ private static X509Certificate2 ExtractKeyFromPem( { if (label.SequenceEqual(eligibleLabel)) { - TAlg key = factory(); - key.ImportFromPem(contents[fields.Location]); - - try - { - return import(key); - } - catch (ArgumentException ae) + using (TAlg key = factory()) { - throw new CryptographicException(SR.Cryptography_X509_NoOrMismatchedPemKey, ae); + key.ImportFromPem(contents[fields.Location]); + + try + { + return import(key); + } + catch (ArgumentException ae) + { + throw new CryptographicException(SR.Cryptography_X509_NoOrMismatchedPemKey, ae); + } } } }