SAML-Toolkits / python3-saml

MIT License
704 stars 309 forks source link

Cannot decrypt Response with RSA-OAEP #316

Closed Lapeth closed 1 year ago

Lapeth commented 2 years ago

(Also raised this on StackOverflow a couple of days ago)

I've run into an issue with python3-saml where the AuthResponse fails to get decrypted. This is with a SP cert/key pair that I know fit together, and a test with these against a local SimpleSamlPHP container works fine. The certificate and key have been provided by the IdP for testing purposes here, so they're not used in production systems.

With debugging enabled, the following is logged:

func=xmlSecTransformNodeRead:file=transforms.c:line=1314:obj=unknown:subj=xmlSecTransformIdListFindByHref:error=1:xmlsec library function failed:href=http://www.w3.org/2009/xmlenc11#rsa-oaep
func=xmlSecTransformCtxNodeRead:file=transforms.c:line=595:obj=EncryptionMethod:subj=xmlSecTransformNodeRead:error=1:xmlsec library function failed: 
func=xmlSecEncCtxEncDataNodeRead:file=xmlenc.c:line=742:obj=EncryptionMethod:subj=xmlSecTransformCtxNodeRead:error=1:xmlsec library function failed: 
func=xmlSecEncCtxDecryptToBuffer:file=xmlenc.c:line=596:obj=unknown:subj=xmlSecEncCtxEncDataNodeRead:error=1:xmlsec library function failed: 
func=xmlSecKeysMngrGetKey:file=keys.c:line=1253:obj=unknown:subj=xmlSecKeysMngrFindKey:error=1:xmlsec library function failed: 
func=xmlSecEncCtxEncDataNodeRead:file=xmlenc.c:line=779:obj=unknown:subj=unknown:error=45:key is not found:encMethod=aes256-gcm
func=xmlSecEncCtxDecryptToBuffer:file=xmlenc.c:line=596:obj=unknown:subj=xmlSecEncCtxEncDataNodeRead:error=1:xmlsec library function failed: 
func=xmlSecEncCtxDecrypt:file=xmlenc.c:line=524:obj=unknown:subj=xmlSecEncCtxDecryptToBuffer:error=1:xmlsec library function failed: 
django_mitid_auth.saml.saml2 ERROR 2022-07-28 05:23:18,918 saml2 handle_login_callback (1, 'failed to decrypt')
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django_mitid_auth/saml/saml2.py", line 108, in handle_login_callback
    saml_auth.process_response(request_id=request_id)
  File "/usr/local/lib/python3.9/site-packages/onelogin/saml2/auth.py", line 130, in process_response
    response = self.response_class(self._settings, self._request_data['post_data']['SAMLResponse'])
  File "/usr/local/lib/python3.9/site-packages/onelogin/saml2/response.py", line 49, in __init__
    self.decrypted_document = self._decrypt_assertion(decrypted_document)
  File "/usr/local/lib/python3.9/site-packages/onelogin/saml2/response.py", line 903, in _decrypt_assertion
    decrypted = OneLogin_Saml2_Utils.decrypt_element(encrypted_data, key, debug=debug, inplace=True)
  File "/usr/local/lib/python3.9/site-packages/onelogin/saml2/utils.py", line 697, in decrypt_element
    return enc_ctx.decrypt(encrypted_data)
xmlsec.Error: (1, 'failed to decrypt')

Example SAML Response that triggers this:

<Response ID="_5319523e-64c5-1918-0657-d3a638045ad5" Version="2.0" IssueInstant="2022-07-26T06:23:31.0393084Z"
          Destination="https://[my domain]/login/callback/"
          InResponseTo="ONELOGIN_91d032676bab01fc363cce24799c865f03d74cba" xmlns="urn:oasis:names:tc:SAML:2.0:protocol">
    <Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
        https://saml.test-nemlog-in.dk/
    </Issuer>
    <Status>
        <StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
    </Status>
    <EncryptedAssertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
        <xenc:EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
                            xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
            <xenc:EncryptionMethod Algorithm="http://www.w3.org/2009/xmlenc11#aes256-gcm"/>
            <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
                <xenc:EncryptedKey>
                    <xenc:EncryptionMethod Algorithm="http://www.w3.org/2009/xmlenc11#rsa-oaep">
                        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
                        <xenc11:MGF Algorithm="http://www.w3.org/2009/xmlenc11#mgf1sha256"
                                    xmlns:xenc11="http://www.w3.org/2009/xmlenc11#"/>
                    </xenc:EncryptionMethod>
                    <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                        <o:SecurityTokenReference
                                xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                            <X509Data>
                                <X509IssuerSerial>
                                    <X509IssuerName>CN=TRUST2408 Systemtest XXXIV CA, O=TRUST2408, C=DK</X509IssuerName>
                                    <X509SerialNumber>1604005554</X509SerialNumber>
                                </X509IssuerSerial>
                            </X509Data>
                        </o:SecurityTokenReference>
                    </KeyInfo>
                    <xenc:CipherData>
                        <xenc:CipherValue>
                            KEvZkAI1oXrkfE7EPtoQ4FWUq8vlLvOG4qxt2GgzssNmPB+/M9egJgYqHWZ07cOSP30mLJ4cuBN6396m1yPqaMpjlhrI4GZr99rXvCI6SrlUGlyYWmciAlygEwed/V6XhSHG/i9MFi57w14JhNfMSe4senMNyy6SEZ9+hCUTtc3wHXlJh2HBGN4YOWcPX7ta8esaDwEx/D9Byw97xajUB/Uu6UL10ifnAM0+vPBCiZUKl5b+BQ08JvGH+AOJPUeloq0uixkS44jjpn7TFKKr6ddnU1iZUNZKrmUMatkMKo+a5dKnLouxfN1DW/g9RQUmaAp3K9QYKBtb9ysHOl1M8Q==
                        </xenc:CipherValue>
                    </xenc:CipherData>
                </xenc:EncryptedKey>
            </ds:KeyInfo>
            <xenc:CipherData>
                <xenc:CipherValue>
                    LYmZOnG1mKfQhU2oNCJHTYl9XvNfb/auF0xaEWZjUZIdwIuJA9gcanhO1VbB6E0WSD4ztujV6JTzfG4RPVjHELiKYE+FqucMJCUKU3/C9FyF0Qt6BzW5vmUVRX5SrGTudmFZn3feaF1m52vbHV3a9ssMMfsbt6AcO+hzlP/Ex/oMt97EWQDA2XPCxsDMJLkuYtTZ/mjEgIxYGEWh92m4b+/pa8yFKkLHc1ZkzeEQU8th0hyA8ZfmDufl+uThLvIHdVDhp9Ewk1H5nGxGG0mEYnNxjHx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WX5uL6BhguFbMbFl/5pbyI1DR5uXQCSCKQ5RMN1rGwjjhVm/VNNPdG7Ra0Z4ppaTaz0pHft4n8yaliUqr6NqYWXnllmlugSLwT4b8Dke13ESFsjguRcRK91KHNZ5D66Yl3TqYVzz9b8W3e3ZwS2GovK2A/jZgqeTCyuoFT0aEM489OCUfYlnzUqnpC+O/Hb3/8b1/t1v7uCZusITkp+CSs6jlhOztz/i8DY8WJj+OycsKP6KFULvAq4TDby/ysHvP5KgXrKElQWADmLNYOskz1v4oUSeRthfAN0ccffKE0DNb3i3KvlQc0giMUhIDg9lGUxkugR6O3FkPaOPJtsSt0y6qZ47kYJV6VVCQxoSFj6/+SkkDzj6R9Ewro1zYzTatkrvLA+ArBTKLRktZUte2/UEqZVsWel7ZfRzhaFBuwxCsAUF+eyAVeF5IMJuWUNud4lIQr/O6TQcYZbdpIrxiwrZWneftYEd4gvB7rhN55Z+Iol67CRU42PVPfMLPdHQpFmYEh6DtZv91XC0OBoa8042tAPWoYNrrpDltyWgRB2YZqxgGJJVzGoS5hpwcf/3UfxSZl203+FXBXq0nblE8upV54clig2Gcae9BPL1nWTzThYjhCeR2KPEmwPeAT9AxgFi6wLBVIvE0KHCqOxksHop8T1by855yBkhV9nz3TLyR1R0GVpThTCTMFbum2BuIfSZWSfrJU9XeIGmeqK/rrrEHBrrVgPv1iVhpNygT6Tcre/KK9f/e7/LIoAeZSroQcnwkBHDg+vOUAWN5umpBi5gAE2z4N4PE9zbWDLG5TznzNKRmR1kiJT7IyBYCRxwhzHnMFv6dWmlo+SDneZkO4EtT8Hj4iTX8rZDV+EhTSl95q/mj3D5YcgTSWjtBdHhIo+Ym8Bsp19ItcdVlOTsX8Yd1G68N48xfI6OcBfDzK8UiAtjwmoazQDuA5VoVC8lx+F8mKCKbNJN/Z5pnHHVmdf1ZadfAmzYgLsf9pJalBikVPmLXYQ=
                </xenc:CipherValue>
            </xenc:CipherData>
        </xenc:EncryptedData>
    </EncryptedAssertion>
</Response>

Note that the IdP uses RSA-OAEP in the response, and that one of the log lines mentions this algorithm.

I've tried patching utils.py, in the method decrypt_element, in order to inspect the key being added and generally to analyze the problem, and found that even though the key is added through an xmlsec.KeysManager, enc_ctx.key is None afterwards. Setting it with enc_ctx.key = xmlsec.Key.from_memory(key, xmlsec.KeyFormat.PEM, None) gets rid of the first several errors, but doesn't solve the problem (plus I know patching the code myself wouldn't be a solution anyway).

Does xmlsec have a problem with RSA-OAEP, or does python3-saml need to register the algorithm somehow?

pitbulk commented 1 year ago

Hi @Lapeth,

have you solved this issue? otherwise, are you still interested in solving this issue?

Check this thread: https://github.com/xmlsec/python-xmlsec/issues/22

You can try to use this simple script to check if the issue is on xmlsec or python3-saml.

Reopen this ticket in case that the issue is on python3-saml side.

Lapeth commented 1 year ago

I ended up using PySaml2 instead. That was a bumpy road as well, but eventually I got it working.