验证来自 C# 的 java SAML 签名

发布于 2024-08-05 04:20:59 字数 4101 浏览 1 评论 0原文

如何在 .Net C# 中验证在 Java 中创建的 SAML 签名? 这是我从 Java 获得的 SAML 签名:

  <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <ds:SignedInfo>
        <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
        </ds:CanonicalizationMethod>
        <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1">
        </ds:SignatureMethod>
        <ds:Reference URI="#_e8bcba9d1c76d128938bddd5ae8c68e1">
            <ds:Transforms>
                <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature">
                </ds:Transform>
                <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
                    <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="code ds kind rw saml samlp typens #default xsd xsi">
                    </ec:InclusiveNamespaces>
                </ds:Transform>
            </ds:Transforms>
            <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1">
            </ds:DigestMethod>
            <ds:DigestValue>zEL7mB0Wkl+LtjMViO1imbucXiE=</ds:DigestValue>
        </ds:Reference>
    </ds:SignedInfo>
    <ds:SignatureValue>
jpIX3WbX9SCFnqrpDyLj4TeJN5DGIvlEH+o/mb9M01VGdgFRLtfHqIm16BloApUPg2dDafmc9DwL
Pyvs3TJ/hi0Q8f0ucaKdIuw+gBGxWFMcj/U68ZuLiv7U+Qe7i4ZA33rWPorkE82yfMacGf6ropPt
v73mC0bpBP1ubo5qbM4=
    </ds:SignatureValue>
    <ds:KeyInfo>
        <ds:X509Data>
            <ds:X509Certificate>
MIIDBDCCAeygAwIBAgIIC/ktBs1lgYcwDQYJKoZIhvcNAQEFBQAwNzERMA8GA1UEAwwIQWRtaW5D
QTExFTATBgNVBAoMDEVKQkNBIFNhbXBsZTELMAkGA1UEBhMCU0UwHhcNMDkwMjIzMTAwMzEzWhcN
MTgxMDE1MDkyNTQyWjBaMRQwEgYDVQQDDAsxMC41NS40MC42MTEbMBkGA1UECwwST24gRGVtYW5k
IFBsYXRmb3JtMRIwEAYDVQQLDAlPbiBEZW1hbmQxETAPBgNVBAsMCFNvZnR3YXJlMIGfMA0GCSqG
SIb3DQEBAQUAA4GNADCBiQKBgQCk5EqiedxA6WEE9N2vegSCqleFpXMfGplkrcPOdXTRLLOuRgQJ
LEsOaqspDFoqk7yJgr7kaQROjB9OicSH7Hhsu7HbdD6N3ntwQYoeNZ8nvLSSx4jz21zvswxAqw1p
DoGl3J6hks5owL4eYs2yRHvqgqXyZoxCccYwc4fYzMi42wIDAQABo3UwczAdBgNVHQ4EFgQUkrpk
yryZToKXOXuiU2hNsKXLbyIwDAYDVR0TAQH/BAIwADAfBgNVHSMEGDAWgBSiviFUK7DUsjvByMfK
g+pm4b2s7DAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDQYJKoZIhvcNAQEF
BQADggEBAKb94tnK2obEyvw8ZJ87u7gvkMxIezpBi/SqXTEBK1by0NHs8VJmdDN9+aOvC5np4fOL
fFcRH++n6fvemEGgIkK3pOmNL5WiPpbWxrx55Yqwnr6eLsbdATALE4cgyZWHl/E0uVO2Ixlqeygw
XTfg450cCWj4yfPTVZ73raKaDTWZK/Tnt7+ulm8xN+YWUIIbtW3KBQbGomqOzpftALyIKLVtBq7L
J0hgsKGHNUnssWj5dt3bYrHgzaWLlpW3ikdRd67Nf0c1zOEgKHNEozrtRKiLLy+3bIiFk0CHImac
1zeqLlhjrG3OmIsIjxc1Vbc0+E+z6Unco474oSGf+D1DO+Y=

            </ds:X509Certificate>
        </ds:X509Data>
    </ds:KeyInfo>
</ds:Signature>

我知道要解析 SAML,我需要验证签名。 我尝试了这一点:

public bool VerifySignature()
{
    X509Certificate2 certificate = null;

    XmlDocument doc = new XmlDocument();
    XmlElement xmlAssertionElement = this.GetXml(doc);
    doc.AppendChild(xmlAssertionElement);

    // Create a new SignedXml object and pass it
    // the XML document class.
    SamlSignedXml signedXml = new SamlSignedXml(xmlAssertionElement);

    // Get signature
    XmlElement xmlSignature = this.Signature;
    if (xmlSignature == null)
    {
        return false;
    }

    // Load the signature node.
    signedXml.LoadXml(xmlSignature);

    // Get the certificate used to sign the assertion if information about this 
    // certificate is available in the signature of the assertion.
    foreach (KeyInfoClause clause in signedXml.KeyInfo)
    {
        if (clause is KeyInfoX509Data)
        {
            if (((KeyInfoX509Data)clause).Certificates.Count > 0)
            {
                certificate = (X509Certificate2)((KeyInfoX509Data)clause).Certificates[0];
            }
        }
    }

    if (certificate == null)
    {
        return false;
    }

    return signedXml.CheckSignature(certificate, true);
  }

它验证了 .Net 中签名的 SAML 的签名,但不验证 Java 签名的签名。

How can i validate in .Net C# a SAML signature created in Java?
Here is the SAML Signature that i get from Java:

  <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <ds:SignedInfo>
        <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
        </ds:CanonicalizationMethod>
        <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1">
        </ds:SignatureMethod>
        <ds:Reference URI="#_e8bcba9d1c76d128938bddd5ae8c68e1">
            <ds:Transforms>
                <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature">
                </ds:Transform>
                <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
                    <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="code ds kind rw saml samlp typens #default xsd xsi">
                    </ec:InclusiveNamespaces>
                </ds:Transform>
            </ds:Transforms>
            <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1">
            </ds:DigestMethod>
            <ds:DigestValue>zEL7mB0Wkl+LtjMViO1imbucXiE=</ds:DigestValue>
        </ds:Reference>
    </ds:SignedInfo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WHl/E0uVO2Ixlqeygw
XTfg450cCWj4yfPTVZ73raKaDTWZK/Tnt7+ulm8xN+YWUIIbtW3KBQbGomqOzpftALyIKLVtBq7L
J0hgsKGHNUnssWj5dt3bYrHgzaWLlpW3ikdRd67Nf0c1zOEgKHNEozrtRKiLLy+3bIiFk0CHImac
1zeqLlhjrG3OmIsIjxc1Vbc0+E+z6Unco474oSGf+D1DO+Y=

            </ds:X509Certificate>
        </ds:X509Data>
    </ds:KeyInfo>
</ds:Signature>

I know to parse SAML, i need to validate the signature.
I tried this:

public bool VerifySignature()
{
    X509Certificate2 certificate = null;

    XmlDocument doc = new XmlDocument();
    XmlElement xmlAssertionElement = this.GetXml(doc);
    doc.AppendChild(xmlAssertionElement);

    // Create a new SignedXml object and pass it
    // the XML document class.
    SamlSignedXml signedXml = new SamlSignedXml(xmlAssertionElement);

    // Get signature
    XmlElement xmlSignature = this.Signature;
    if (xmlSignature == null)
    {
        return false;
    }

    // Load the signature node.
    signedXml.LoadXml(xmlSignature);

    // Get the certificate used to sign the assertion if information about this 
    // certificate is available in the signature of the assertion.
    foreach (KeyInfoClause clause in signedXml.KeyInfo)
    {
        if (clause is KeyInfoX509Data)
        {
            if (((KeyInfoX509Data)clause).Certificates.Count > 0)
            {
                certificate = (X509Certificate2)((KeyInfoX509Data)clause).Certificates[0];
            }
        }
    }

    if (certificate == null)
    {
        return false;
    }

    return signedXml.CheckSignature(certificate, true);
  }

It validates the signature of a SAML signed in .Net but not of this Java one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2024-08-12 04:20:59

使用此线程中的答案解决了问题:
http://social.msdn .microsoft.com/Forums/en-US/wcf/thread/faf0b66c-294b-4d84-a19b-504dd8e81922
我的代码与 MSDN 中显示的示例非常相似,唯一缺少的是:
验证时 doc.PreserveWhitespace=true。

Solved the problem with the answers in this thread:
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/faf0b66c-294b-4d84-a19b-504dd8e81922
My code was very similar with examples from MSDN shown there, only thing missing was:
doc.PreserveWhitespace=true on validation.

不再让梦枯萎 2024-08-12 04:20:59

只要您的证书存储中有公钥,您就可以使用 System.Security.Cryptography.Xml 命名空间中的类来验证使用数字签名签名的 XML 数据。不久前,我用 AD 联合服务做了一些工作,它也使用 SAML,我记得,在找到我需要的命名空间后,其余的就相当简单了——但时间太久远了,我忘记了细节。

You can use the classes in the System.Security.Cryptography.Xml namespace to verify XML data signed with a digital signature, so long as you have the public key in your certificate store. I've done some work a while back with AD Federation Services which also use SAML and as I remember, after finding the namespace I needed, the rest was fairly simple -- but it's long enough ago that details escape me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文