Commit 1f2385c729 for strongswan.org

commit 1f2385c729d8eb7486ee1f0ae053ce7bedefd32e
Author: Tobias Brunner <tobias@strongswan.org>
Date:   Wed Nov 25 16:53:37 2026 +0100

    ike-auth: Make sure PPK settings of switched peer configs match

    If configs get switched, the previous code could allow a mismatch between
    configured and received PPK_IDs and the `ppk_required` setting.  We now
    store the received ID and whether we found a key and check this
    information against the config like other constraints when selecting
    configs.

    Fixes: a9e60c96dc88 ("ike-auth: Add basic PPK support")

diff --git a/src/libcharon/sa/ikev2/tasks/ike_auth.c b/src/libcharon/sa/ikev2/tasks/ike_auth.c
index e998b902b7..4d09e31d7b 100644
--- a/src/libcharon/sa/ikev2/tasks/ike_auth.c
+++ b/src/libcharon/sa/ikev2/tasks/ike_auth.c
@@ -71,6 +71,23 @@ struct private_ike_auth_t {
 	 */
 	chunk_t ppk;

+	/**
+	 * Whether the peer's PPK_ID was processed and a PPK searched as responder
+	 */
+	bool ppk_checked;
+
+	/**
+	 * Whether a key was found for the peer's PPK_ID as responder (the actual
+	 * PPK is cleared once we applied it)
+	 */
+	bool ppk_found;
+
+	/**
+	 * PPK_ID received from the peer as responder (the ID above is cleared once
+	 * we applied the PPK)
+	 */
+	identification_t *ppk_id_peer;
+
 	/**
 	 * IKE_SA_INIT message sent by us
 	 */
@@ -441,6 +458,30 @@ static bool load_cfg_candidates(private_ike_auth_t *this)
 	return FALSE;
 }

+/**
+ * Check whether the given peer config matches the observed PPK data
+ */
+static bool ppk_cfg_matches(private_ike_auth_t *this, peer_cfg_t *cfg)
+{
+	identification_t *ppk_id_cfg = cfg->get_ppk_id(cfg);
+	bool required = cfg->has_option(cfg, OPT_PPK_REQUIRED);
+
+	if (!this->ike_sa->supports_extension(this->ike_sa, EXT_PPK))
+	{
+		return !required;
+	}
+	if (!this->ppk_id_peer)
+	{
+		return !required;
+	}
+	if (ppk_id_cfg &&
+		!this->ppk_id_peer->matches(this->ppk_id_peer, ppk_id_cfg))
+	{
+		return FALSE;
+	}
+	return !required || this->ppk_found;
+}
+
 /**
  * update the current peer candidate if necessary, using candidates
  */
@@ -485,6 +526,10 @@ static bool update_cfg_candidates(private_ike_auth_t *this, bool strict)
 			}
 			e1->destroy(e1);
 			e2->destroy(e2);
+			if (this->ppk_checked && !ppk_cfg_matches(this, this->peer_cfg))
+			{
+				comply_error = "PPK policy mismatch";
+			}
 			if (!comply_error)
 			{
 				break;
@@ -679,14 +724,7 @@ static bool get_ppk(private_ike_auth_t *this, identification_t *ppk_id)
 	key = lib->credmgr->get_shared(lib->credmgr, SHARED_PPK, ppk_id, NULL);
 	if (!key)
 	{
-		if (this->peer_cfg->has_option(this->peer_cfg, OPT_PPK_REQUIRED))
-		{
-			DBG1(DBG_CFG, "PPK required but no PPK found for '%Y'", ppk_id);
-			return FALSE;
-		}
-		DBG1(DBG_CFG, "no PPK for '%Y' found, ignored because PPK is not "
-			 "required", ppk_id);
-		return TRUE;
+		return FALSE;
 	}
 	this->ppk = chunk_clone(key->get_key(key));
 	this->ppk_id = ppk_id->clone(ppk_id);
@@ -722,51 +760,50 @@ static bool get_ppk_i(private_ike_auth_t *this)
 		}
 		return TRUE;
 	}
-	return get_ppk(this, ppk_id);
+	if (!get_ppk(this, ppk_id))
+	{
+		if (this->peer_cfg->has_option(this->peer_cfg, OPT_PPK_REQUIRED))
+		{
+			DBG1(DBG_CFG, "PPK required but no PPK found for '%Y'", ppk_id);
+			return FALSE;
+		}
+		DBG1(DBG_CFG, "no PPK found for '%Y', ignored because PPK is not "
+			 "required", ppk_id);
+	}
+	return TRUE;
 }

 /**
- * Check if we have a PPK available and if not whether we require one as
- * responder
+ * Lookup and store PPK_ID and PPK as responder, enforcement happens in
+ * ppk_cfg_matches().
  */
-static bool get_ppk_r(private_ike_auth_t *this, message_t *msg)
+static void get_ppk_r(private_ike_auth_t *this, message_t *msg)
 {
 	notify_payload_t *notify;
-	identification_t *ppk_id, *ppk_id_cfg;
-	bool result;
+	identification_t *ppk_id;

+	if (this->ppk_checked)
+	{
+		return;
+	}
+	/* set even if the peer doesn't support PPKs, so configs that require one
+	 * are rejected later */
+	this->ppk_checked = TRUE;
 	if (!this->ike_sa->supports_extension(this->ike_sa, EXT_PPK))
 	{
-		if (this->peer_cfg->has_option(this->peer_cfg, OPT_PPK_REQUIRED))
-		{
-			DBG1(DBG_CFG, "PPK required but peer does not support PPK");
-			return FALSE;
-		}
-		return TRUE;
+		return;
 	}
-
 	notify = msg->get_notify(msg, PPK_IDENTITY);
 	if (!notify || !parse_ppk_identity(notify, &ppk_id))
 	{
-		if (this->peer_cfg->has_option(this->peer_cfg, OPT_PPK_REQUIRED))
-		{
-			DBG1(DBG_CFG, "PPK required but no PPK_IDENTITY received");
-			return FALSE;
-		}
-		return TRUE;
+		return;
 	}
-
-	ppk_id_cfg = this->peer_cfg->get_ppk_id(this->peer_cfg);
-	if (ppk_id_cfg && !ppk_id->matches(ppk_id, ppk_id_cfg))
+	this->ppk_id_peer = ppk_id;
+	this->ppk_found = get_ppk(this, ppk_id);
+	if (!this->ppk_found)
 	{
-		DBG1(DBG_CFG, "received PPK_ID '%Y', but require '%Y'", ppk_id,
-			 ppk_id_cfg);
-		ppk_id->destroy(ppk_id);
-		return FALSE;
+		DBG1(DBG_CFG, "no PPK found for '%Y'", ppk_id);
 	}
-	result = get_ppk(this, ppk_id);
-	ppk_id->destroy(ppk_id);
-	return result;
 }

 /**
@@ -1095,12 +1132,8 @@ METHOD(task_t, process_r, status_t,
 	if (message->get_payload(message, PLV2_AUTH) &&
 		is_first_round(this, FALSE))
 	{
-		if (!get_ppk_r(this, message))
-		{
-			this->authentication_failed = TRUE;
-			return NEED_MORE;
-		}
-		else if (this->ppk.ptr && this->other_auth->use_ppk)
+		get_ppk_r(this, message);
+		if (this->ppk.ptr && this->other_auth->use_ppk)
 		{
 			this->other_auth->use_ppk(this->other_auth, this->ppk, FALSE);
 		}
@@ -1803,6 +1836,7 @@ METHOD(task_t, destroy, void,
 	DESTROY_IF(this->other_auth);
 	DESTROY_IF(this->peer_cfg);
 	DESTROY_IF(this->redirect_to);
+	DESTROY_IF(this->ppk_id_peer);
 	this->candidates->destroy_offset(this->candidates, offsetof(peer_cfg_t, destroy));
 	free(this);
 }
diff --git a/testing/tests/ikev2/rw-cert-ppk/evaltest.dat b/testing/tests/ikev2/rw-cert-ppk/evaltest.dat
index 4138124a78..99c6a9ef0f 100755
--- a/testing/tests/ikev2/rw-cert-ppk/evaltest.dat
+++ b/testing/tests/ikev2/rw-cert-ppk/evaltest.dat
@@ -5,7 +5,7 @@ moon:: swanctl --list-sas --ike-id 2 --raw 2> /dev/null::rw.*version=2 state=EST
 carol::cat /var/log/daemon.log::using PPK for PPK_ID 'ppk-carol@strongswan.org'::YES
 dave:: cat /var/log/daemon.log::peer didn't use PPK for PPK_ID 'ppk-dave@strongswan.org'::YES
 moon:: cat /var/log/daemon.log::using PPK for PPK_ID 'ppk-carol@strongswan.org'::YES
-moon:: cat /var/log/daemon.log::no PPK for 'ppk-dave@strongswan.org' found, ignored because PPK is not required::YES
+moon:: cat /var/log/daemon.log::no PPK found for 'ppk-dave@strongswan.org'::YES
 moon:: cat /var/log/daemon.log::no PPK available, using NO_PPK_AUTH notify::YES
 alice::ping -c 1 192.168.0.100::64 bytes from 192.168.0.100: icmp_.eq=1::YES
 alice::ping -c 1 192.168.0.200::64 bytes from 192.168.0.200: icmp_.eq=1::YES
diff --git a/testing/tests/ikev2/rw-eap-md5-id-rsa-ppk/evaltest.dat b/testing/tests/ikev2/rw-eap-md5-id-rsa-ppk/evaltest.dat
index b02c212579..5d62168e43 100644
--- a/testing/tests/ikev2/rw-eap-md5-id-rsa-ppk/evaltest.dat
+++ b/testing/tests/ikev2/rw-eap-md5-id-rsa-ppk/evaltest.dat
@@ -13,7 +13,7 @@ moon:: cat /var/log/daemon.log::received EAP identity.*dave
 moon:: cat /var/log/daemon.log::EAP method EAP_MD5 succeeded, no MSK established
 moon:: cat /var/log/daemon.log::authentication of '192.168.0.200' with EAP successful::YES
 moon:: cat /var/log/daemon.log::using PPK for PPK_ID 'ppk-carol@strongswan.org'::YES
-moon:: cat /var/log/daemon.log::no PPK for 'ppk-dave@strongswan.org' found, ignored because PPK is not required::YES
+moon:: cat /var/log/daemon.log::no PPK found for 'ppk-dave@strongswan.org'::YES
 moon:: cat /var/log/daemon.log::no PPK available, using NO_PPK_AUTH notify::YES
 carol::ping -c 1 PH_IP_ALICE::64 bytes from PH_IP_ALICE: icmp_.eq=1::YES
 carol::swanctl --list-sas --raw 2> /dev/null::home.*version=2 state=ESTABLISHED local-host=192.168.0.100 local-port=4500 local-id=192.168.0.100 remote-host=192.168.0.1 remote-port=4500 remote-id=moon.strongswan.org initiator=yes.*encr-alg=AES_CBC encr-keysize=128 integ-alg=HMAC_SHA2_256_128 prf-alg=PRF_HMAC_SHA2_256 dh-group=CURVE_25519 ppk=yes.*child-sas.*home.*state=INSTALLED mode=TUNNEL.*ESP.*encr-alg=AES_GCM_16 encr-keysize=128.*local-ts=\[192.168.0.100/32] remote-ts=\[10.1.0.0/16]::YES
diff --git a/testing/tests/ikev2/rw-psk-ppk/evaltest.dat b/testing/tests/ikev2/rw-psk-ppk/evaltest.dat
index b8b3fcb0ef..365ccc3c03 100755
--- a/testing/tests/ikev2/rw-psk-ppk/evaltest.dat
+++ b/testing/tests/ikev2/rw-psk-ppk/evaltest.dat
@@ -5,7 +5,7 @@ moon:: swanctl --list-sas --ike-id 2 --raw 2> /dev/null::rw.*version=2 state=EST
 carol::cat /var/log/daemon.log::using PPK for PPK_ID 'ppk-carol@strongswan.org'::YES
 dave:: cat /var/log/daemon.log::peer didn't use PPK for PPK_ID 'ppk-dave@strongswan.org'::YES
 moon:: cat /var/log/daemon.log::using PPK for PPK_ID 'ppk-carol@strongswan.org'::YES
-moon:: cat /var/log/daemon.log::no PPK for 'ppk-dave@strongswan.org' found, ignored because PPK is not required::YES
+moon:: cat /var/log/daemon.log::no PPK found for 'ppk-dave@strongswan.org'::YES
 moon:: cat /var/log/daemon.log::no PPK available, using NO_PPK_AUTH notify::YES
 alice::ping -c 1 -W 1 192.168.0.100::64 bytes from 192.168.0.100: icmp_.eq=1::YES
 alice::ping -c 1 -W 1 192.168.0.200::64 bytes from 192.168.0.200: icmp_.eq=1::YES