我們最近出現了IdP不信任來自RP/SP的SAML 2.0註銷請求籤名的問題。我們正在尋求替代方法來驗證SAML請求籤名,因爲IdP和samltool.com都抱怨過簽名驗證。以下是我們用來檢查簽名數據的示例答案,可以根據簽名進行驗證。PowerShell驗證SAML簽名的XML
1
A
回答
2
添加所需類型和定義爲SHA256
Add-Type -AssemblyName System.Security
# Add SHA-256 per http://stackoverflow.com/questions/30759119/verifying-xml-signature-in-powershell-with-pem-certificate
Add-Type @'
public class RSAPKCS1SHA256SignatureDescription : System.Security.Cryptography.SignatureDescription
{
public RSAPKCS1SHA256SignatureDescription()
{
base.KeyAlgorithm = "System.Security.Cryptography.RSACryptoServiceProvider";
base.DigestAlgorithm = "System.Security.Cryptography.SHA256Managed";
base.FormatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureFormatter";
base.DeformatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";
}
public override System.Security.Cryptography.AsymmetricSignatureDeformatter CreateDeformatter(System.Security.Cryptography.AsymmetricAlgorithm key)
{
System.Security.Cryptography.AsymmetricSignatureDeformatter asymmetricSignatureDeformatter = (System.Security.Cryptography.AsymmetricSignatureDeformatter)
System.Security.Cryptography.CryptoConfig.CreateFromName(base.DeformatterAlgorithm);
asymmetricSignatureDeformatter.SetKey(key);
asymmetricSignatureDeformatter.SetHashAlgorithm("SHA256");
return asymmetricSignatureDeformatter;
}
}
'@
$RSAPKCS1SHA256SignatureDescription = New-Object RSAPKCS1SHA256SignatureDescription
[System.Security.Cryptography.CryptoConfig]::AddAlgorithm($RSAPKCS1SHA256SignatureDescription.GetType(), "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256")
驗證SAML 2.0 HTTP-POST無證書請求包含在請求:
$saml = "insert real saml request here"
$decoded = [System.Convert]::FromBase64String($saml)
$stream = [System.IO.MemoryStream]::new($decoded, 0, $decoded.length)
$xml = New-Object System.Xml.XmlDocument
$xml.PreserveWhitespace = $true
$xml.Load($stream)
$signed = New-Object System.Security.Cryptography.Xml.SignedXml -ArgumentList $xml
$signed.LoadXml($xml.DocumentElement.Assertion.Signature)
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new("C:\Users\username\Desktop\idp.cer")
$keyinfo = [System.Security.Cryptography.Xml.KeyInfo]::new()
$clause = [System.Security.Cryptography.Xml.KeyInfoX509Data]::new($cert)
$keyinfo.AddClause($clause)
$signed.KeyInfo = $keyinfo
$signed.CheckSignature()
修改XML所以簽名不能被驗證在上例中:
$xml.Response.Assertion.Subject.NameID.'#text' = 'asdasdasd'
$signed = New-Object System.Security.Cryptography.Xml.SignedXml -ArgumentList $xml
$signed.LoadXml($xml.DocumentElement.Assertion.Signature)
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new("C:\Users\username\Desktop\idp.cer")
$keyinfo = [System.Security.Cryptography.Xml.KeyInfo]::new()
$clause = [System.Security.Cryptography.Xml.KeyInfoX509Data]::new($cert)
$keyinfo.AddClause($clause)
$signed.KeyInfo = $keyinfo
$signed.CheckSignature()
驗證SAML 2.0 HTTP-POST請求與證書包括在請求:
$saml = "insert example saml request here"
$decoded = [System.Convert]::FromBase64String($saml)
$stream = [System.IO.MemoryStream]::new($decoded, 0, $decoded.length)
$xml = New-Object System.Xml.XmlDocument
$xml.PreserveWhitespace = $true
$xml.Load($stream)
$signed = New-Object System.Security.Cryptography.Xml.SignedXml -ArgumentList $xml
$signed.LoadXml($xml.DocumentElement.Signature)
$signed.CheckSignature()
修改XML所以簽名不能在上面的例子中進行驗證:
$xml.LogoutRequest.NameID.'#text' = "dasdasd"
$signed = New-Object System.Security.Cryptography.Xml.SignedXml -ArgumentList $xml
$signed.LoadXml($xml.DocumentElement.Signature)
# Should return false since we modified the data
$signed.CheckSignature()
希望這節省了如果他們需要完成相同的任務,則需要其他人。如果您有任何意見和建議,請告訴我。
謝謝!
+0
好建議@AndersAbel,我已經更新了問題/答案的格式。 –
相關問題
- 1. SAML簽名驗證失敗
- 2. OKTA SAML簽名驗證 - PHP
- 3. 驗證SAML簽名取決於證書
- 4. 驗證C#中的java SAML簽名
- 5. 驗證python中的SAML簽名
- 6. XML簽名驗證
- 7. 間歇性SAML簽名驗證問題
- 8. 如何驗證SAML簽名值
- 9. 無法驗證SAML斷言簽名
- 10. SAML簽名驗證使用Python/M2Crypto
- 11. SAML RSA FIM(EMC)無法驗證簽名
- 12. SSO SAML數字簽名驗證 - XML unicode字符
- 13. Android - 驗證XML的簽名
- 14. Powershell XML驗證
- 15. Android - keytool XML簽名/驗證
- 16. XML數字簽名驗證
- 17. 驗證XML簽名JAVA
- 18. XML數字簽名驗證
- 19. passport-saml問題用於解碼和驗證已簽名的SAML響應
- 20. Java中的XML簽名驗證失敗
- 21. xml簽名中的引用驗證
- 22. 使用XSD驗證簽名的XML - xmldsig
- 23. XmlElement的Xml簽名無法驗證
- 24. C中的XML簽名驗證庫?
- 25. 哪個結果驗證簽名的XML - 核心驗證,簽名驗證和引用驗證
- 26. SAML重定向簽名或驗證未能生成正確的簽名
- 27. 簽名驗證在Wso2IS中的SAML聲明失敗
- 28. WSO2 IS 4.6.0:已簽名SAML身份驗證請求的組成
- 29. SAML 2.0的自簽名證書
- 30. 用於SAML簽名的證書類型
此代碼無法正確驗證簽名,並且容易受到XML簽名包裝攻擊的影響,因爲它不檢查引用。我寫了一篇關於引用是什麼的[博客文章](https://coding.abel.nu/2015/12/xml-signatures-and-references/),以及爲什麼檢查它們是必須的。 –
@AndersAbel,我不確定我是否按照你的評論100%。在我對此代碼進行測試時,如果我更改了簽名數據,則XML簽名驗證將按預期返回false。如果您正在討論驗證整個XML文檔的信任度,那麼這超出了我試圖展示的範圍。這只是一個驗證簽名數據簽名的簡單腳本。 在這兩個示例中,我都提供了$ signed對象中的關鍵信息,您是否指示我仍然需要將它作爲$ signed.CheckSignature()中的參數提供? –
@AndersAbel我想我現在明白了,但是這段代碼並不是用來檢查SAML令牌的可信/有效性,只是根據簽名數據來驗證簽名通過。 –