Skip to content

[Authn request/response] Added configurable signing and digest algorithm to SP and IDP #597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 0 commits into from

Conversation

peppelinux
Copy link
Member

Added support for configurable signing and digest algorithms, following this PR, thank you @joetsoi!

What's new?

  • Resolved conflicting files, it work with the actual master branch;
  • SP authn_request and logout_request are now signed by default with the configured values;
  • Added IDP authn_response with this default behaviour;

Why this?

The problem is that some federation contexts don't permit the sha1 digest anymore, I really need of saml2.xmldsig.SIG_RSA_SHA256 to get pySAML2 in a production environment.

To get it working we have to put in the config file the same options already proposed by @joetsoi:

'signing_algorithm':  saml2.xmldsig.SIG_RSA_SHA256,
'digest_algorithm':  saml2.xmldsig.DIGEST_SHA256,

Tests

Environment tests was made with djangosaml2 and djangosaml2idp.
Authn request and response, and Logout Response was succesfully tested.

A request from SP:
<ns0:AuthnRequest AssertionConsumerServiceURL="http://sp1.testunical.it:8000/saml2/acs/" Destination="http://idp1.testunical.it:9000/idp/sso/post" ForceAuthn="true" ID="id-bKo8ZFu82dsVemb7O" IssueInstant="2019-03-20T16:23:55Z" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Version="2.0" xmlns:ns0="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:ns1="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#">
	<ns1:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">http://sp1.testunical.it:8000/saml2/metadata/</ns1:Issuer>
	<ns2:Signature Id="Signature1">
		<ns2:SignedInfo>
			<ns2:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
			<ns2:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
			<ns2:Reference URI="#id-bKo8ZFu82dsVemb7O">
				<ns2:Transforms>
					<ns2:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
					<ns2:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
				</ns2:Transforms>
				<ns2:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
				<ns2:DigestValue>vdszs92V0F/g0DQCMuhti1yj6Bxv4Otjqzoykv/O8JY=</ns2:DigestValue>
			</ns2:Reference>
		</ns2:SignedInfo>
		<ns2:SignatureValue>Qor0shTfX0lPb74ypXjhGQRUvr2JDdJvfxC0wQXC3MdTJryYSxbHAVE/Yp53C9dq
6cZsDGHMyaW7OlG7mmHPfqChWEoPtKJxEKr/917MK3070VxgS6tias2So1KiFElz
k8oCmjHVP8sj+02x1yUTNyfcr0moW4KaOJFPMPF750VqB/G/hQSTlDAww1ehZLKr
LZ/0dWvExbeTjmuH8SU/DaalGF0NuIGiE8y8kyJ/9I9O655UFPwljfqxpNl3TV/k
dgC3s7dNL1iBCIINarQa7HhbJFKXiNKhiIKy8/K+ILDppWlCCbf62AvgXMq/yVO6
LYG9NUC6giFpv4HRqQNAJg==</ns2:SignatureValue>
		<ns2:KeyInfo>
			<ns2:X509Data>
				<ns2:X509Certificate>MIIDETCCAfmgAwIBAgIUPw12Gkt4agBtLBntd7RzTWwLKAYwDQYJKoZIhvcNAQELBQAwGDEWMBQGA1UEAwwNc3AxLnVuaWNhbC5pdDAeFw0xOTAzMjAxNDMxMTVaFw0yOTAzMTcxNDMxMTVaMBgxFjAUBgNVBAMMDXNwMS51bmljYWwuaXQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCj0BzOt58ECsSwT049bIhmD0p7q0Y+4L7cjrvvJYcMT7HZE+tbO4M6upkXnP+3gvPpRccaEtwJoda3gYzvF35VMzp0fCW7OmXIR8cJtySIfzkdmmO385Tbxlp1jRxZyQtc2nPzCKeV4xlQNEinQr94nI7tMFReDejjXKwS5RABk8KQMo2M78xa9RQyxqDC1e0ioeVQRR2og99fF3u/WOJ/JB2aETTfvInrFIyFA5XB0roBDyM44877nRKYeMBd4kVk+fs4yu6kZm7WOXHUXFLKRuXLeVxEbZYzSSMjncsB1U35OAt+Ozkp+12qaqMAVdGKP+xso3zGAr/5AC6CuPnrAgMBAAGjUzBRMB0GA1UdDgQWBBR6RRoajGB1UmdiMAKSmgpL3RD0mzAfBgNVHSMEGDAWgBR6RRoajGB1UmdiMAKSmgpL3RD0mzAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBzRUa++T0EiR+Fq4iTpKIysigV+1CeMbS+u1JaPOnMXfWmboOOVDrHhnitbpfxm+SpbafPTz40THtfw9EKvReMjNa4HQ4vFBMwZtmYZ4piGS5PferFDzYdZG1dS/2vcCQA4Dya/R675XKEhBdWO8JfUOL1ImMoJBa5Z+ApU8OCk8hpiJUV0akpw7jA+VO8+VR2T+SH+3h28KOrNdraWozZ99NKqB7GUFcaxouaOkPE7mi8JyAgLZMZvJaeQeJEUI9sfavSLmvBsfbusAeCjFYCVM9MM7uZNvK6gI0Dzppl+rN7vRWcBF+oYyiS1EX9j2GqG1yWcdGvY60GJu5Er5id</ns2:X509Certificate>
			</ns2:X509Data>
		</ns2:KeyInfo>
	</ns2:Signature>
	<ns0:NameIDPolicy AllowCreate="false" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:persistent"/>
</ns0:AuthnRequest>
The Response from IDP:
<?xml version="1.0" ?>
<ns0:Response Destination="http://sp1.testunical.it:8000/saml2/acs/" ID="id-6xnSXo08EBFp4oYfU" InResponseTo="id-bKo8ZFu82dsVemb7O" IssueInstant="2019-03-20T16:24:39Z" Version="2.0" xmlns:ns0="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:ns1="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<ns1:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">http://idp1.testunical.it:9000/idp/metadata</ns1:Issuer>
	<ns2:Signature Id="Signature1">
		<ns2:SignedInfo>
			<ns2:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
			<ns2:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
			<ns2:Reference URI="#id-6xnSXo08EBFp4oYfU">
				<ns2:Transforms>
					<ns2:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
					<ns2:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
				</ns2:Transforms>
				<ns2:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
				<ns2:DigestValue>5ynI4Mo4blrOoBaZCuA5BPlN02cye7Uwb/eHQXSum3M=</ns2:DigestValue>
			</ns2:Reference>
		</ns2:SignedInfo>
		<ns2:SignatureValue>GfTHf+dR9H9kn2r+Dms7zQ1Wz+/gCnaCW/auuq/9S0vAioDKMN5AYqOmGg1vIfaS
MswBZ0GDN6Z4bZ//NnpJA68/SFFjIqllghhqx+ElfvYl1FbdCr3ae1DMSpONtZQb
sw6CahxCLyYz6Gg3b199tM0RAcuygXiKwI8QD1WqgtErAErWwsBZ87G/0sKOB+wX
2x0T0DOitMjE/sm6p02qtvUe592VjJcwsc8EJUW0bM9HSkNOeP+3bjWDpj/ku7aJ
2LWLckZf2RHHTB/Kdn2cao0rPVuotx7cqT4FJdlI/LsQPLn1nEx/uPMLTs6+ZfEw
+0PRdQNpEjNC070D/PuISw==</ns2:SignatureValue>
		<ns2:KeyInfo>
			<ns2:X509Data>
				<ns2:X509Certificate>MIIDgzCCAmugAwIBAgIQSeYzhdkMjG/8jtR/zOIM8jANBgkqhkiG9w0BAQsFADAYMRYwFAYDVQQDDA10ZXN0dW5pY2FsLml0MB4XDTE5MDMxMjEwMDQ0MVoXDTIyMDIyNDEwMDQ0MVowHTEbMBkGA1UEAwwSaWRwM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ns2:Signature>
	<ns0:Status>
		<ns0:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
	</ns0:Status>
	<ns1:Assertion ID="id-V2lAummsu0aaktgtN" IssueInstant="2019-03-20T16:24:39Z" Version="2.0">
		<ns1:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">http://idp1.testunical.it:9000/idp/metadata</ns1:Issuer>
		<ns2:Signature Id="Signature2">
			<ns2:SignedInfo>
				<ns2:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
				<ns2:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
				<ns2:Reference URI="#id-V2lAummsu0aaktgtN">
					<ns2:Transforms>
						<ns2:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
						<ns2:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
					</ns2:Transforms>
					<ns2:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
					<ns2:DigestValue>Z1Exv9KHS5Xw7kr0X6HbmXkxOw8=</ns2:DigestValue>
				</ns2:Reference>
			</ns2:SignedInfo>
			<ns2:SignatureValue>EW9b4lElDU1pzqGeuj2mVeyJpSn8w+QfvOnxmTSa4mXOAOXh7KofG6YOnAcrSrx9
uCuJtQ+5gFyNklpADeMr9i+JSaxacxTd4vWrlnMA8zIDkxzBgQt1jGDHSVHch3PV
IAPzrv/wuuSbZ5o6CpXq4moRWLJagedtbfjve8BghaXjJxDO0dM8w8FFf0C2BcUC
OAKFHXv03PySNr6863xeX8qt+t3yTK7IURQHtY9R7JhTd1AJB15kUKWdHbFNSXJi
otktInRwtc2YtYbe11dhq8ng9VGFoP+Fh6Umr4ts4jeVw/8NnjuT0sm5d2KmUmcx
p4eADOHCKDqnyi3iIGuTUw==</ns2:SignatureValue>
			<ns2:KeyInfo>
				<ns2:X509Data>
					<ns2:X509Certificate>MIIDgzCCAmugAwIBAgIQSeYzhd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6FIOBcMvp6holAqa7JaX56BjC/x0po0vPDljoNrQo2PSmq/UrF9ZclHY/6RJTYieKJh4gST88kNOY4k1xNSLdvngTVipNd3zA3dyb3</ns2:X509Certificate>
				</ns2:X509Data>
			</ns2:KeyInfo>
		</ns2:Signature>
		<ns1:Subject>
			<ns1:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:persistent" SPNameQualifier="http://sp1.testunical.it:8000/saml2/metadata/">wert</ns1:NameID>
			<ns1:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
				<ns1:SubjectConfirmationData InResponseTo="id-bKo8ZFu82dsVemb7O" NotOnOrAfter="2019-03-20T16:39:39Z" Recipient="http://sp1.testunical.it:8000/saml2/acs/"/>
			</ns1:SubjectConfirmation>
		</ns1:Subject>
		<ns1:Conditions NotBefore="2019-03-20T16:24:39Z" NotOnOrAfter="2019-03-20T16:39:39Z">
			<ns1:AudienceRestriction>
				<ns1:Audience>http://sp1.testunical.it:8000/saml2/metadata/</ns1:Audience>
			</ns1:AudienceRestriction>
		</ns1:Conditions>
		<ns1:AuthnStatement AuthnInstant="2019-03-20T16:24:39Z" SessionIndex="id-QFdE2NuADrHQTt63I">
			<ns1:AuthnContext>
				<ns1:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</ns1:AuthnContextClassRef>
			</ns1:AuthnContext>
		</ns1:AuthnStatement>
		<ns1:AttributeStatement>
			<ns1:Attribute FriendlyName="email" Name="urn:oid:1.2.840.113549.1.9.1.1" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">
				<ns1:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">[email protected]</ns1:AttributeValue>
			</ns1:Attribute>
			<ns1:Attribute Name="username" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">
				<ns1:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">wert</ns1:AttributeValue>
			</ns1:Attribute>
		</ns1:AttributeStatement>
	</ns1:Assertion>
</ns0:Response>

Tests

Regarding test and code linting read this

Conclusions

I hope to see this merged, optionally in a separate branch, and continue working on this to get all the others services to get supported by these configuration paramenters. Thank you for your time.

@codecov
Copy link

codecov bot commented Mar 20, 2019

Codecov Report

Merging #597 into master will increase coverage by 0.00%.
The diff coverage is 80.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #597   +/-   ##
=======================================
  Coverage   65.28%   65.29%           
=======================================
  Files         102      102           
  Lines       25493    25503   +10     
=======================================
+ Hits        16643    16651    +8     
- Misses       8850     8852    +2     
Impacted Files Coverage Δ
src/saml2/entity.py 85.75% <50.00%> (-0.23%) ⬇️
src/saml2/client_base.py 76.90% <100.00%> (+0.12%) ⬆️
src/saml2/config.py 86.57% <100.00%> (+0.09%) ⬆️
src/saml2/server.py 72.62% <100.00%> (+0.14%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1474c4b...9385c8d. Read the comment docs.

peppelinux added a commit to peppelinux/pysaml2 that referenced this pull request Apr 8, 2019
…ts](../../pulls) for the same update/change?

* [x] Have you added an explanation of what problem you are trying to solve with this PR?

It is needed for returning attributes like
````
          <saml:Attribute Name="dateOfBirth">
            <saml:AttributeValue xsi:type="xs:date">2015-12-06</saml:AttributeValue>
          </saml:Attribute>
 ````

Otherwise I get an ValueError Exception like
````
Type and value do not match: date:<class 'str'>:2015-12-06
````
because `type(value) is not valid_type == True`

I didn't write any unit test, just tested in my context.

Two important things:
1. This PR is on top of IdentityPython#597 but it doesn't have any dependencies on it. The only file I changed is saml.py, let me know if this PR will be merged in next releases and if you need separate branch for every features. In the future I will this way.
2. I think that the following code https://github.com/IdentityPython/pysaml2/pull/602/files#diff-6c156669cad61eda35e679329251dce9R197 could be also improved according to IdentityPython#518
@eelkevdbos
Copy link

Would love to see this merged 👍

@peppelinux
Copy link
Member Author

Glad to hear this ;)
I patched the conflicts

@joetsoi
Copy link

joetsoi commented Nov 28, 2019

I'm no longer working on the project that needed this, but I'm sure whoever is maintaining that code now would really appreciate this being merged!

@PurkkaKoodari
Copy link

With my setup I'm currently unable to authenticate against ADFS without patching in either this PR or IdentityPython/djangosaml2#142. I would also very much like to see this merged so I don't need to maintain a fork for our system.

@@ -181,6 +181,9 @@ def __init__(self, config=None, identity_cache=None, state_cache=None,

setattr(self, attr, val)

for algorithm in ('signing_algorithm', 'digest_algorithm'):
setattr(self, algorithm, self.config.getattr(algorithm, "sp"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer the more explicit

self.signing_algorithm = self.config.getattr(signing_algorithm, "sp")
self.digest_algorithm = self.config.getattr(digest_algorithm, "sp")

but I see that some lines above is the setattr(self, attr, val) so maybe it's just me 🤷‍♂️

@@ -595,6 +595,9 @@ def gather_authn_response_args(self, sp_entity_id, name_id_policy, userid,
except KeyError:
args['best_effort'] = False

for algorithm in ('signing_algorithm', 'digest_algorithm'):
setattr(self, algorithm, self.config.getattr(algorithm, "idp"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again I would prefer the more explicit

self.signing_algorithm = self.config.getattr(signing_algorithm, "idp")
self.digest_algorithm = self.config.getattr(digest_algorithm, "idp")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not, sure!

sign_alg = self.signing_algorithm

if digest_alg is None and self.digest_algorithm:
digest_alg = self.digest_algorithm
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm doing a general refactor,

        # sign adn digest algs
        sign_alg = sign_alg or self.signing_algorithm
        digest_alg = digest_alg or self.digest_algorithm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants