[PATCH 07/14] ext_password_pkcs11: default to the PKCS#11 module of EAP-TLS

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


The token with the passwords is often the same token that has the
EAP-TLS keys. Its PKCS#11 module is then already set in
pkcs11_module_path. If the URI of the pkcs11 backend has no
module-path, use pkcs11_module_path instead of the modules registered
with p11-kit. One setting then names the module for both.

Tested with SoftHSM 2.6.1. With pkcs11_module_path set, no module-path
in the URI and no module registered with p11-kit, the module is loaded
and SAVE_CONFIG stores the PSK on the token. The same test passed with
tpm2-pkcs11 on an STM32MP157, built with CONFIG_TLS_ENGINE_TRUSTED_PATH.

Signed-off-by: Angelo Compagnucci <angelo.compagnucci at gmail.com>
---
 wpa_supplicant/wpa_supplicant.c    | 48 +++++++++++++++++++++++++++++-
 wpa_supplicant/wpa_supplicant.conf |  5 ++--
 2 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/wpa_supplicant/wpa_supplicant.c b/wpa_supplicant/wpa_supplicant.c
index 329fb4b0c..2d47bae28 100644
--- a/wpa_supplicant/wpa_supplicant.c
+++ b/wpa_supplicant/wpa_supplicant.c
@@ -7999,9 +7999,42 @@ static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
 }
 
 
+#ifdef CONFIG_EXT_PASSWORD_PKCS11
+/*
+ * Return the PKCS#11 URI of the pkcs11 backend. If the URI has no
+ * module-path, add pkcs11_module_path, the PKCS#11 module of the EAP-TLS keys.
+ */
+static char * wpas_ext_pw_pkcs11_uri(struct wpa_supplicant *wpa_s,
+				     const char *uri)
+{
+	const char *path = wpa_s->conf->pkcs11_module_path;
+	size_t len;
+	char *res;
+
+	if (!uri)
+		uri = "";
+	if (!path || os_strstr(uri, "module-path="))
+		return os_strdup(uri);
+	if (os_strchr(path, '%') || os_strchr(path, '&') ||
+	    os_strchr(path, '#') || os_strchr(path, '?')) {
+		wpa_printf(MSG_INFO,
+			   "EXT PW: pkcs11_module_path cannot be used in a PKCS#11 URI");
+		return os_strdup(uri);
+	}
+
+	len = os_strlen(uri) + 13 + os_strlen(path) + 1;
+	res = os_malloc(len);
+	if (res)
+		os_snprintf(res, len, "%s%cmodule-path=%s", uri,
+			    os_strchr(uri, '?') ? '&' : '?', path);
+	return res;
+}
+#endif /* CONFIG_EXT_PASSWORD_PKCS11 */
+
+
 int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
 {
-	char *val, *pos;
+	char *val, *pos, *uri = NULL;
 
 	ext_password_deinit(wpa_s->ext_pw);
 	wpa_s->ext_pw = NULL;
@@ -8019,8 +8052,21 @@ int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
 
 	wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
 
+#ifdef CONFIG_EXT_PASSWORD_PKCS11
+	if (os_strcmp(val, "pkcs11") == 0) {
+		uri = wpas_ext_pw_pkcs11_uri(wpa_s, pos);
+		if (!uri) {
+			os_free(val);
+			return -1;
+		}
+		pos = uri;
+	}
+#endif /* CONFIG_EXT_PASSWORD_PKCS11 */
+
 	wpa_s->ext_pw = ext_password_init(val, pos);
 	os_free(val);
+	/* The URI may hold a PIN */
+	str_clear_free(uri);
 	if (wpa_s->ext_pw == NULL) {
 		wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
 		return -1;
diff --git a/wpa_supplicant/wpa_supplicant.conf b/wpa_supplicant/wpa_supplicant.conf
index 5a73a83c5..b505bc615 100644
--- a/wpa_supplicant/wpa_supplicant.conf
+++ b/wpa_supplicant/wpa_supplicant.conf
@@ -389,8 +389,9 @@ fast_reauth=1
 #   slot: slot-description, slot-manufacturer, slot-id
 #   token: token, manufacturer, serial, model
 # An attribute that is not given matches any value. Query attributes:
-#   module-path: the PKCS#11 module to load instead of the modules
-#     registered with p11-kit
+#   module-path: the PKCS#11 module to load. The default is
+#     pkcs11_module_path. If neither is set, the modules registered
+#     with p11-kit are used.
 #     With CONFIG_TLS_ENGINE_TRUSTED_PATH, the module must be a file owned
 #     by root under /usr/lib/.
 #   module-name: the registered module to look for the token in
-- 
2.53.0




More information about the Hostap mailing list