Skip to content

Commit

Permalink
Dispose of key when importing X.509 certificate from PEM
Browse files Browse the repository at this point in the history
  • Loading branch information
vcsjones authored Sep 29, 2022
1 parent e3ed6c1 commit b631a67
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1433,16 +1433,18 @@ private static X509Certificate2 ExtractKeyFromPem<TAlg>(
{
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);
}
}
}
}
Expand Down

0 comments on commit b631a67

Please sign in to comment.