Blame SOURCES/0005-Check-if-the-signature-method-is-allowed-in-addition.patch

db363b
From f9a3aca0cb31a412faae25dd9fdbbf3fb61cb62f Mon Sep 17 00:00:00 2001
db363b
From: Jakub Hrozek <jhrozek@redhat.com>
db363b
Date: Tue, 15 Jun 2021 15:08:44 +0200
db363b
Subject: [PATCH 5/7] Check if the signature method is allowed in addition to
db363b
 being valid (#54037)
db363b
db363b
Adds a new utility function lasso_allowed_signature_method() that checks
db363b
if the signature method is allowed. Previously, the code would only
db363b
check if the method was valid.
db363b
db363b
This new function is used whenever lasso_validate_signature_method was
db363b
previously used through lasso_ok_signature_method() which wraps both
db363b
validate and allowed.
db363b
db363b
lasso_allowed_signature_method() is also used on a couple of places,
db363b
notably lasso_query_verify_helper().
db363b
db363b
Related:
db363b
https://dev.entrouvert.org/issues/54037
db363b
---
db363b
 lasso/id-ff/server.c     |  4 ++--
db363b
 lasso/saml-2.0/profile.c |  4 ++--
db363b
 lasso/xml/tools.c        | 11 ++++++++++-
db363b
 lasso/xml/xml.c          |  5 +++--
db363b
 lasso/xml/xml.h          | 13 +++++++++++++
db363b
 5 files changed, 30 insertions(+), 7 deletions(-)
db363b
db363b
diff --git a/lasso/id-ff/server.c b/lasso/id-ff/server.c
db363b
index 2bf5b7a8c..98a6c0214 100644
db363b
--- a/lasso/id-ff/server.c
db363b
+++ b/lasso/id-ff/server.c
db363b
@@ -909,7 +909,7 @@ lasso_server_get_signature_context_for_provider(LassoServer *server,
db363b
 		private_context = &provider->private_data->signature_context;
db363b
 	}
db363b
 
db363b
-	if (private_context && lasso_validate_signature_method(private_context->signature_method)) {
db363b
+	if (private_context && lasso_ok_signature_method(private_context->signature_method)) {
db363b
 		lasso_assign_signature_context(*signature_context, *private_context);
db363b
 	} else {
db363b
 		rc = lasso_server_get_signature_context(server, signature_context);
db363b
@@ -1014,7 +1014,7 @@ lasso_server_export_to_query_for_provider_by_name(LassoServer *server, const cha
db363b
 				provider_id, &context));
db363b
 	query = lasso_node_build_query(node);
db363b
 	goto_cleanup_if_fail_with_rc(query, LASSO_PROFILE_ERROR_BUILDING_QUERY_FAILED);
db363b
-	if (lasso_validate_signature_method(context.signature_method)) {
db363b
+	if (lasso_ok_signature_method(context.signature_method)) {
db363b
 		lasso_assign_new_string(query, lasso_query_sign(query, context));
db363b
 	}
db363b
 	goto_cleanup_if_fail_with_rc(query,
db363b
diff --git a/lasso/saml-2.0/profile.c b/lasso/saml-2.0/profile.c
db363b
index 85f535ae0..412c391a6 100644
db363b
--- a/lasso/saml-2.0/profile.c
db363b
+++ b/lasso/saml-2.0/profile.c
db363b
@@ -1181,7 +1181,7 @@ lasso_saml20_profile_export_to_query(LassoProfile *profile, LassoNode *msg, char
db363b
 					"see #3.4.3 of saml-bindings-2.0-os");
db363b
 		}
db363b
 	}
db363b
-	if (lasso_validate_signature_method(context.signature_method)) {
db363b
+	if (lasso_ok_signature_method(context.signature_method)) {
db363b
 		result = lasso_query_sign(unsigned_query, context);
db363b
 		goto_cleanup_if_fail_with_rc(result != NULL,
db363b
 				LASSO_PROFILE_ERROR_BUILDING_QUERY_FAILED);
db363b
@@ -1219,7 +1219,7 @@ lasso_saml20_profile_build_http_redirect(LassoProfile *profile,
db363b
 	goto_cleanup_if_fail_with_rc (url != NULL, LASSO_PROFILE_ERROR_UNKNOWN_PROFILE_URL);
db363b
 	/* if message is signed, remove XML signature, add query signature */
db363b
 	lasso_assign_signature_context(context, lasso_node_get_signature(msg));
db363b
-	if (lasso_validate_signature_method(context.signature_method)) {
db363b
+	if (lasso_ok_signature_method(context.signature_method)) {
db363b
 		lasso_node_remove_signature(msg);
db363b
 	}
db363b
 	lasso_check_good_rc(lasso_saml20_profile_export_to_query(profile, msg, &query, context));
db363b
diff --git a/lasso/xml/tools.c b/lasso/xml/tools.c
db363b
index cf6dade09..077b1134d 100644
db363b
--- a/lasso/xml/tools.c
db363b
+++ b/lasso/xml/tools.c
db363b
@@ -499,7 +499,7 @@ lasso_query_sign(char *query, LassoSignatureContext context)
db363b
         lasso_error_t rc = 0;
db363b
 
db363b
 	g_return_val_if_fail(query != NULL, NULL);
db363b
-	g_return_val_if_fail(lasso_validate_signature_method(context.signature_method), NULL);
db363b
+	g_return_val_if_fail(lasso_ok_signature_method(context.signature_method), NULL);
db363b
 
db363b
 	key = context.signature_key;
db363b
 	sign_method = context.signature_method;
db363b
@@ -804,6 +804,12 @@ lasso_query_verify_helper(const char *signed_content, const char *b64_signature,
db363b
 	} else {
db363b
 		goto_cleanup_with_rc(LASSO_DS_ERROR_INVALID_SIGALG);
db363b
 	}
db363b
+
db363b
+	/* is the signature algo allowed */
db363b
+	goto_cleanup_if_fail_with_rc(
db363b
+                lasso_allowed_signature_method(method),
db363b
+                LASSO_DS_ERROR_INVALID_SIGALG);
db363b
+
db363b
 	/* decode signature */
db363b
 	signature = g_malloc(key_size+1);
db363b
 	goto_cleanup_if_fail_with_rc(
db363b
@@ -2434,6 +2440,9 @@ _lasso_xmlsec_load_key_from_buffer(const char *buffer, size_t length, const char
db363b
 	};
db363b
 	xmlSecKey *private_key = NULL;
db363b
 
db363b
+	/* is the signature algo allowed */
db363b
+	goto_cleanup_if_fail(lasso_allowed_signature_method(signature_method));
db363b
+
db363b
 	xmlSecErrorsDefaultCallbackEnableOutput(FALSE);
db363b
 	switch (signature_method) {
db363b
 		case LASSO_SIGNATURE_METHOD_RSA_SHA1:
db363b
diff --git a/lasso/xml/xml.c b/lasso/xml/xml.c
db363b
index f017ebbe3..49574de68 100644
db363b
--- a/lasso/xml/xml.c
db363b
+++ b/lasso/xml/xml.c
db363b
@@ -824,7 +824,7 @@ lasso_legacy_extract_and_copy_signature_parameters(LassoNode *node, LassoNodeCla
db363b
 			node_data->sign_method_offset);
db363b
 	private_key_file = G_STRUCT_MEMBER(char *, node, node_data->private_key_file_offset);
db363b
 	certificate_file = G_STRUCT_MEMBER(char *, node, node_data->certificate_file_offset);
db363b
-	if (! lasso_validate_signature_method(signature_method)) {
db363b
+	if (! lasso_ok_signature_method(signature_method)) {
db363b
 		return FALSE;
db363b
 	}
db363b
 	if (lasso_node_set_signature(node,
db363b
@@ -1873,10 +1873,11 @@ lasso_node_impl_init_from_xml(LassoNode *node, xmlNode *xmlnode)
db363b
 			int what;
db363b
 			if (! lasso_get_integer_attribute(xmlnode, LASSO_SIGNATURE_METHOD_ATTRIBUTE,
db363b
 						BAD_CAST LASSO_LIB_HREF, &what,
db363b
-						LASSO_SIGNATURE_METHOD_RSA_SHA1,
db363b
+						lasso_get_min_signature_method(),
db363b
 						LASSO_SIGNATURE_METHOD_LAST))
db363b
 				break;
db363b
 			method = what;
db363b
+
db363b
 			if (! lasso_get_integer_attribute(xmlnode, LASSO_SIGNATURE_METHOD_ATTRIBUTE,
db363b
 					BAD_CAST LASSO_LIB_HREF, &what, LASSO_SIGNATURE_TYPE_NONE+1,
db363b
 					LASSO_SIGNATURE_TYPE_LAST))
db363b
diff --git a/lasso/xml/xml.h b/lasso/xml/xml.h
db363b
index d0d3e1b0d..60c04eae5 100644
db363b
--- a/lasso/xml/xml.h
db363b
+++ b/lasso/xml/xml.h
db363b
@@ -132,6 +132,19 @@ lasso_validate_signature_method(LassoSignatureMethod signature_method)
db363b
 		&& signature_method < (LassoSignatureMethod)LASSO_SIGNATURE_METHOD_LAST;
db363b
 }
db363b
 
db363b
+static inline gboolean
db363b
+lasso_allowed_signature_method(LassoSignatureMethod signature_method)
db363b
+{
db363b
+	return signature_method >= lasso_get_min_signature_method();
db363b
+}
db363b
+
db363b
+static inline gboolean
db363b
+lasso_ok_signature_method(LassoSignatureMethod signature_method)
db363b
+{
db363b
+	return lasso_validate_signature_method(signature_method) \
db363b
+	    && lasso_allowed_signature_method(signature_method);
db363b
+}
db363b
+
db363b
 typedef struct _LassoNode LassoNode;
db363b
 typedef struct _LassoNodeClass LassoNodeClass;
db363b
 typedef struct _LassoNodeClassData LassoNodeClassData;
db363b
-- 
db363b
2.26.3
db363b