Blame SOURCES/0003-Make-the-default-signature-method-and-the-minimal-ha.patch

db363b
From f095ac8f5740b6eee687cac97840bc7e72992999 Mon Sep 17 00:00:00 2001
db363b
From: Jakub Hrozek <jhrozek@redhat.com>
db363b
Date: Mon, 7 Jun 2021 12:27:15 +0200
db363b
Subject: [PATCH 3/7] Make the default signature method and the minimal hash
db363b
 strength configurable (#54037)
db363b
db363b
Adds two new configure options:
db363b
    --with-default-sign-algo
db363b
    --min-hash-algo
db363b
db363b
--with-default-sign-algo sets the default signing algorithm and defaults
db363b
to rsa-sha1. At the moment, two algorithms are supported: rsa-sha1 and
db363b
rsa-sha256.
db363b
db363b
--min-hash-algo sets the minimum hash algorithm to be accepted. The
db363b
default is sha1 for backwards compatibility as well.
db363b
db363b
Related:
db363b
https://dev.entrouvert.org/issues/54037
db363b
---
db363b
 configure.ac         | 42 +++++++++++++++++++++++++++++
db363b
 lasso/id-ff/server.c |  2 +-
db363b
 lasso/id-ff/server.h |  2 ++
db363b
 lasso/lasso.c        | 51 +++++++++++++++++++++++++++++++++++
db363b
 lasso/xml/tools.c    | 63 +++++++++++++++++++++++++++++++++++---------
db363b
 lasso/xml/xml.c      | 24 +++++++++++++++++
db363b
 lasso/xml/xml.h      |  9 +++++++
db363b
 tests/random_tests.c |  6 ++---
db363b
 8 files changed, 182 insertions(+), 17 deletions(-)
db363b
db363b
diff --git a/configure.ac b/configure.ac
db363b
index b527def43..2cdfbb149 100644
db363b
--- a/configure.ac
db363b
+++ b/configure.ac
db363b
@@ -795,6 +795,43 @@ else
db363b
     AC_MSG_RESULT(no)
db363b
 fi 
db363b
 
db363b
+AC_ARG_WITH([default-sign-algo],
db363b
+            [AS_HELP_STRING([--with-default-sign-algo=[rsa-sha1|rsa-sha256]],
db363b
+                            [Default signing algorithm (rsa-sha1)]
db363b
+                           )
db363b
+            ]
db363b
+)
db363b
+
db363b
+SIGNING_ALGO=rsa-sha1
db363b
+if test x"$with_default_sign_algo" != x; then
db363b
+    if test ! "$with_default_sign_algo" = "rsa-sha1" -a ! "$with_default_sign_algo" = "rsa-sha256"; then
db363b
+	AC_MSG_ERROR("Default signing algorithm must be either rsa-sha1 or rsa-sha256")
db363b
+    else
db363b
+	SIGNING_ALGO=$with_default_sign_algo
db363b
+    fi
db363b
+fi
db363b
+
db363b
+AC_DEFINE_UNQUOTED(DEFAULT_SIGNING_ALGO, "$SIGNING_ALGO", ["The default signing algorithm"])
db363b
+
db363b
+AC_ARG_WITH([min-hash-algo],
db363b
+            [AS_HELP_STRING([--with-min-hash-algo=[sha1|sha256|sha384|sha512]],
db363b
+                            [Minimal allowed hash algorithm (rsa-sha1)]
db363b
+                           )
db363b
+            ]
db363b
+)
db363b
+
db363b
+MIN_HASH_ALGO=sha1
db363b
+if test x"$with_min_hash_algo" != x; then
db363b
+    if test ! "$with_min_hash_algo" = "sha1" -a ! "$with_min_hash_algo" = "sha256" -a ! "$with_min_hash_algo" = "sha384" -a ! "$with_min_hash_algo" = "sha512"; then
db363b
+	AC_MSG_ERROR("Minimal allowed hash algorithm must be one of sha1, sha256, sha384 or sha512)
db363b
+    else
db363b
+	MIN_HASH_ALGO=$with_min_hash_algo
db363b
+    fi
db363b
+fi
db363b
+
db363b
+AC_DEFINE_UNQUOTED(MIN_HASH_ALGO, "$MIN_HASH_ALGO", ["The minimal hash algorithm"])
db363b
+
db363b
+
db363b
 dnl ==========================================================================
db363b
 dnl Pedantic compilation
db363b
 dnl ==========================================================================
db363b
@@ -939,4 +976,9 @@ Python binding:         ${enable_python}
db363b
 
db363b
 C API references:       ${enable_gtk_doc}
db363b
 Tests suite:            ${enable_tests}
db363b
+
db363b
+Crypto settings
db363b
+---------------
db363b
+Default signature:      ${SIGNING_ALGO}
db363b
+Minimal accepted hash:  ${MIN_HASH_ALGO}
db363b
 )
db363b
diff --git a/lasso/id-ff/server.c b/lasso/id-ff/server.c
db363b
index 08bbde833..2bf5b7a8c 100644
db363b
--- a/lasso/id-ff/server.c
db363b
+++ b/lasso/id-ff/server.c
db363b
@@ -682,7 +682,7 @@ instance_init(LassoServer *server)
db363b
 	server->private_key = NULL;
db363b
 	server->private_key_password = NULL;
db363b
 	server->certificate = NULL;
db363b
-	server->signature_method = LASSO_SIGNATURE_METHOD_RSA_SHA1;
db363b
+	server->signature_method = lasso_get_default_signature_method();
db363b
 
db363b
 	server->services = g_hash_table_new_full(g_str_hash, g_str_equal,
db363b
 			(GDestroyNotify)g_free,
db363b
diff --git a/lasso/id-ff/server.h b/lasso/id-ff/server.h
db363b
index 8b4192793..5f9022e9d 100644
db363b
--- a/lasso/id-ff/server.h
db363b
+++ b/lasso/id-ff/server.h
db363b
@@ -133,6 +133,8 @@ LASSO_EXPORT gchar *lasso_server_get_endpoint_url_by_id(const LassoServer *serve
db363b
 LASSO_EXPORT GList *lasso_server_get_filtered_provider_list(const LassoServer *server,
db363b
 	LassoProviderRole role, LassoMdProtocolType protocol_type, LassoHttpMethod http_method);
db363b
 
db363b
+LASSO_EXPORT LassoSignatureMethod lasso_get_default_signature_method();
db363b
+void lasso_set_default_signature_method(LassoSignatureMethod meth);
db363b
 
db363b
 #ifdef __cplusplus
db363b
 }
db363b
diff --git a/lasso/lasso.c b/lasso/lasso.c
db363b
index 087485998..67340317d 100644
db363b
--- a/lasso/lasso.c
db363b
+++ b/lasso/lasso.c
db363b
@@ -149,6 +149,44 @@ lasso_xmlsec_errors_callback(const char *file G_GNUC_UNUSED, int line G_GNUC_UNU
db363b
 	g_log("libxmlsec", G_LOG_LEVEL_DEBUG, "libxmlsec: %s:%d:%s:%s:%s:%s:%s", file, line, func, errorObject, errorSubject, xmlSecErrorsGetMsg(reason), msg);
db363b
 }
db363b
 
db363b
+static int
db363b
+set_default_signature_method()
db363b
+{
db363b
+	int rv = LASSO_ERROR_UNDEFINED;
db363b
+
db363b
+	if (lasso_strisequal(DEFAULT_SIGNING_ALGO, "rsa-sha256")) {
db363b
+		lasso_set_default_signature_method(LASSO_SIGNATURE_METHOD_RSA_SHA256);
db363b
+		rv = 0;
db363b
+	} else if (lasso_strisequal(DEFAULT_SIGNING_ALGO, "rsa-sha1")) {
db363b
+		lasso_set_default_signature_method(LASSO_SIGNATURE_METHOD_RSA_SHA1);
db363b
+		rv = 0;
db363b
+	}
db363b
+
db363b
+	return rv;
db363b
+}
db363b
+
db363b
+static int
db363b
+set_min_allowed_hash_algo()
db363b
+{
db363b
+	int rv = LASSO_ERROR_UNDEFINED;
db363b
+
db363b
+	if (lasso_strisequal(MIN_HASH_ALGO, "sha1")) {
db363b
+		lasso_set_min_signature_method(LASSO_SIGNATURE_METHOD_RSA_SHA1);
db363b
+		rv = 0;
db363b
+	} else if (lasso_strisequal(MIN_HASH_ALGO, "sha256")) {
db363b
+		lasso_set_min_signature_method(LASSO_SIGNATURE_METHOD_RSA_SHA256);
db363b
+		rv = 0;
db363b
+	} else if (lasso_strisequal(MIN_HASH_ALGO, "sha384")) {
db363b
+		lasso_set_min_signature_method(LASSO_SIGNATURE_METHOD_RSA_SHA384);
db363b
+		rv = 0;
db363b
+	} else if (lasso_strisequal(MIN_HASH_ALGO, "sha512")) {
db363b
+		lasso_set_min_signature_method(LASSO_SIGNATURE_METHOD_RSA_SHA512);
db363b
+		rv = 0;
db363b
+	}
db363b
+
db363b
+	return rv;
db363b
+}
db363b
+
db363b
 /**
db363b
  * lasso_init:
db363b
  *
db363b
@@ -164,6 +202,19 @@ int lasso_init()
db363b
 	g_type_init();
db363b
 #endif
db363b
 
db363b
+	/* Set the default hash algo */
db363b
+	if (set_default_signature_method() != 0) {
db363b
+		message(G_LOG_LEVEL_CRITICAL, "Unsupported signature "
db363b
+			"algorithm "DEFAULT_SIGNING_ALGO" configured");
db363b
+		return LASSO_ERROR_UNDEFINED;
db363b
+	}
db363b
+	if (set_min_allowed_hash_algo() != 0) {
db363b
+		message(G_LOG_LEVEL_CRITICAL, "Unsupported hash algorithm "
db363b
+			"algorithm "MIN_HASH_ALGO" configured");
db363b
+		return LASSO_ERROR_UNDEFINED;
db363b
+	}
db363b
+
db363b
+
db363b
 	/* Init Lasso classes */
db363b
 	for (i=0; functions[i]; i++)
db363b
 		functions[i]();
db363b
diff --git a/lasso/xml/tools.c b/lasso/xml/tools.c
db363b
index 290fd55f2..ce322ee1f 100644
db363b
--- a/lasso/xml/tools.c
db363b
+++ b/lasso/xml/tools.c
db363b
@@ -1505,16 +1505,6 @@ lasso_saml_constrain_dsigctxt(xmlSecDSigCtxPtr dsigCtx) {
db363b
 			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformExclC14NWithCommentsId) < 0) ||
db363b
 			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformInclC14N11Id) < 0) ||
db363b
 			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformInclC14N11WithCommentsId) < 0) ||
db363b
-			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformSha1Id) < 0) ||
db363b
-			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformHmacSha1Id) < 0) ||
db363b
-			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformDsaSha1Id) < 0) ||
db363b
-			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformRsaSha1Id) < 0) ||
db363b
-			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformSha256Id) < 0) ||
db363b
-			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformHmacSha256Id) < 0) ||
db363b
-			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformRsaSha256Id) < 0) ||
db363b
-			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformSha384Id) < 0) ||
db363b
-			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformHmacSha384Id) < 0) ||
db363b
-			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformRsaSha384Id) < 0) ||
db363b
 			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformSha512Id) < 0) ||
db363b
 			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformHmacSha512Id) < 0) ||
db363b
 			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformRsaSha512Id) < 0)
db363b
@@ -1523,15 +1513,62 @@ lasso_saml_constrain_dsigctxt(xmlSecDSigCtxPtr dsigCtx) {
db363b
 		message(G_LOG_LEVEL_CRITICAL, "Error: failed to limit allowed signature transforms");
db363b
 		return FALSE;
db363b
 	}
db363b
+
db363b
+	if (lasso_get_min_signature_method() <= LASSO_SIGNATURE_METHOD_RSA_SHA384) {
db363b
+		if ((xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformSha384Id) < 0) ||
db363b
+			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformHmacSha384Id) < 0) ||
db363b
+			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformRsaSha384Id) < 0)) {
db363b
+
db363b
+			message(G_LOG_LEVEL_CRITICAL, "Error: failed to limit allowed sha384 signature transforms");
db363b
+			return FALSE;
db363b
+		}
db363b
+
db363b
+		if (xmlSecDSigCtxEnableReferenceTransform(dsigCtx, xmlSecTransformSha384Id) < 0) {
db363b
+
db363b
+			message(G_LOG_LEVEL_CRITICAL, "Error: failed to limit allowed sha384 reference transforms");
db363b
+			return FALSE;
db363b
+		}
db363b
+	}
db363b
+
db363b
+	if (lasso_get_min_signature_method() <= LASSO_SIGNATURE_METHOD_RSA_SHA256) {
db363b
+		if ((xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformSha256Id) < 0) ||
db363b
+			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformHmacSha256Id) < 0) ||
db363b
+			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformRsaSha256Id) < 0)) {
db363b
+
db363b
+			message(G_LOG_LEVEL_CRITICAL, "Error: failed to limit allowed sha256 signature transforms");
db363b
+			return FALSE;
db363b
+		}
db363b
+
db363b
+		if (xmlSecDSigCtxEnableReferenceTransform(dsigCtx, xmlSecTransformSha256Id) < 0) {
db363b
+
db363b
+			message(G_LOG_LEVEL_CRITICAL, "Error: failed to limit allowed sha256 reference transforms");
db363b
+			return FALSE;
db363b
+		}
db363b
+	}
db363b
+
db363b
+	if (lasso_get_min_signature_method() <= LASSO_SIGNATURE_METHOD_RSA_SHA1) {
db363b
+		if ((xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformSha1Id) < 0) ||
db363b
+			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformHmacSha1Id) < 0) ||
db363b
+			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformDsaSha1Id) < 0) ||
db363b
+			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformRsaSha1Id) < 0)) {
db363b
+
db363b
+			message(G_LOG_LEVEL_CRITICAL, "Error: failed to limit allowed sha1 signature transforms");
db363b
+			return FALSE;
db363b
+		}
db363b
+
db363b
+		if (xmlSecDSigCtxEnableReferenceTransform(dsigCtx, xmlSecTransformSha1Id) < 0) {
db363b
+
db363b
+			message(G_LOG_LEVEL_CRITICAL, "Error: failed to limit allowed sha1 reference transforms");
db363b
+			return FALSE;
db363b
+		}
db363b
+	}
db363b
+
db363b
 	if((xmlSecDSigCtxEnableReferenceTransform(dsigCtx, xmlSecTransformInclC14NId) < 0) ||
db363b
 			(xmlSecDSigCtxEnableReferenceTransform(dsigCtx, xmlSecTransformExclC14NId) < 0) ||
db363b
 			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformInclC14NWithCommentsId) < 0) ||
db363b
 			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformExclC14NWithCommentsId) < 0) ||
db363b
 			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformInclC14N11Id) < 0) ||
db363b
 			(xmlSecDSigCtxEnableSignatureTransform(dsigCtx, xmlSecTransformInclC14N11WithCommentsId) < 0) ||
db363b
-			(xmlSecDSigCtxEnableReferenceTransform(dsigCtx, xmlSecTransformSha1Id) < 0) ||
db363b
-			(xmlSecDSigCtxEnableReferenceTransform(dsigCtx, xmlSecTransformSha256Id) < 0) ||
db363b
-			(xmlSecDSigCtxEnableReferenceTransform(dsigCtx, xmlSecTransformSha384Id) < 0) ||
db363b
 			(xmlSecDSigCtxEnableReferenceTransform(dsigCtx, xmlSecTransformSha512Id) < 0) ||
db363b
 			(xmlSecDSigCtxEnableReferenceTransform(dsigCtx, xmlSecTransformEnvelopedId) < 0)) {
db363b
 
db363b
diff --git a/lasso/xml/xml.c b/lasso/xml/xml.c
db363b
index 938844baf..f017ebbe3 100644
db363b
--- a/lasso/xml/xml.c
db363b
+++ b/lasso/xml/xml.c
db363b
@@ -91,6 +91,10 @@ GHashTable *dst_services_by_prefix = NULL; /* ID-WSF 1 extra DST services, index
db363b
 GHashTable *idwsf2_dst_services_by_href = NULL; /* ID-WSF 2 DST services, indexed on href */
db363b
 GHashTable *idwsf2_dst_services_by_prefix = NULL; /* ID-WSF 2 DST services, indexed on prefix */
db363b
 
db363b
+
db363b
+static LassoSignatureMethod default_signature_method = LASSO_SIGNATURE_METHOD_RSA_SHA1;
db363b
+static LassoSignatureMethod min_signature_method = LASSO_SIGNATURE_METHOD_RSA_SHA1;
db363b
+
db363b
 /*****************************************************************************/
db363b
 /* global methods                                                            */
db363b
 /*****************************************************************************/
db363b
@@ -3689,3 +3693,23 @@ lasso_node_new_from_saml2_query(const char *url_or_qs, const char *param_name, L
db363b
 cleanup:
db363b
 	return result;
db363b
 }
db363b
+
db363b
+LassoSignatureMethod
db363b
+lasso_get_default_signature_method() {
db363b
+	return default_signature_method;
db363b
+}
db363b
+
db363b
+void
db363b
+lasso_set_default_signature_method(LassoSignatureMethod meth) {
db363b
+	default_signature_method = meth;
db363b
+}
db363b
+
db363b
+LassoSignatureMethod
db363b
+lasso_get_min_signature_method() {
db363b
+	return min_signature_method;
db363b
+}
db363b
+
db363b
+void
db363b
+lasso_set_min_signature_method(LassoSignatureMethod meth) {
db363b
+	min_signature_method = meth;
db363b
+}
db363b
diff --git a/lasso/xml/xml.h b/lasso/xml/xml.h
db363b
index 7660a0647..d0d3e1b0d 100644
db363b
--- a/lasso/xml/xml.h
db363b
+++ b/lasso/xml/xml.h
db363b
@@ -116,6 +116,15 @@ typedef enum {
db363b
 	LASSO_SIGNATURE_METHOD_LAST
db363b
 } LassoSignatureMethod;
db363b
 
db363b
+/* signature method and hash strength */
db363b
+LassoSignatureMethod lasso_get_default_signature_method();
db363b
+
db363b
+void lasso_set_default_signature_method(LassoSignatureMethod meth);
db363b
+
db363b
+LassoSignatureMethod lasso_get_min_signature_method();
db363b
+
db363b
+void lasso_set_min_signature_method(LassoSignatureMethod meth);
db363b
+
db363b
 static inline gboolean
db363b
 lasso_validate_signature_method(LassoSignatureMethod signature_method)
db363b
 {
db363b
diff --git a/tests/random_tests.c b/tests/random_tests.c
db363b
index fa0367a3c..cf112c7e2 100644
db363b
--- a/tests/random_tests.c
db363b
+++ b/tests/random_tests.c
db363b
@@ -97,7 +97,7 @@ START_TEST(test01_server_new)
db363b
 	fail_unless(server->private_key != NULL);
db363b
 	fail_unless(server->private_key_password == NULL);
db363b
 	fail_unless(server->certificate != NULL);
db363b
-	fail_unless(server->signature_method == LASSO_SIGNATURE_METHOD_RSA_SHA1);
db363b
+	fail_unless(server->signature_method == lasso_get_default_signature_method());
db363b
 	fail_unless(provider->ProviderID != NULL);
db363b
 	fail_unless(provider->role == 0);
db363b
 	fail_unless(g_file_get_contents(TESTSDATADIR "/idp1-la/metadata.xml", &content, &len, NULL));
db363b
@@ -115,7 +115,7 @@ START_TEST(test01_server_new)
db363b
 	fail_unless(server->private_key != NULL);
db363b
 	fail_unless(server->private_key_password == NULL);
db363b
 	fail_unless(server->certificate != NULL);
db363b
-	fail_unless(server->signature_method == LASSO_SIGNATURE_METHOD_RSA_SHA1);
db363b
+	fail_unless(server->signature_method == lasso_get_default_signature_method());
db363b
 	fail_unless(server->providers != NULL);
db363b
 	fail_unless(provider->ProviderID != NULL);
db363b
 	fail_unless(provider->role == 0, "provider->role != 0 => provider :=  %d", provider->role);
db363b
@@ -143,7 +143,7 @@ START_TEST(test02_server_add_provider)
db363b
 	fail_unless(server->private_key != NULL);
db363b
 	fail_unless(! server->private_key_password);
db363b
 	fail_unless(server->certificate != NULL);
db363b
-	fail_unless(server->signature_method == LASSO_SIGNATURE_METHOD_RSA_SHA1);
db363b
+	fail_unless(server->signature_method == lasso_get_default_signature_method());
db363b
 	fail_unless(server->providers != NULL);
db363b
 	lasso_server_add_provider(
db363b
 			server,
db363b
-- 
db363b
2.26.3
db363b