Blame SOURCES/0003-Choose-the-Reference-transform-based-on-the-chosen-S.patch

f2be37
From 9525237236eef4097300d9b6e93d2178a7a72267 Mon Sep 17 00:00:00 2001
f2be37
From: Benjamin Dauvergne <bdauvergne@entrouvert.com>
f2be37
Date: Fri, 4 Mar 2016 10:38:07 +0100
f2be37
Subject: [PATCH] Choose the Reference transform based on the chosen Signature
f2be37
 transform (fixes #10155)
f2be37
f2be37
i.e. if the signature use SHA2 then use SHA2 of the same strength for digesting
f2be37
references.
f2be37
---
f2be37
 lasso/xml/tools.c         | 41 +++++++++++++++++++++++++++++++++--------
f2be37
 tests/login_tests_saml2.c | 45 +++++++++++++++++++++++++++++++++++++++++++--
f2be37
 2 files changed, 76 insertions(+), 10 deletions(-)
f2be37
f2be37
diff --git a/lasso/xml/tools.c b/lasso/xml/tools.c
f2be37
index 0405592..cf9fe65 100644
f2be37
--- a/lasso/xml/tools.c
f2be37
+++ b/lasso/xml/tools.c
f2be37
@@ -2823,14 +2823,39 @@ lasso_xmlnode_add_saml2_signature_template(xmlNode *node, LassoSignatureContext
f2be37
 		xmlAddChild(node, signature);
f2be37
 	}
f2be37
 
f2be37
-	/* Normally the signature is son of the signed node, which holds an Id attribute, but in
f2be37
-	 * other cases, set snippet->offset to 0 and use xmlSecTmpSignatureAddReference from another
f2be37
-	 * node get_xmlNode virtual method to add the needed reference.
f2be37
-	 */
f2be37
-	uri = g_strdup_printf("#%s", id);
f2be37
-	reference = xmlSecTmplSignatureAddReference(signature,
f2be37
-			xmlSecTransformSha1Id, NULL, (xmlChar*)uri, NULL);
f2be37
-	lasso_release(uri);
f2be37
+	/* choose a digest for handling references based on the chosen signature algorithm */
f2be37
+	{
f2be37
+		xmlSecTransformId digest_method_id;
f2be37
+		switch (context.signature_method) {
f2be37
+			case LASSO_SIGNATURE_METHOD_RSA_SHA1:
f2be37
+			case LASSO_SIGNATURE_METHOD_DSA_SHA1:
f2be37
+			case LASSO_SIGNATURE_METHOD_HMAC_SHA1:
f2be37
+				digest_method_id = xmlSecTransformSha1Id;
f2be37
+				break;
f2be37
+			case LASSO_SIGNATURE_METHOD_RSA_SHA256:
f2be37
+			case LASSO_SIGNATURE_METHOD_HMAC_SHA256:
f2be37
+				digest_method_id = xmlSecTransformSha256Id;
f2be37
+				break;
f2be37
+			case LASSO_SIGNATURE_METHOD_RSA_SHA384:
f2be37
+			case LASSO_SIGNATURE_METHOD_HMAC_SHA384:
f2be37
+				digest_method_id = xmlSecTransformSha384Id;
f2be37
+				break;
f2be37
+			case LASSO_SIGNATURE_METHOD_RSA_SHA512:
f2be37
+			case LASSO_SIGNATURE_METHOD_HMAC_SHA512:
f2be37
+				digest_method_id = xmlSecTransformSha384Id;
f2be37
+				break;
f2be37
+			default:
f2be37
+				g_assert_not_reached();
f2be37
+		}
f2be37
+		/* Normally the signature is son of the signed node, which holds an Id attribute, but in
f2be37
+		 * other cases, set snippet->offset to 0 and use xmlSecTmpSignatureAddReference from another
f2be37
+		 * node get_xmlNode virtual method to add the needed reference.
f2be37
+		 */
f2be37
+		uri = g_strdup_printf("#%s", id);
f2be37
+		reference = xmlSecTmplSignatureAddReference(signature, digest_method_id, NULL,
f2be37
+				(xmlChar*)uri, NULL);
f2be37
+		lasso_release(uri);
f2be37
+	}
f2be37
 
f2be37
 	/* add enveloped transform */
f2be37
 	xmlSecTmplReferenceAddTransform(reference, xmlSecTransformEnvelopedId);
f2be37
diff --git a/tests/login_tests_saml2.c b/tests/login_tests_saml2.c
f2be37
index 9cd04ce..7f45099 100644
f2be37
--- a/tests/login_tests_saml2.c
f2be37
+++ b/tests/login_tests_saml2.c
f2be37
@@ -925,8 +925,10 @@ END_TEST
f2be37
 	lasso_provider_add_key(LASSO_PROVIDER(providers->data), key, FALSE); \
f2be37
 	g_list_free(providers);
f2be37
 
f2be37
+typedef void (*SsoCallback)(LassoLogin *idp_login_context, LassoLogin *sp_login_context);
f2be37
+
f2be37
 static void
f2be37
-sso_initiated_by_sp(LassoServer *idp_context, LassoServer *sp_context)
f2be37
+sso_initiated_by_sp(LassoServer *idp_context, LassoServer *sp_context, SsoCallback sso_callback)
f2be37
 {
f2be37
 	LassoLogin *idp_login_context;
f2be37
 	LassoLogin *sp_login_context;
f2be37
@@ -970,6 +972,10 @@ sso_initiated_by_sp(LassoServer *idp_context, LassoServer *sp_context)
f2be37
 				idp_login_context->parent.msg_body));
f2be37
 	check_good_rc(lasso_login_accept_sso(sp_login_context));
f2be37
 
f2be37
+	if (sso_callback) {
f2be37
+		sso_callback(idp_login_context, sp_login_context);
f2be37
+	}
f2be37
+
f2be37
 	/* Cleanup */
f2be37
 	lasso_release_gobject(idp_login_context);
f2be37
 	lasso_release_gobject(sp_login_context);
f2be37
@@ -991,8 +997,9 @@ START_TEST(test07_sso_sp_with_hmac_sha1_signatures)
f2be37
 	test07_make_context(idp_context, "idp6-saml2", LASSO_PROVIDER_ROLE_SP, "sp6-saml2", key)
f2be37
 	test07_make_context(sp_context, "sp6-saml2", LASSO_PROVIDER_ROLE_IDP, "idp6-saml2", key)
f2be37
 
f2be37
+
f2be37
 	block_lasso_logs;
f2be37
-	sso_initiated_by_sp(idp_context, sp_context);
f2be37
+	sso_initiated_by_sp(idp_context, sp_context, NULL);
f2be37
 	unblock_lasso_logs;
f2be37
 
f2be37
 	/* Cleanup */
f2be37
@@ -1514,6 +1521,39 @@ START_TEST(test11_ecp)
f2be37
 }
f2be37
 END_TEST
f2be37
 
f2be37
+void check_digest_method(LassoLogin *idp_login_context, LassoLogin *sp_login_context)
f2be37
+{
f2be37
+	char *dump = lasso_node_debug((LassoNode*)sp_login_context->parent.response, 10);
f2be37
+	check_true(strstr(dump, "<DigestMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#sha256\"/>") != NULL);
f2be37
+	lasso_release_string(dump)
f2be37
+}
f2be37
+
f2be37
+START_TEST(test12_sso_sp_with_rsa_sha256_signatures)
f2be37
+{
f2be37
+	LassoServer *idp_context = NULL;
f2be37
+	LassoServer *sp_context = NULL;
f2be37
+	GList *providers;
f2be37
+	LassoKey *key = NULL;
f2be37
+
f2be37
+	/* Create a key for signature algorithm RSA_SHA256 */
f2be37
+	key = lasso_key_new_for_signature_from_file(TESTSDATADIR "idp6-saml2/private-key.pem", NULL,
f2be37
+			LASSO_SIGNATURE_METHOD_RSA_SHA256, NULL);
f2be37
+	check_true(LASSO_IS_KEY(key));
f2be37
+
f2be37
+	test07_make_context(idp_context, "idp6-saml2", LASSO_PROVIDER_ROLE_SP, "sp6-saml2", key)
f2be37
+	test07_make_context(sp_context, "sp6-saml2", LASSO_PROVIDER_ROLE_IDP, "idp6-saml2", key)
f2be37
+
f2be37
+	block_lasso_logs;
f2be37
+	sso_initiated_by_sp(idp_context, sp_context, check_digest_method);
f2be37
+	unblock_lasso_logs;
f2be37
+
f2be37
+	/* Cleanup */
f2be37
+	lasso_release_gobject(idp_context);
f2be37
+	lasso_release_gobject(sp_context);
f2be37
+	lasso_release_gobject(key);
f2be37
+}
f2be37
+END_TEST
f2be37
+
f2be37
 Suite*
f2be37
 login_saml2_suite()
f2be37
 {
f2be37
@@ -1545,6 +1585,7 @@ login_saml2_suite()
f2be37
 	tcase_add_test(tc_ecp, test09_ecp);
f2be37
 	tcase_add_test(tc_ecp, test10_ecp);
f2be37
 	tcase_add_test(tc_ecp, test11_ecp);
f2be37
+	tcase_add_test(tc_spLogin, test12_sso_sp_with_rsa_sha256_signatures);
f2be37
 	return s;
f2be37
 }
f2be37
 
f2be37
-- 
f2be37
1.8.3.1
f2be37