SAML 2.0 实例分析 idp向sp发送响应(4)

当idp与user建立起联系后,idp向sp发送响应

<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
                ID="_81e65e52081649f77587dd7a12b0e3c3"
                InResponseTo="6541c310-4e01-4d85-8f8b-3d05d119b9c2"
                IssueInstant="2020-04-18T08:13:50.867Z"
                Version="2.0"
                >
    <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">http://localhost:8080/idp</saml:Issuer>
    <samlp:Status>
        <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
    </samlp:Status>
    <saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
                    ID="18dfba4b-e0b5-46fa-b7b7-7fdbbe295b39"
                    IssueInstant="2020-04-18T08:13:50.866Z"
                    Version="2.0"
                    >
        <saml:Issuer>http://localhost:8080/idp</saml:Issuer>
        <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
            <SignedInfo>
                <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
                <Reference URI="#18dfba4b-e0b5-46fa-b7b7-7fdbbe295b39">
                    <Transforms>
                        <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                        <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                    </Transforms>
                    <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                    <DigestValue>vFGoJhHTVDDvkRz9bezf9+zhEBs=</DigestValue>
                </Reference>
            </SignedInfo>
            <SignatureValue>DuvgFw+uqHeMZr5y/1iF29ZNB6Gt6Vj6IqyIN4wR0Ty5EVd2iaYREQxIRv+/xLoko7XiO4o3bkFs
6g/mlDc3Bs81LZYBCW11kTlDNtayQtuffzvdI+w9Z4h5pFo8uvAo7bA4hDPlUSsewu/5MOGMUiiI
4b/HoIrb7sCeZgnFF8k=</SignatureValue>
            <KeyInfo>
                <X509Data>
                    <X509Certificate>MIICcjCCAdugAwIBAgIEYUV9sjANBgkqhkiG9w0BAQsFADBsMRAwDgYDVQQGEwdVbmtub3duMRAw
DgYDVQQIEwdVbmtub3duMRAwDgYDVQQHEwdVbmtub3duMRAwDgYDVQQKEwdVbmtub3duMRAwDgYD
VQQLEwdVbmtub3duMRAwDgYDVQQDEwdVbmtub3duMB4XDTE3MTIyMTA2NDc0MloXDTI3MTIxOTA2
NDc0MlowbDEQMA4GA1UEBhMHVW5rbm93bjEQMA4GA1UECBMHVW5rbm93bjEQMA4GA1UEBxMHVW5r
bm93bjEQMA4GA1UEChMHVW5rbm93bjEQMA4GA1UECxMHVW5rbm93bjEQMA4GA1UEAxMHVW5rbm93
bjCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAtD3CyKRmy7aaTOeBCDiQaqGQu99wEAHqbcXu
7/ZkNYGlb9HIGM4ga5rnQqrZIR+1njcss1B+6agRmREu3Db9SKL6OqsdKEY8KZX318VhksoECK1m
FvzuwK/S7VAHAay3ApLfBMCC+8sZ7njvByqSo0CAFAcQvu5mtkYPC6xzHX0CAwEAAaMhMB8wHQYD
VR0OBBYEFMcXi7CGVHaw7JLJlEi3mSV0ny4QMA0GCSqGSIb3DQEBCwUAA4GBAIM4m+sXq/S5ryxa
Lf8o/HlKHj+61e6n6vP+zrVnAuXiNNjEI76PQWws+1CpaZq2XI0IC+mY1SwdrvltUTCiTFEv3NA5
EhnB6aPUHORX2WF80s6yEq8AZ0IITRL2nxilb+kWxbU/BKLN7qWXcwEI/mKM/Aowha6vYxtnIYy2
H8hy</X509Certificate>
                </X509Data>
            </KeyInfo>
        </Signature>
        <saml:Subject>
            <saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">test@qq.com</saml:NameID>
            <saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
                <saml:SubjectConfirmationData Recipient="http://localhost:8080/sp/consumer" />
            </saml:SubjectConfirmation>
        </saml:Subject>
        <saml:Conditions NotBefore="2020-04-18T08:13:35.866Z"
                         NotOnOrAfter="2020-04-18T08:14:20.866Z"
                         >
            <saml:AudienceRestriction>
                <saml:Audience>http://localhost:8080/sp</saml:Audience>
            </saml:AudienceRestriction>
        </saml:Conditions>
        <saml:AuthnStatement AuthnInstant="2020-04-18T08:13:50.866Z">
            <saml:AuthnContext>
                <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml:AuthnContextClassRef>
            </saml:AuthnContext>
        </saml:AuthnStatement>
    </saml:Assertion>
</samlp:Response>

 

1、 samlp:Response 根节点,表明这是一个 response 对象
2、 Destination 目标地址,即 ACS 地址,Response 返回的地址
3、 ID 唯一标识
4、 IssueInstant 时间戳
5、 saml:Issuer IDP 身份信息,IDP 的 EntityId
6、 samlp:Status 认证结果, samlp:StatusCode 表明认证成功或失败
7、 saml:Assertion 断言,这是 Response 中最为重要的字段,里面包含着用户身份信息
8、 Signature 断言的签名,使用非对称私钥对 Assertion 内容(不包含 Signature)进行签名,防止信息被篡改
9、 saml:Subject 身份主体,主要包括身份信息
10、 saml:NameID 身份信息
11、 saml:Conditions 给出了断言被认为有效的验证条件。
12、 saml:AuthnStatement 描述了在身份提供者的认证行为。

首先生成断言信息 Assertion,然后对断言进行签名,将签名信息插入到 Assertion 的子节点中,即 <Signature>的信息,最后生成 Response 结构,将 Response 字符串进行 base64 编码后 post 到 acs 地址上,SP 对其进行验证。

 

SAML 中的签名:

 <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
            <SignedInfo>
                <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
                <Reference URI="#18dfba4b-e0b5-46fa-b7b7-7fdbbe295b39">
                    <Transforms>
                        <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                        <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                    </Transforms>
                    <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                    <DigestValue>vFGoJhHTVDDvkRz9bezf9+zhEBs=</DigestValue>
                </Reference>
            </SignedInfo>
            <SignatureValue>DuvgFw+uqHeMZr5y/1iF29ZNB6Gt6Vj6IqyIN4wR0Ty5EVd2iaYREQxIRv+/xLoko7XiO4o3bkFs
6g/mlDc3Bs81LZYBCW11kTlDNtayQtuffzvdI+w9Z4h5pFo8uvAo7bA4hDPlUSsewu/5MOGMUiiI
4b/HoIrb7sCeZgnFF8k=</SignatureValue>
            <KeyInfo>
                <X509Data>
                    <X509Certificate>MIICcjCCAdugAwIBAgIEYUV9sjANBgkqhkiG9w0BAQsFADBsMRAwDgYDVQQGEwdVbmtub3duMRAw
DgYDVQQIEwdVbmtub3duMRAwDgYDVQQHEwdVbmtub3duMRAwDgYDVQQKEwdVbmtub3duMRAwDgYD
VQQLEwdVbmtub3duMRAwDgYDVQQDEwdVbmtub3duMB4XDTE3MTIyMTA2NDc0MloXDTI3MTIxOTA2
NDc0MlowbDEQMA4GA1UEBhMHVW5rbm93bjEQMA4GA1UECBMHVW5rbm93bjEQMA4GA1UEBxMHVW5r
bm93bjEQMA4GA1UEChMHVW5rbm93bjEQMA4GA1UECxMHVW5rbm93bjEQMA4GA1UEAxMHVW5rbm93
bjCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAtD3CyKRmy7aaTOeBCDiQaqGQu99wEAHqbcXu
7/ZkNYGlb9HIGM4ga5rnQqrZIR+1njcss1B+6agRmREu3Db9SKL6OqsdKEY8KZX318VhksoECK1m
FvzuwK/S7VAHAay3ApLfBMCC+8sZ7njvByqSo0CAFAcQvu5mtkYPC6xzHX0CAwEAAaMhMB8wHQYD
VR0OBBYEFMcXi7CGVHaw7JLJlEi3mSV0ny4QMA0GCSqGSIb3DQEBCwUAA4GBAIM4m+sXq/S5ryxa
Lf8o/HlKHj+61e6n6vP+zrVnAuXiNNjEI76PQWws+1CpaZq2XI0IC+mY1SwdrvltUTCiTFEv3NA5
EhnB6aPUHORX2WF80s6yEq8AZ0IITRL2nxilb+kWxbU/BKLN7qWXcwEI/mKM/Aowha6vYxtnIYy2
H8hy</X509Certificate>
                </X509Data>
            </KeyInfo>
        </Signature>

 

SAML 中的签名算法就是对 xml 文档树进行签名,说明如下:
1、确认签名内容,通过 URL 将这些内容表示为引用资源,用 Reference 标识。对于断言信息来说,其 URI 是saml:Assertion的 ID
2、对待签名的数据进行转化处理,包括执行编码规则、规范化算法等, Transform 指定了转化的算法
3、对整个断言进行消息摘要, DigestMethod 指定了消息摘要算法,消息摘要的结果保存在 DigestValue 元素中
4、构造包含 Reference 的 SignedInfo 元素
5、 CanonicalizationMethod 元素指定了规范化的算法,如果不对其进行规范化处理,验证 xml 签名时可能因为 xml 结构表示不同而失败
6、计算 SignedInfo 的摘要,使用 SignatureMethod 声明的签名算法,并对其进行签名,结果保存到 SignatureValue 元素中
7、 KeyInfo 元素可选,表明签名的公钥信息

SP 接收到 IDP 的响应后,验证签名,获取用户的信息后跳转响应页面。

posted @ 2020-04-19 13:25  程程111  阅读(1601)  评论(2编辑  收藏  举报