[PATCH 10/14] wpa_supplicant: store external PSKs when saving the configuration

Angelo Compagnucci angelo.compagnucci at gmail.com
Mon Nov 23 06:24:44 PST 2026


When a new passphrase or PSK is set on a network with psk=ext:<name>,
the network keeps the name. But saving the configuration wrote only
the name. The new value was lost at the next start, and the backend
still had the old one.

Before SAVE_CONFIG or the D-Bus SaveConfig method writes the
configuration, store the new value in the external password backend as
<name> and drop the inline copy. The configuration file then still has
only psk=ext:<name>. If the value cannot be stored, saving fails, so
the file never points to an old value.

This is done only with the pkcs11 backend. It is checked at build time
(CONFIG_EXT_PASSWORD_PKCS11) and in ext_password_backend, so saving
with the other backends does not change.

Tested on an STM32MP133 with the OP-TEE PKCS#11 TA and on an STM32MP157
with tpm2-pkcs11 on an SLB9670 TPM 2.0. SET_NETWORK psk and SAVE_CONFIG
store the passphrase as a private object, and the file has only
psk=ext:<name>. The station connects to a WPA2-PSK AP with the PSK read
from the token, also after a second save replaced it. With SoftHSM on
x86_64, the same binary saves as before with the file backend. The
D-Bus path was also tested on both boards. A network was added with
psk "ext:<name>" and its passphrase was set through Properties.
SaveConfig stored it on the token, and the station then connected with
it.

Signed-off-by: Angelo Compagnucci <angelo.compagnucci at gmail.com>
---
 doc/ctrl_iface.doxygen                  |  5 ++
 wpa_supplicant/ctrl_iface.c             |  8 +++
 wpa_supplicant/dbus/dbus_new_handlers.c |  6 +++
 wpa_supplicant/wpa_supplicant.c         | 72 +++++++++++++++++++++++++
 wpa_supplicant/wpa_supplicant.conf      |  5 ++
 wpa_supplicant/wpa_supplicant_i.h       |  3 ++
 6 files changed, 99 insertions(+)

diff --git a/doc/ctrl_iface.doxygen b/doc/ctrl_iface.doxygen
index 7dccdc797..423e8c90f 100644
--- a/doc/ctrl_iface.doxygen
+++ b/doc/ctrl_iface.doxygen
@@ -379,6 +379,11 @@ Get network variables. Network id can be received from the
 
 Save the current configuration.
 
+With the pkcs11 external password backend (ext_password_backend=pkcs11:...),
+a new passphrase or PSK set on a network with psk=ext:<name> is first stored
+in the backend as <name>. Only psk=ext:<name> is written to the configuration
+file. Saving fails if the backend cannot store the value.
+
 
 \subsection ctrl_iface_P2P_FIND P2P_FIND
 
diff --git a/wpa_supplicant/ctrl_iface.c b/wpa_supplicant/ctrl_iface.c
index 244f32d4c..ae2466fef 100644
--- a/wpa_supplicant/ctrl_iface.c
+++ b/wpa_supplicant/ctrl_iface.c
@@ -4117,6 +4117,14 @@ static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
 		return -1;
 	}
 
+#ifdef CONFIG_EXT_PASSWORD_PKCS11
+	if (wpas_ext_password_store(wpa_s) < 0) {
+		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
+			   "store external PSKs");
+		return -1;
+	}
+#endif /* CONFIG_EXT_PASSWORD_PKCS11 */
+
 	ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
 	if (ret) {
 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
diff --git a/wpa_supplicant/dbus/dbus_new_handlers.c b/wpa_supplicant/dbus/dbus_new_handlers.c
index 106872aab..4ee8f9153 100644
--- a/wpa_supplicant/dbus/dbus_new_handlers.c
+++ b/wpa_supplicant/dbus/dbus_new_handlers.c
@@ -3167,6 +3167,12 @@ DBusMessage * wpas_dbus_handler_save_config(DBusMessage *message,
 			"Not allowed to update configuration (update_config=0)");
 	}
 
+#ifdef CONFIG_EXT_PASSWORD_PKCS11
+	if (wpas_ext_password_store(wpa_s) < 0)
+		return wpas_dbus_error_unknown_error(
+			message, "Failed to store external PSKs");
+#endif /* CONFIG_EXT_PASSWORD_PKCS11 */
+
 	ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
 	if (ret)
 		return wpas_dbus_error_unknown_error(
diff --git a/wpa_supplicant/wpa_supplicant.c b/wpa_supplicant/wpa_supplicant.c
index 2d47bae28..5bb352e70 100644
--- a/wpa_supplicant/wpa_supplicant.c
+++ b/wpa_supplicant/wpa_supplicant.c
@@ -8077,6 +8077,78 @@ int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
 }
 
 
+#ifdef CONFIG_EXT_PASSWORD_PKCS11
+/**
+ * wpas_ext_password_store - Store new PSKs of networks using an external PSK
+ * @wpa_s: Pointer to wpa_supplicant data
+ * Returns: 0 on success, -1 if a PSK could not be stored
+ *
+ * When a new passphrase or PSK is set on a network with psk=ext:<name>, the
+ * network keeps the name. Before the configuration is written, store the new
+ * value in the external password backend as <name> and drop the inline
+ * value. The configuration file then has only psk=ext:<name>.
+ *
+ * This is done only with the pkcs11 backend. With the other backends, saving
+ * the configuration works as before.
+ */
+int wpas_ext_password_store(struct wpa_supplicant *wpa_s)
+{
+	const char *backend = wpa_s->conf->ext_password_backend;
+	struct wpa_ssid *ssid;
+
+	if (!backend || !str_starts(backend, "pkcs11:"))
+		return 0;
+
+	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
+		struct wpabuf *pw;
+		char *ext;
+		int ret;
+
+		if (!ssid->ext_psk || (!ssid->passphrase && !ssid->psk_set))
+			continue;
+
+		if (ssid->passphrase) {
+			pw = wpabuf_alloc_copy(ssid->passphrase,
+					       os_strlen(ssid->passphrase));
+		} else {
+			char hex[2 * PMK_LEN + 1];
+
+			wpa_snprintf_hex(hex, sizeof(hex), ssid->psk, PMK_LEN);
+			pw = wpabuf_alloc_copy(hex, 2 * PMK_LEN);
+			forced_memzero(hex, sizeof(hex));
+		}
+		if (!pw)
+			return -1;
+
+		ret = ext_password_set(wpa_s->ext_pw, ssid->ext_psk, pw);
+		wpabuf_clear_free(pw);
+		if (ret < 0) {
+			wpa_msg(wpa_s, MSG_INFO,
+				"EXT PW: Could not store the PSK of network id=%d as '%s'",
+				ssid->id, ssid->ext_psk);
+			return -1;
+		}
+
+		/* Use the stored PSK, as psk=ext:<name> does */
+		ext = os_malloc(4 + os_strlen(ssid->ext_psk) + 1);
+		if (!ext)
+			return -1;
+		os_snprintf(ext, 4 + os_strlen(ssid->ext_psk) + 1, "ext:%s",
+			    ssid->ext_psk);
+		ret = wpa_config_set(ssid, "psk", ext, 0);
+		os_free(ext);
+		if (ret < 0)
+			return -1;
+		wpa_printf(MSG_DEBUG,
+			   "EXT PW: Stored the PSK of network id=%d as '%s'",
+			   ssid->id, ssid->ext_psk);
+	}
+
+	return 0;
+}
+#endif /* CONFIG_EXT_PASSWORD_PKCS11 */
+
+
 #ifdef CONFIG_FST
 
 static const u8 * wpas_fst_get_bssid_cb(void *ctx)
diff --git a/wpa_supplicant/wpa_supplicant.conf b/wpa_supplicant/wpa_supplicant.conf
index 3445ab420..e8a975226 100644
--- a/wpa_supplicant/wpa_supplicant.conf
+++ b/wpa_supplicant/wpa_supplicant.conf
@@ -400,6 +400,11 @@ fast_reauth=1
 #     line. Use it instead of pin-value.
 # Without a PIN, only public objects can be read and new objects are
 # public. Secrets should be kept in private objects, so give a PIN.
+# When the configuration is saved (SAVE_CONFIG or the D-Bus SaveConfig method),
+# a new passphrase or PSK set on a network with psk=ext:<name> is stored in the
+# backend as <name>. Only psk=ext:<name> is written to the file. This works
+# only with the pkcs11 backend. With the other backends, only psk=ext:<name> is
+# written and the new value is lost.
 # Other tools can also create the objects, for example:
 #   pkcs11-tool --login --write-object psk.txt --type data --label wifi_psk \
 #       --private
diff --git a/wpa_supplicant/wpa_supplicant_i.h b/wpa_supplicant/wpa_supplicant_i.h
index a1d8029b6..4f7233768 100644
--- a/wpa_supplicant/wpa_supplicant_i.h
+++ b/wpa_supplicant/wpa_supplicant_i.h
@@ -1866,6 +1866,9 @@ int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
 				    struct wpa_ssid *ssid);
 
 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s);
+#ifdef CONFIG_EXT_PASSWORD_PKCS11
+int wpas_ext_password_store(struct wpa_supplicant *wpa_s);
+#endif /* CONFIG_EXT_PASSWORD_PKCS11 */
 
 const char * wpa_supplicant_state_txt(enum wpa_states state);
 int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s);
-- 
2.53.0




More information about the Hostap mailing list