diff --git a/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.CreateToken.cs b/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.CreateToken.cs index c05ec8bce1..dae8ce0401 100644 --- a/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.CreateToken.cs +++ b/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.CreateToken.cs @@ -1334,6 +1334,7 @@ internal IEnumerable GetContentEncryptionKeys(JsonWebToken jwtToken StringBuilder keysAttempted = null; foreach (var key in keys) { + KeyWrapProvider kwp = null; try { #if NET472 || NET6_0_OR_GREATER @@ -1365,7 +1366,7 @@ internal IEnumerable GetContentEncryptionKeys(JsonWebToken jwtToken jwtToken.TryGetHeaderValue(JwtHeaderParameterNames.Apu, out string apu); jwtToken.TryGetHeaderValue(JwtHeaderParameterNames.Apv, out string apv); SecurityKey kdf = ecdhKeyExchangeProvider.GenerateKdf(apu, apv); - var kwp = key.CryptoProviderFactory.CreateKeyWrapProviderForUnwrap(kdf, ecdhKeyExchangeProvider.GetEncryptionAlgorithm()); + kwp = key.CryptoProviderFactory.CreateKeyWrapProviderForUnwrap(kdf, ecdhKeyExchangeProvider.GetEncryptionAlgorithm()); var unwrappedKey = kwp.UnwrapKey(Base64UrlEncoder.DecodeBytes(jwtToken.EncryptedKey)); unwrappedKeys.Add(new SymmetricSecurityKey(unwrappedKey)); } @@ -1373,7 +1374,7 @@ internal IEnumerable GetContentEncryptionKeys(JsonWebToken jwtToken #endif if (key.CryptoProviderFactory.IsSupportedAlgorithm(jwtToken.Alg, key)) { - var kwp = key.CryptoProviderFactory.CreateKeyWrapProviderForUnwrap(key, jwtToken.Alg); + kwp = key.CryptoProviderFactory.CreateKeyWrapProviderForUnwrap(key, jwtToken.Alg); var unwrappedKey = kwp.UnwrapKey(jwtToken.EncryptedKeyBytes); unwrappedKeys.Add(new SymmetricSecurityKey(unwrappedKey)); } @@ -1382,6 +1383,13 @@ internal IEnumerable GetContentEncryptionKeys(JsonWebToken jwtToken { (exceptionStrings ??= new StringBuilder()).AppendLine(ex.ToString()); } + finally + { + if (kwp != null) + { + key.CryptoProviderFactory.ReleaseKeyWrapProvider(kwp); + } + } (keysAttempted ??= new StringBuilder()).AppendLine(key.ToString()); }