Commit 2628d51819 for strongswan.org

commit 2628d518190d4a6c51b1b006a62814af6eb3253f
Author: Tobias Brunner <tobias@strongswan.org>
Date:   Wed Nov 18 15:13:02 2026 +0100

    auth-cfg: Add support for ECDSA curve constraints for IKEv2 authentication

    This adds a specific marker if a curve could not be determined, so we
    definitely fail if that happens.

    The local constraints are only used when searching a key by identity and
    no specific KEY_ID or certificate is configured, which force a
    specific key to get selected anyway.

    The fallback from chain- to IKE-specific constraints is the same as that
    for signature schemes but handled independently (i.e. an IKE-specific
    signature scheme constraint still causes curve constraints to get added
    automatically if none are explicitly configured and vice-versa).

diff --git a/conf/options/charon.opt b/conf/options/charon.opt
index 2f95552f7b..f7db7a6f90 100644
--- a/conf/options/charon.opt
+++ b/conf/options/charon.opt
@@ -455,10 +455,11 @@ charon.signature_authentication = yes
 charon.signature_authentication_constraints = yes
 	Whether to enable constraints against IKEv2 signature schemes.

-	If enabled, signature schemes configured in _rightauth_, in addition to
-	getting used as constraints against signature schemes employed in the
-	certificate chain, are also used as constraints against the signature scheme
-	used by peers during IKEv2.
+	If enabled, signature schemes configured in the local/remote _auth_ setting,
+	in addition to getting used as constraints against signature schemes
+	employed in the certificate chain, are also used as constraints against the
+	signature scheme used by peers during IKEv2. The same applies for ECDSA
+	curve constraints.

 charon.spi_label = 0x0000000000000000
 	Value mixed into the local IKE SPIs after applying _spi_mask_.
diff --git a/src/libcharon/sa/ikev2/authenticators/pubkey_authenticator.c b/src/libcharon/sa/ikev2/authenticators/pubkey_authenticator.c
index bf63bda96b..3418cd2405 100644
--- a/src/libcharon/sa/ikev2/authenticators/pubkey_authenticator.c
+++ b/src/libcharon/sa/ikev2/authenticators/pubkey_authenticator.c
@@ -706,6 +706,7 @@ METHOD(authenticator_t, process, status_t,
 			auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
 			auth->add(auth, AUTH_RULE_IKE_SIGNATURE_SCHEME,
 					  signature_params_clone(params));
+			auth_cfg_add_ecdsa_curve(auth, AUTH_RULE_IKE_ECDSA_CURVE, public);
 			if (!online)
 			{
 				auth->add(auth, AUTH_RULE_CERT_VALIDATION_SUSPENDED, TRUE);
diff --git a/src/libstrongswan/credentials/auth_cfg.c b/src/libstrongswan/credentials/auth_cfg.c
index 3952de02e3..a46cdb358e 100644
--- a/src/libstrongswan/credentials/auth_cfg.c
+++ b/src/libstrongswan/credentials/auth_cfg.c
@@ -54,6 +54,7 @@ ENUM(auth_rule_names, AUTH_RULE_IDENTITY, AUTH_HELPER_AC_CERT,
 	"RULE_RSA_STRENGTH",
 	"RULE_ECDSA_STRENGTH",
 	"RULE_ECDSA_CURVE",
+	"RULE_IKE_ECDSA_CURVE",
 	"RULE_SIGNATURE_SCHEME",
 	"RULE_IKE_SIGNATURE_SCHEME",
 	"RULE_CERT_POLICY",
@@ -86,6 +87,7 @@ ENUM(auth_rule_names_lower, AUTH_RULE_IDENTITY, AUTH_HELPER_AC_CERT,
 	"rsa_strength",
 	"ecdsa_strength",
 	"ecdsa_curve",
+	"ike_ecdsa_curve",
 	"signature_scheme",
 	"ike_signature_scheme",
 	"cert_policy",
@@ -129,6 +131,7 @@ static inline bool is_multi_value_rule(auth_rule_t type)
 		case AUTH_RULE_RSA_STRENGTH:
 		case AUTH_RULE_ECDSA_STRENGTH:
 		case AUTH_RULE_ECDSA_CURVE:
+		case AUTH_RULE_IKE_ECDSA_CURVE:
 		case AUTH_RULE_SIGNATURE_SCHEME:
 		case AUTH_RULE_IKE_SIGNATURE_SCHEME:
 		case AUTH_HELPER_IM_CERT:
@@ -266,6 +269,7 @@ static void init_entry(entry_t *this, auth_rule_t type, va_list args)
 		case AUTH_RULE_SUBJECT_CERT:
 		case AUTH_RULE_CERT_POLICY:
 		case AUTH_RULE_ECDSA_CURVE:
+		case AUTH_RULE_IKE_ECDSA_CURVE:
 		case AUTH_RULE_SIGNATURE_SCHEME:
 		case AUTH_RULE_IKE_SIGNATURE_SCHEME:
 		case AUTH_HELPER_IM_CERT:
@@ -342,6 +346,7 @@ static bool entry_equals(entry_t *e1, entry_t *e2)
 		}
 		case AUTH_RULE_CERT_POLICY:
 		case AUTH_RULE_ECDSA_CURVE:
+		case AUTH_RULE_IKE_ECDSA_CURVE:
 		case AUTH_RULE_XAUTH_BACKEND:
 		case AUTH_HELPER_IM_HASH_URL:
 		case AUTH_HELPER_SUBJECT_HASH_URL:
@@ -386,6 +391,7 @@ static void destroy_entry_value(entry_t *entry)
 		}
 		case AUTH_RULE_CERT_POLICY:
 		case AUTH_RULE_ECDSA_CURVE:
+		case AUTH_RULE_IKE_ECDSA_CURVE:
 		case AUTH_RULE_XAUTH_BACKEND:
 		case AUTH_HELPER_IM_HASH_URL:
 		case AUTH_HELPER_SUBJECT_HASH_URL:
@@ -454,6 +460,7 @@ static void replace(private_auth_cfg_t *this, entry_enumerator_t *enumerator,
 			case AUTH_RULE_SUBJECT_CERT:
 			case AUTH_RULE_CERT_POLICY:
 			case AUTH_RULE_ECDSA_CURVE:
+			case AUTH_RULE_IKE_ECDSA_CURVE:
 			case AUTH_RULE_SIGNATURE_SCHEME:
 			case AUTH_RULE_IKE_SIGNATURE_SCHEME:
 			case AUTH_HELPER_IM_CERT:
@@ -535,6 +542,7 @@ METHOD(auth_cfg_t, get, void*,
 		case AUTH_RULE_SUBJECT_CERT:
 		case AUTH_RULE_CERT_POLICY:
 		case AUTH_RULE_ECDSA_CURVE:
+		case AUTH_RULE_IKE_ECDSA_CURVE:
 		case AUTH_RULE_SIGNATURE_SCHEME:
 		case AUTH_RULE_IKE_SIGNATURE_SCHEME:
 		case AUTH_HELPER_IM_CERT:
@@ -660,7 +668,7 @@ METHOD(auth_cfg_t, add_pubkey_constraints, void,
 	private_auth_cfg_t *this, char* constraints, bool ike)
 {
 	enumerator_t *enumerator;
-	bool ike_added = FALSE, rsa_pss;
+	bool ike_sig_added = FALSE, ike_curve_added = FALSE, rsa_pss;
 	key_type_t expected_type = -1;
 	auth_rule_t expected_strength = AUTH_RULE_MAX;
 	signature_params_t *params;
@@ -738,7 +746,15 @@ METHOD(auth_cfg_t, add_pubkey_constraints, void,
 				chunk_free(&curve);
 				if (name)
 				{
-					add(this, AUTH_RULE_ECDSA_CURVE, name);
+					if (strpfx(key_token, "ike:"))
+					{
+						add(this, AUTH_RULE_IKE_ECDSA_CURVE, name);
+						ike_curve_added = TRUE;
+					}
+					else
+					{
+						add(this, AUTH_RULE_ECDSA_CURVE, name);
+					}
 					continue;
 				}
 			}
@@ -752,7 +768,7 @@ METHOD(auth_cfg_t, add_pubkey_constraints, void,
 				if (strpfx(key_token, "ike:"))
 				{
 					add(this, AUTH_RULE_IKE_SIGNATURE_SCHEME, params);
-					ike_added = TRUE;
+					ike_sig_added = TRUE;
 				}
 				else
 				{
@@ -774,7 +790,7 @@ METHOD(auth_cfg_t, add_pubkey_constraints, void,
 						if (strpfx(key_token, "ike:"))
 						{
 							add(this, AUTH_RULE_IKE_SIGNATURE_SCHEME, params);
-							ike_added = TRUE;
+							ike_sig_added = TRUE;
 						}
 						else
 						{
@@ -797,7 +813,7 @@ METHOD(auth_cfg_t, add_pubkey_constraints, void,
 						if (strpfx(key_token, "ike:"))
 						{
 							add(this, AUTH_RULE_IKE_SIGNATURE_SCHEME, params);
-							ike_added = TRUE;
+							ike_sig_added = TRUE;
 						}
 						else
 						{
@@ -815,9 +831,9 @@ METHOD(auth_cfg_t, add_pubkey_constraints, void,
 	}
 	enumerator->destroy(enumerator);

-	/* if no explicit IKE signature constraints were added we add them for all
-	 * configured signature constraints */
-	if (ike && !ike_added &&
+	/* if explicit IKE signature constraints or ECDSA curves are missing, we
+	 * mirror the configured constraints */
+	if (ike && (!ike_sig_added || !ike_curve_added) &&
 		lib->settings->get_bool(lib->settings,
 							"%s.signature_authentication_constraints", TRUE,
 							lib->ns))
@@ -825,11 +841,15 @@ METHOD(auth_cfg_t, add_pubkey_constraints, void,
 		enumerator = create_enumerator(this);
 		while (enumerator->enumerate(enumerator, &type, &value))
 		{
-			if (type == AUTH_RULE_SIGNATURE_SCHEME)
+			if (type == AUTH_RULE_SIGNATURE_SCHEME && !ike_sig_added)
 			{
 				add(this, AUTH_RULE_IKE_SIGNATURE_SCHEME,
 					signature_params_clone(value));
 			}
+			else if (type == AUTH_RULE_ECDSA_CURVE && !ike_curve_added)
+			{
+				add(this, AUTH_RULE_IKE_ECDSA_CURVE, strdup(value));
+			}
 		}
 		enumerator->destroy(enumerator);
 	}
@@ -839,28 +859,20 @@ METHOD(auth_cfg_t, add_pubkey_constraints, void,
  * Check if ECDSA curves are compliant
  */
 static bool complies_curve(private_auth_cfg_t *this, auth_cfg_t *constraints,
-						   bool log_error)
+						   auth_rule_t type, bool log_error)
 {
 	enumerator_t *e1, *e2;
 	auth_rule_t t1, t2;
 	char *curve, *allowed;
-	int ecdsa = 0, curves = 0;
 	bool success = TRUE;

 	e2 = create_enumerator(this);
 	while (e2->enumerate(e2, &t2, &curve))
 	{
-		if (t2 == AUTH_RULE_ECDSA_STRENGTH)
-		{	/* at least one cert used ECDSA, keep track in case we have no
-			 * information about the curves */
-			ecdsa++;
-			continue;
-		}
-		if (t2 != AUTH_RULE_ECDSA_CURVE)
+		if (t2 != type)
 		{
 			continue;
 		}
-		curves++;
 		success = FALSE;
 		e1 = constraints->create_enumerator(constraints);
 		while (e1->enumerate(e1, &t1, &allowed))
@@ -876,22 +888,21 @@ static bool complies_curve(private_auth_cfg_t *this, auth_cfg_t *constraints,
 		{
 			if (log_error)
 			{
-				DBG1(DBG_CFG, "ECDSA curve %s not acceptable", curve);
+				if (streq(curve, AUTH_CFG_ECDSA_CURVE_UNKNOWN))
+				{
+					DBG1(DBG_CFG, "unable to determine ECDSA curve for %s",
+						 AUTH_RULE_ECDSA_CURVE == type ? "X.509" : "IKE");
+				}
+				else
+				{
+					DBG1(DBG_CFG, "ECDSA curve %s not acceptable for %s", curve,
+						 AUTH_RULE_ECDSA_CURVE == type ? "X.509" : "IKE");
+				}
 			}
 			break;
 		}
 	}
 	e2->destroy(e2);
-
-	if (success && ecdsa > curves)
-	{
-		success = FALSE;
-		if (log_error)
-		{
-			DBG1(DBG_CFG, "unable to determine ECDSA curve for %d keys",
-				 ecdsa - curves);
-		}
-	}
 	return success;
 }

@@ -944,7 +955,8 @@ METHOD(auth_cfg_t, complies, bool,
 {
 	enumerator_t *e1, *e2;
 	bool success = TRUE, group_match = FALSE;
-	bool ca_match = FALSE, cert_match = FALSE, curves = FALSE;
+	bool ca_match = FALSE, cert_match = FALSE;
+	bool curves = FALSE, ike_curves = FALSE;
 	identification_t *require_group = NULL, *require_ca = NULL;
 	certificate_t *require_cert = NULL;
 	signature_params_t *ike_scheme = NULL, *scheme = NULL;
@@ -1201,6 +1213,11 @@ METHOD(auth_cfg_t, complies, bool,
 				curves = TRUE;
 				break;
 			}
+			case AUTH_RULE_IKE_ECDSA_CURVE:
+			{
+				ike_curves = TRUE;
+				break;
+			}
 			case AUTH_RULE_CERT_POLICY:
 			{
 				char *oid1, *oid2;
@@ -1260,7 +1277,13 @@ METHOD(auth_cfg_t, complies, bool,
 	}
 	if (success && curves)
 	{
-		success = complies_curve(this, constraints, log_error);
+		success = complies_curve(this, constraints, AUTH_RULE_ECDSA_CURVE,
+								 log_error);
+	}
+	if (success && ike_curves)
+	{
+		success = complies_curve(this, constraints, AUTH_RULE_IKE_ECDSA_CURVE,
+								 log_error);
 	}

 	/* Check if we have a matching constraint (or none at all) for used
@@ -1375,6 +1398,7 @@ static void *clone_value(auth_rule_t type, void *value)
 		case AUTH_RULE_XAUTH_BACKEND:
 		case AUTH_RULE_CERT_POLICY:
 		case AUTH_RULE_ECDSA_CURVE:
+		case AUTH_RULE_IKE_ECDSA_CURVE:
 		case AUTH_HELPER_IM_HASH_URL:
 		case AUTH_HELPER_SUBJECT_HASH_URL:
 		{
@@ -1438,6 +1462,7 @@ static void merge(private_auth_cfg_t *this, private_auth_cfg_t *other, bool copy
 				case AUTH_RULE_XAUTH_BACKEND:
 				case AUTH_RULE_CERT_POLICY:
 				case AUTH_RULE_ECDSA_CURVE:
+				case AUTH_RULE_IKE_ECDSA_CURVE:
 				case AUTH_HELPER_IM_HASH_URL:
 				case AUTH_HELPER_SUBJECT_HASH_URL:
 					add(this, type, value);
@@ -1579,6 +1604,7 @@ METHOD(auth_cfg_t, clone_, auth_cfg_t*,
 			case AUTH_RULE_XAUTH_BACKEND:
 			case AUTH_RULE_CERT_POLICY:
 			case AUTH_RULE_ECDSA_CURVE:
+			case AUTH_RULE_IKE_ECDSA_CURVE:
 			case AUTH_HELPER_IM_HASH_URL:
 			case AUTH_HELPER_SUBJECT_HASH_URL:
 				clone->add(clone, type, value);
@@ -1636,3 +1662,29 @@ auth_cfg_t *auth_cfg_create()

 	return &this->public;
 }
+
+/*
+ * Described in header
+ */
+void auth_cfg_add_ecdsa_curve(auth_cfg_t *cfg, auth_rule_t type,
+							  public_key_t *key)
+{
+	chunk_t curve;
+	char *name = NULL;
+
+	if (key->get_type(key) != KEY_ECDSA ||
+		(type != AUTH_RULE_ECDSA_CURVE && type != AUTH_RULE_IKE_ECDSA_CURVE))
+	{
+		return;
+	}
+	if (key->get_encoding(key, PUBKEY_ECDSA_CURVE_DER, &curve))
+	{
+		name = ecdsa_curve_to_string(curve);
+		chunk_free(&curve);
+	}
+	if (!name)
+	{
+		name = strdup(AUTH_CFG_ECDSA_CURVE_UNKNOWN);
+	}
+	cfg->add(cfg, type, name);
+}
diff --git a/src/libstrongswan/credentials/auth_cfg.h b/src/libstrongswan/credentials/auth_cfg.h
index 7384a05adc..54a216adfa 100644
--- a/src/libstrongswan/credentials/auth_cfg.h
+++ b/src/libstrongswan/credentials/auth_cfg.h
@@ -23,6 +23,7 @@
 #ifndef AUTH_CFG_H_
 #define AUTH_CFG_H_

+#include <credentials/keys/public_key.h>
 #include <collections/enumerator.h>

 typedef struct auth_cfg_t auth_cfg_t;
@@ -109,6 +110,8 @@ enum auth_rule_t {
 	AUTH_RULE_ECDSA_STRENGTH,
 	/** required ECDSA curve, name/OID (via ecdsa_curve_to_string()) as char* */
 	AUTH_RULE_ECDSA_CURVE,
+	/** required ECDSA curve for IKE authentication, name/OID as char* */
+	AUTH_RULE_IKE_ECDSA_CURVE,
 	/** required signature scheme, signature_params_t* */
 	AUTH_RULE_SIGNATURE_SCHEME,
 	/** required signature scheme for IKE authentication, signature_params_t* */
@@ -133,6 +136,12 @@ enum auth_rule_t {
 	AUTH_RULE_MAX,
 };

+/**
+ * Marker used with AUTH_RULE_(IKE_)ECDSA_CURVE if an ECDSA curve could not be
+ * determined.
+ */
+#define AUTH_CFG_ECDSA_CURVE_UNKNOWN "<unknown>"
+
 /**
  * Names for auth_rule_t (upper case).
  */
@@ -292,4 +301,16 @@ auth_cfg_t *auth_cfg_create();
  */
 bool auth_cfg_is_pubkey_prefix(const char *str);

+/**
+ * Add the ECDSA curve of the given public key to the config.  If the curve
+ * can't be determined, a marker is added to check against in
+ * authc_cfg_t::complies().
+ *
+ * @param cfg			config to add the curve to
+ * @param type			AUTH_RULE_ECDSA_CURVE or AUTH_RULE_IKE_ECDSA_CURVE
+ * @param key			public key (only ECDSA keys are handled)
+ */
+void auth_cfg_add_ecdsa_curve(auth_cfg_t *cfg, auth_rule_t type,
+							  public_key_t *key);
+
 #endif /** AUTH_CFG_H_ @}*/
diff --git a/src/libstrongswan/credentials/credential_manager.c b/src/libstrongswan/credentials/credential_manager.c
index a49b8c9619..127555454c 100644
--- a/src/libstrongswan/credentials/credential_manager.c
+++ b/src/libstrongswan/credentials/credential_manager.c
@@ -708,25 +708,6 @@ static certificate_t *get_issuer_cert(private_credential_manager_t *this,
 	return issuer;
 }

-/**
- * Get the ECDSA curve from the given public key, add it to auth
- */
-static void get_ecdsa_curve(public_key_t *key, auth_cfg_t *auth)
-{
-	chunk_t curve;
-	char *name;
-
-	if (key->get_encoding(key, PUBKEY_ECDSA_CURVE_DER, &curve))
-	{
-		name = ecdsa_curve_to_string(curve);
-		if (name)
-		{
-			auth->add(auth, AUTH_RULE_ECDSA_CURVE, name);
-		}
-		chunk_free(&curve);
-	}
-}
-
 /**
  * Get the strength of certificate, add it to auth
  */
@@ -750,7 +731,7 @@ static void get_key_strength(certificate_t *cert, auth_cfg_t *auth)
 				break;
 			case KEY_ECDSA:
 				auth->add(auth, AUTH_RULE_ECDSA_STRENGTH, strength);
-				get_ecdsa_curve(key, auth);
+				auth_cfg_add_ecdsa_curve(auth, AUTH_RULE_ECDSA_CURVE, key);
 				break;
 			default:
 				break;
@@ -1208,11 +1189,53 @@ static auth_cfg_t *build_trustchain(private_credential_manager_t *this,
 	return trustchain;
 }

+/**
+ * Make sure the public key matches any configured ECDSA curve constraints.
+ *
+ * If any constraints are found, only ECDSA keys will be allowed.
+ */
+static bool match_ecdsa_curve(auth_cfg_t *auth, public_key_t *public)
+{
+	enumerator_t *enumerator;
+	auth_rule_t rule;
+	chunk_t curve;
+	char *allowed, *name = NULL;
+	bool constrained = FALSE, matched = FALSE;
+
+	if (!auth)
+	{
+		return TRUE;
+	}
+	enumerator = auth->create_enumerator(auth);
+	while (enumerator->enumerate(enumerator, &rule, &allowed))
+	{
+		if (rule == AUTH_RULE_IKE_ECDSA_CURVE)
+		{
+			constrained = TRUE;
+			if (!name && public->get_type(public) == KEY_ECDSA &&
+				public->get_encoding(public, PUBKEY_ECDSA_CURVE_DER, &curve))
+			{
+				name = ecdsa_curve_to_string(curve);
+				chunk_free(&curve);
+			}
+			if (name && streq(name, allowed))
+			{
+				matched = TRUE;
+				break;
+			}
+		}
+	}
+	enumerator->destroy(enumerator);
+	free(name);
+	return !constrained || matched;
+}
+
 /**
  * find a private key of a given certificate
  */
 static private_key_t *get_private_by_cert(private_credential_manager_t *this,
-										  certificate_t *cert, key_type_t type)
+										  auth_cfg_t *auth, certificate_t *cert,
+										  key_type_t type)
 {
 	private_key_t *private = NULL;
 	identification_t *keyid;
@@ -1222,7 +1245,8 @@ static private_key_t *get_private_by_cert(private_credential_manager_t *this,
 	public = cert->get_public_key(cert);
 	if (public)
 	{
-		if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
+		if (match_ecdsa_curve(auth, public) &&
+			public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
 		{
 			keyid = identification_create_from_encoding(ID_KEY_ID, chunk);
 			private = get_private_by_keyid(this, type, keyid);
@@ -1288,7 +1312,7 @@ METHOD(credential_manager_t, get_private, private_key_t*,
 		{
 			if (rule == AUTH_RULE_SUBJECT_CERT)
 			{
-				private = get_private_by_cert(this, cert, type);
+				private = get_private_by_cert(this, NULL, cert, type);
 				if (private)
 				{
 					trustchain = build_trustchain(this, cert, auth, &found_anchor);
@@ -1324,7 +1348,7 @@ METHOD(credential_manager_t, get_private, private_key_t*,
 			enumerator = create_cert_enumerator(this, CERT_ANY, type, id, FALSE);
 			while (enumerator->enumerate(enumerator, &cert))
 			{
-				private = get_private_by_cert(this, cert, type);
+				private = get_private_by_cert(this, auth, cert, type);
 				if (private)
 				{
 					trustchain = build_trustchain(this, cert, auth, &found_anchor);
@@ -1373,7 +1397,7 @@ METHOD(credential_manager_t, get_private, private_key_t*,
 		enumerator = create_cert_enumerator(this, CERT_ANY, type, id, FALSE);
 		while (enumerator->enumerate(enumerator, &cert))
 		{
-			private = get_private_by_cert(this, cert, type);
+			private = get_private_by_cert(this, NULL, cert, type);
 			if (private)
 			{
 				break;
diff --git a/src/libstrongswan/tests/suites/test_auth_cfg.c b/src/libstrongswan/tests/suites/test_auth_cfg.c
index 667d50f880..70c20cf081 100644
--- a/src/libstrongswan/tests/suites/test_auth_cfg.c
+++ b/src/libstrongswan/tests/suites/test_auth_cfg.c
@@ -18,7 +18,7 @@

 #include <credentials/auth_cfg.h>

-struct {
+static struct {
 	char *constraints;
 	signature_scheme_t sig[5];
 	signature_scheme_t ike[5];
@@ -111,7 +111,7 @@ typedef union {
 	rsa_pss_params_t pss;
 } signature_param_types_t;

-struct {
+static struct {
 	char *constraints;
 	signature_scheme_t sig[5];
 	signature_param_types_t p[5];
@@ -177,7 +177,7 @@ START_TEST(test_sig_constraints_params)
 }
 END_TEST

-struct {
+static struct {
 	char *constraints;
 	signature_scheme_t sig[6];
 	signature_param_types_t p[6];
@@ -203,12 +203,15 @@ START_TEST(test_sig_constraints_rsa_pss)
 }
 END_TEST

-struct {
+static struct {
 	char *constraints;
 	char *curves[3];
+	char *ike[3];
 } curve_constraints_tests[] = {
-	{ "ecdsa-p256", { "prime256v1" }},
-	{ "ecdsa-bp384", { "brainpoolP384r1" }},
+	{ "ecdsa-p256", { "prime256v1" }, {}},
+	{ "ike:ecdsa-p256", {}, { "prime256v1" }},
+	{ "ecdsa-bp384", { "brainpoolP384r1" }, {}},
+	{ "ike:ecdsa-p256-ecdsa-bp384", { "brainpoolP384r1" }, { "prime256v1" }},
 	{ "ecdsa-brainpoolP256r1", { "brainpoolP256r1" }},
 	{ "ecdsa-1.3.36.3.3.2.8.1.1.7", { "brainpoolP256r1" }},
 	{ "ecdsa-1.3.6.1.4.1.36906.15", { "1.3.6.1.4.1.36906.15" }},
@@ -218,7 +221,8 @@ struct {
 	{ "rsa-p256", {}},
 };

-static void check_curve_constraints(auth_cfg_t *cfg, char *curves[])
+static void check_curve_constraints(auth_cfg_t *cfg, auth_rule_t type,
+									char *expected[])
 {
 	enumerator_t *enumerator;
 	auth_rule_t t;
@@ -228,28 +232,67 @@ static void check_curve_constraints(auth_cfg_t *cfg, char *curves[])
 	enumerator = cfg->create_enumerator(cfg);
 	while (enumerator->enumerate(enumerator, &t, &value))
 	{
-		if (t == AUTH_RULE_ECDSA_CURVE)
+		if (t == type)
 		{
-			ck_assert_str_eq(curves[i], value);
+			ck_assert_str_eq(expected[i], value);
 			i++;
 		}
 	}
 	enumerator->destroy(enumerator);
-	ck_assert(!curves[i]);
+	ck_assert(!expected[i]);
 }

 START_TEST(test_curve_constraints)
 {
 	auth_cfg_t *cfg;
+	char *none[] = {NULL};
+
+	cfg = auth_cfg_create();
+	cfg->add_pubkey_constraints(cfg, curve_constraints_tests[_i].constraints, FALSE);
+	check_curve_constraints(cfg, AUTH_RULE_ECDSA_CURVE,
+							curve_constraints_tests[_i].curves);
+	check_curve_constraints(cfg, AUTH_RULE_IKE_ECDSA_CURVE, none);
+	cfg->destroy(cfg);
+
+	lib->settings->set_bool(lib->settings, "%s.signature_authentication_constraints",
+							FALSE, lib->ns);

 	cfg = auth_cfg_create();
 	cfg->add_pubkey_constraints(cfg, curve_constraints_tests[_i].constraints, TRUE);
-	check_curve_constraints(cfg, curve_constraints_tests[_i].curves);
+	check_curve_constraints(cfg, AUTH_RULE_ECDSA_CURVE,
+							curve_constraints_tests[_i].curves);
+	check_curve_constraints(cfg, AUTH_RULE_IKE_ECDSA_CURVE,
+							curve_constraints_tests[_i].ike);
 	cfg->destroy(cfg);
 }
 END_TEST

-struct {
+START_TEST(test_curve_constraints_fallback)
+{
+	auth_cfg_t *cfg;
+
+	lib->settings->set_bool(lib->settings, "%s.signature_authentication_constraints",
+							TRUE, lib->ns);
+
+	cfg = auth_cfg_create();
+	cfg->add_pubkey_constraints(cfg, curve_constraints_tests[_i].constraints, TRUE);
+	check_curve_constraints(cfg, AUTH_RULE_ECDSA_CURVE,
+							curve_constraints_tests[_i].curves);
+	if (curve_constraints_tests[_i].ike[0])
+	{
+		check_curve_constraints(cfg, AUTH_RULE_IKE_ECDSA_CURVE,
+								curve_constraints_tests[_i].ike);
+	}
+	else
+	{
+		check_curve_constraints(cfg, AUTH_RULE_IKE_ECDSA_CURVE,
+								curve_constraints_tests[_i].curves);
+	}
+	cfg->destroy(cfg);
+}
+END_TEST
+
+static struct {
 	bool compliant;
 	char *constraints;
 	struct {
@@ -259,32 +302,43 @@ struct {
 } complies_curves_tests[] = {
 	{ TRUE, "ecdsa-p256", {}},
 	{ TRUE, "rsa-2048-ecdsa-p256", {}},
-	{ FALSE, "ecdsa-p256", {
+	{ TRUE, "ecdsa-p256", {
 		{ AUTH_RULE_ECDSA_STRENGTH, (void*)(uintptr_t)256 }, }},
 	{ TRUE, "ecdsa-256", {
 		{ AUTH_RULE_ECDSA_STRENGTH, (void*)(uintptr_t)256 }, }},
+	{ FALSE, "ecdsa-384", {
+		{ AUTH_RULE_ECDSA_STRENGTH, (void*)(uintptr_t)256 }, }},
 	{ TRUE, "ecdsa-256", {
-		{ AUTH_RULE_ECDSA_STRENGTH, (void*)(uintptr_t)256 },
 		{ AUTH_RULE_ECDSA_CURVE, "prime256v1" }, }},
-	{ TRUE, "ecdsa-p256", {
-		{ AUTH_RULE_ECDSA_STRENGTH, (void*)(uintptr_t)256 },
+	{ FALSE, "ecdsa-p384", {
 		{ AUTH_RULE_ECDSA_CURVE, "prime256v1" }, }},
+	{ FALSE, "ecdsa-p256", {
+		{ AUTH_RULE_ECDSA_CURVE, AUTH_CFG_ECDSA_CURVE_UNKNOWN }, }},
+	{ FALSE, "ecdsa-p256", {
+		{ AUTH_RULE_ECDSA_CURVE, "prime256v1" },
+		{ AUTH_RULE_ECDSA_CURVE, AUTH_CFG_ECDSA_CURVE_UNKNOWN }, }},
 	{ TRUE, "ecdsa-256-p256", {
 		{ AUTH_RULE_ECDSA_STRENGTH, (void*)(uintptr_t)256 },
 		{ AUTH_RULE_ECDSA_CURVE, "prime256v1" }, }},
-	{ FALSE, "ecdsa-p384", {
-		{ AUTH_RULE_ECDSA_STRENGTH, (void*)(uintptr_t)256 },
+	{ TRUE, "ecdsa-256-p256", {
+		{ AUTH_RULE_ECDSA_CURVE, "prime256v1" }, }},
+	{ TRUE, "ike:ecdsa-p256", {}},
+	{ TRUE, "ike:ecdsa-p256", {
 		{ AUTH_RULE_ECDSA_CURVE, "prime256v1" }, }},
+	{ TRUE, "ike:ecdsa-p256", {
+		{ AUTH_RULE_ECDSA_CURVE, "secp384r1" }, }},
+	{ TRUE, "ike:ecdsa-p256", {
+		{ AUTH_RULE_IKE_ECDSA_CURVE, "prime256v1" }, }},
+	{ FALSE, "ike:ecdsa-p384", {
+		{ AUTH_RULE_IKE_ECDSA_CURVE, "prime256v1" }, }},
+	{ FALSE, "ike:ecdsa-p384", {
+		{ AUTH_RULE_IKE_ECDSA_CURVE, AUTH_CFG_ECDSA_CURVE_UNKNOWN }, }},
 	{ TRUE, "ecdsa-p256-p384", {
-		{ AUTH_RULE_ECDSA_STRENGTH, (void*)(uintptr_t)256 },
 		{ AUTH_RULE_ECDSA_CURVE, "prime256v1" }, }},
 	{ TRUE, "ecdsa-p256-p384", {
-		{ AUTH_RULE_ECDSA_STRENGTH, (void*)(uintptr_t)384 },
 		{ AUTH_RULE_ECDSA_CURVE, "secp384r1" }, }},
 	{ TRUE, "ecdsa-p256-p384", {
-		{ AUTH_RULE_ECDSA_STRENGTH, (void*)(uintptr_t)384 },
 		{ AUTH_RULE_ECDSA_CURVE, "secp384r1" },
-		{ AUTH_RULE_ECDSA_STRENGTH, (void*)(uintptr_t)256 },
 		{ AUTH_RULE_ECDSA_CURVE, "prime256v1" }, }},
 };

@@ -299,6 +353,7 @@ static void add_rule(auth_cfg_t *cfg, auth_rule_t type, void *value)
 			cfg->add(cfg, type, (uintptr_t)value);
 			break;
 		case AUTH_RULE_ECDSA_CURVE:
+		case AUTH_RULE_IKE_ECDSA_CURVE:
 			cfg->add(cfg, type, strdup(value));
 			break;
 		default:
@@ -352,6 +407,7 @@ Suite *auth_cfg_suite_create()

 	tc = tcase_create("add_pubkey_constraints curves");
 	tcase_add_loop_test(tc, test_curve_constraints, 0, countof(curve_constraints_tests));
+	tcase_add_loop_test(tc, test_curve_constraints_fallback, 0, countof(curve_constraints_tests));
 	suite_add_tcase(s, tc);

 	tc = tcase_create("complies");
diff --git a/src/swanctl/swanctl.opt b/src/swanctl/swanctl.opt
index e092af1596..6245c5c672 100644
--- a/src/swanctl/swanctl.opt
+++ b/src/swanctl/swanctl.opt
@@ -459,12 +459,16 @@ connections.<conn>.local<suffix>.auth = pubkey
 	order and depending on the hash algorithms supported by the peer. If no
 	specific schemes are configured this way, the default is to prefer an
 	algorithm that matches or exceeds the strength of the signature key.
-	If no constraints with _ike:_ prefix are configured, any signature scheme
-	constraint (without _ike:_ prefix) will also apply to IKEv2 authentication,
-	unless this is disabled in **strongswan.conf**(5). To use RSASSA-PSS
-	signatures use _rsa/pss_ instead of _pubkey_ or _rsa_ as in e.g.
-	_ike:rsa/pss-sha256_. If _pubkey_ or _rsa_ constraints are configured
-	RSASSA-PSS signatures will only be used if enabled in
+	If no signature constraints with _ike:_ prefix are configured, any signature
+	scheme constraint (without _ike:_ prefix) will also apply to IKEv2
+	authentication, unless this is disabled in **strongswan.conf**(5). This
+	applies similarly for ECDSA curve constraints. If IKE-specific curve
+	constraints are found and no certificate is explicitly configured, the
+	curves will be used to select a suitable ECDSA private key.
+
+	To use RSASSA-PSS signatures use _rsa/pss_ instead of _pubkey_ or _rsa_ as
+	in e.g. _ike:rsa/pss-sha256_. If _pubkey_ or _rsa_ constraints are
+	configured RSASSA-PSS signatures will only be used if enabled in
 	**strongswan.conf**(5).

 connections.<conn>.local<suffix>.id =
@@ -708,7 +712,12 @@ connections.<conn>.remote<suffix>.auth = pubkey
 	constraints are configured (refer to the description of the **local**
 	section's **auth** keyword for details), such signature schemes are also
 	applied as constraints against IKEv2 signature authentication schemes used
-	by the remote side.
+	by the remote side. This similarly applies to ECDSA curve constraints,
+	which may be specified with the _ike:_ prefix to restrict the curves
+	allowed for IKEv2 ECDSA authentication. And as with signature schemes, if
+	no such IKE-specific curve constraints are configured and not disabled
+	globally, regular curve constraints are applied to the IKEv2 authentication
+	as well.

 	To require RSASSA-PSS signatures use _rsa/pss_ instead of _pubkey_ or _rsa_
 	as in e.g. _rsa/pss-sha256_. If _pubkey_ or _rsa_ constraints are configured