Getting "Signature validation failed. SAML Response rejected" using python-saml with flask
Asked Answered
P

1

7

I'm getting the following error when trying to process a IdP-initiated SAML2 response using python-saml and flask:

Signature validation failed. SAML Response rejected

I'm following the example here. My code is:

url_data = urlparse(request.url)
req = {
    "https": "on",
    "http_host": request.host,
    "server_port": url_data.port,
    "script_name": request.path,
    "get_data": request.args.copy(),
    "post_data": request.form.copy()
}
auth = OneLogin_Saml2_Auth(req, custom_base_path=app.config['SAML_PATH'])
auth.process_response()

In SAML_PATH, I have the following in my settings.json file:

{
    "strict": false,
    "debug": true,
    "sp": {
        "entityId": "[spEntityId]",
        "assertionConsumerService": {
            "url": "[acsUrl]",
            "binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
        },
        "NameIDFormat": "urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified",
        "x509cert": "[x509cert]",
        "privateKey": "[privateKey]"
    },
    "idp": {
        "entityId": "[idpEntityId]",
        "singleSignOnService": {
            "url": "http://dummy.com/saml2",
            "binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
        },
        "singleLogoutService": {
            "url": "http://dummy.com/saml2",
            "binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
        },
        "x509cert": "[x509cert]"
    },
    "security": {
        "nameIdEncrypted": false,
        "authnRequestsSigned": false,
        "logoutRequestSigned": false,
        "logoutResponseSigned": false,
        "signMetadata": false,
        "wantMessagesSigned": true,
        "wantAssertionsSigned": true,
        "wantNameIdEncrypted": false,
        "requestedAuthnContext": false
    }
}

As you can see, I've used dummy values for the IdP singleSignOnService and singleLogoutService URLs. I don't think I need them in my case as I just need to process the SAML Response. I've also using the same x509cert for both the SP and IdP. The response has a signed message and encrypted assertion:

    <?xml version="1.0" encoding="UTF-8"?>
<saml2p:Response xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" Destination="[Destination]" ID="[ID]" IssueInstant="2015-11-30T15:35:02.702Z" Version="2.0">
    <saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">
        [Issuer]
    </saml2:Issuer>
    <saml2p:Status>
        <saml2p:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
    </saml2p:Status>
    <saml2:EncryptedAssertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">
        <xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Id="[ID]" Type="http://www.w3.org/2001/04/xmlenc#Element">
            <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" />
            <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
                <xenc:EncryptedKey Id="[ID]" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
                    <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" />
                    <xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
                        <xenc:CipherValue>
                            [CipherValue]
                        </xenc:CipherValue>
                    </xenc:CipherData>
                </xenc:EncryptedKey>
            </ds:KeyInfo>
            <xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
                <xenc:CipherValue>
                    [CipherValue]
                </xenc:CipherValue>
            </xenc:CipherData>
        </xenc:EncryptedData>
    </saml2:EncryptedAssertion>
</saml2p:Response>

I've verified that the x509cert and privateKey are correct. I'm new to SAML2, so I'm hoping it's something simple :) Thank you in advance!!

Pullulate answered 3/12, 2015 at 14:11 Comment(2)
Did you find the fix? I'm having a similar issue.Eldredge
My issue was resolved by having the IdP include the KeyInfo element in the Signature. This is currently required by python-saml: github.com/onelogin/python-saml/issues/101Pullulate
H
3

The response you provide above isn't signed, but you've requested that that response be signed, therefore you software is rejecting the response.

Your configuration says wantMessagesSigned, and wantAssertionsSigned. The Assertions are signed (maybe -- we can't actually tell from your example), but the message (i.e., the response as a whole) is not signed.

How can you tell? If your response were signed, it would look more like the following -- note the ds:Signature block (which I've included some blank lines around to make it easier to see). The block includes a DigestValue of the message, and then a Signature of that Digest. It includes the Certificate with which you can decode the signature and verify it matches the digest.

The IdP signs the response with its private key and sends you the certificate. Assertions are encrypted using your certificate by the IdP, allowing you (and only you) to decrypt them (Are the encrypted assertions signed? I don't know -- need to decrypt them first.)

<?xml version="1.0" encoding="UTF-8"?>
  <saml2p:Response xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" Destination="[Destination]" ID="[ID]" IssueInstant="2015-11-30T15:35:02.702Z" Version="2.0">
<saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">
    [Issuer]
</saml2:Issuer>

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
  <ds:SignedInfo>
    <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-cl14n#"/>
    <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
    <ds:Reference URI="#[ID]">
      <ds:Transforms>
         ...
      </ds:Transforms>
      <ds:DigestMethod Algorithm="http://www/s3.org/2001/04/xmlenc#sha256"/>
      <ds:DigestValue>[DigestValue]</ds:DigestValue>
    </ds:Reference>
  </ds:SignedInfo>
  <ds:SignatureValue>
     [Signature]
  </ds:SignatureValue>
  <ds:KeyInfo>
    <ds:X509Data>
      <ds:X509Certificate>
         [Certificate]
      </ds:X509Certificate>
    </ds:X509Data>
  </ds:KeyInfo>
</ds:Signature>


<saml2p:Status>
    <saml2p:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
</saml2p:Status>
<saml2:EncryptedAssertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">
    <xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Id="[ID]" Type="http://www.w3.org/2001/04/xmlenc#Element">
        <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" />
        <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
            <xenc:EncryptedKey Id="[ID]" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
                <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" />
                <xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
                    <xenc:CipherValue>
                        [CipherValue]
                    </xenc:CipherValue>
                </xenc:CipherData>
            </xenc:EncryptedKey>
        </ds:KeyInfo>
        <xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
            <xenc:CipherValue>
                [CipherValue]
            </xenc:CipherValue>
        </xenc:CipherData>
    </xenc:EncryptedData>
</saml2:EncryptedAssertion>

Housewifery answered 7/11, 2018 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.