使用ITFoxTec SAML2与Azure KeyVault时解密密钥遇403禁止错误
SAML 2.0集成解密失败问题
背景
我们基于.NET 6 MVC框架,使用ITfoxtec Identity SAML 2.0包实现了与丹麦KOMBIT系统(外部身份提供商)的SSO集成,参考官方示例将Azure KeyVault用于加密操作。
更新证书后,新证书的密钥用途仅包含KeyEncipherment标识,不再具备DataEncipherment标识。此时接收并解密来自身份提供商的SAML响应时,Azure KeyVault返回403禁止响应,证书根CA为Den Danske Stat OCES rod-CA。
相关SAML2Configuration配置
var saml2Configuration = new Saml2Configuration { Issuer = _config.Issuer, SignatureAlgorithm = _config.SignatureAlgorithm, CertificateValidationMode = _config.CertificateValidationMode, RevocationMode = _config.RevocationMode }; var certificateClient = new CertificateClient(new Uri(AzureKeyVaultBaseUrl), _tokenCredential); var certificateWithPolicy = certificateClient.GetCertificate(AzureKeyVaultCertificateName); var publicCertificate = new X509Certificate2(certificateWithPolicy.Value.Cer); var rsa = RSAFactory.Create(_tokenCredential, certificateWithPolicy.Value.KeyId, new Azure.Security.KeyVault.Keys.JsonWebKey(publicCertificate.GetRSAPublicKey())); saml2Configuration.SigningCertificate = new Saml2X509Certificate(publicCertificate, rsa); saml2Configuration.DecryptionCertificates = new List<X509Certificate2>() {new Saml2X509Certificate(publicCertificate, rsa)}; saml2Configuration.SignAuthnRequest = true;
回调端点读取SAML响应代码
var binding = new Saml2PostBinding(); var saml2AuthResponse = new Saml2AuthnResponse(_saml2Configuration); var httpRequest = Request.ToGenericHttpRequest(); binding.ReadSamlResponse(httpRequest, saml2AuthResponse);
抛出异常信息
System.Security.Cryptography.CryptographicException: Error calling Key Vault ---> Azure.RequestFailedException: Operation decrypt is not permitted on this key. Status: 403 (Forbidden) ErrorCode: Forbidden Content: {"error":{"code":"Forbidden","message":"Operation decrypt is not permitted on this key.","innererror":{"code":"KeyOperationForbidden"}}} at Azure.Security.KeyVault.KeyVaultPipeline.SendRequest(Request request, CancellationToken cancellationToken) at Azure.Security.KeyVault.KeyVaultPipeline.SendRequest[TContent,TResult](RequestMethod method, TContent content, Func`1 resultFactory, CancellationToken cancellationToken, String[] path) at Azure.Security.KeyVault.Keys.Cryptography.RemoteCryptographyClient.Decrypt(DecryptParameters parameters, CancellationToken cancellationToken) at Azure.Security.KeyVault.Keys.Cryptography.CryptographyClient.Decrypt(DecryptParameters decryptParameters, CancellationToken cancellationToken) at Azure.Security.KeyVault.Keys.Cryptography.CryptographyClient.Decrypt(EncryptionAlgorithm algorithm, Byte[] ciphertext, CancellationToken cancellationToken) at RSAKeyVaultProvider.KeyVaultContext.DecryptData(Byte[] cipherText, RSAEncryptionPadding padding) in /_/RSAKeyVaultProvider/KeyVaultContext.cs:line 104 at RSAKeyVaultProvider.RSAKeyVault.Decrypt(Byte[] data, RSAEncryptionPadding padding) in /_/RSAKeyVaultProvider/RSAKeyVault.cs:line 87 --- End of inner exception stack trace --- at RSAKeyVaultProvider.RSAKeyVault.Decrypt(Byte[] data, RSAEncryptionPadding padding) in /_/RSAKeyVaultProvider/RSAKeyVault.cs:line 91 at System.Security.Cryptography.RSAOAEPKeyExchangeDeformatter.DecryptKeyExchange(Byte[] rgbData) at System.Security.Cryptography.Xml.EncryptedXml.DecryptKey(Byte[] keyData, RSA rsa, Boolean useOAEP) at ITfoxtec.Identity.Saml2.Cryptography.Saml2EncryptedXml.DecryptEncryptedKey(EncryptedKey encryptedKey) at System.Security.Cryptography.Xml.EncryptedXml.GetDecryptionKey(EncryptedData encryptedData, String symmetricAlgorithmUri) at System.Security.Cryptography.Xml.EncryptedXml.DecryptDocument() at ITfoxtec.Identity.Saml2.Saml2AuthnResponse.DecryptMessage() at ITfoxtec.Identity.Saml2.Saml2Request.Read(String xml, Boolean validateXmlSignature, Boolean detectReplayedTokens) at ITfoxtec.Identity.Saml2.Saml2Response.Read(String xml, Boolean validateXmlSignature, Boolean detectReplayedTokens) at ITfoxtec.Identity.Saml2.Saml2AuthnResponse.Read(String xml, Boolean validateXmlSignature, Boolean detectReplayedTokens) at ITfoxtec.Identity.Saml2.Saml2PostBinding.Read(HttpRequest request, Saml2Request saml2RequestResponse, String messageName, Boolean validateXmlSignature, Boolean detectReplayedTokens) at ITfoxtec.Identity.Saml2.Saml2Binding`1.ReadSamlResponse(HttpRequest request, Saml2Response saml2Response) at "our code"
已尝试方案
- 调试确认
ITfoxtec.Identity.Saml2.Cryptography.Saml2EncryptedXml.DecryptEncryptedKey最终调用RSAKeyVaultProvider.KeyVaultContext.DecryptData,该方法向Azure KeyVault发起的API请求返回禁止响应 - 临时修改第三方库中该方法的API调用为
UnwrapKey后功能恢复正常,但不希望修改第三方库实现
疑问
原本期望通过接受RSA参数的Saml2X509Certificate构造函数继续集成,但System.Security.Cryptography的RSA类没有DecryptKey方法,仅包含Decrypt方法。使用无DataEncipherment标识的证书,是否无法再遵循示例完成集成?
内容的提问来源于stack exchange,提问作者madsatwork
相关产品推荐
相关产品推荐

