[PATCH 06/14] ext_password_pkcs11: read the PIN from a file

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


Accept the pin-source attribute of the PKCS#11 URI (RFC 7512). It names
a file with the user PIN on its first line, so the PIN does not need to
be in the configuration. A URI with both pin-value and pin-source is
refused, as RFC 7512 section 2.3 recommends.

Tested with SoftHSM 2.6.1 and with the PIN files of the OP-TEE token on
an STM32MP133 and of the tpm2-pkcs11 token on an STM32MP157. On SoftHSM
a URI with both PIN attributes was also tested, and refused.

Signed-off-by: Angelo Compagnucci <angelo.compagnucci at gmail.com>
---
 src/utils/ext_password_pkcs11.c    | 53 +++++++++++++++++++++++++++---
 wpa_supplicant/wpa_supplicant.conf |  6 ++--
 2 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/src/utils/ext_password_pkcs11.c b/src/utils/ext_password_pkcs11.c
index 79b117b6b..5f1f61f44 100644
--- a/src/utils/ext_password_pkcs11.c
+++ b/src/utils/ext_password_pkcs11.c
@@ -12,14 +12,15 @@
  * The backend parameter is a PKCS#11 URI (RFC 7512) that selects the token. The
  * "pkcs11:" prefix is optional:
  *
- * ext_password_backend=pkcs11:token=wifi?pin-value=1234
+ * ext_password_backend=pkcs11:token=wifi?pin-source=file:/etc/pin
  *
  * p11-kit parses the URI and loads the PKCS#11 modules. If the URI has
  * module-path, only that module is loaded. With CONFIG_TLS_ENGINE_TRUSTED_PATH
  * it must pass os_lib_path_trusted(). Otherwise the modules registered in the
  * p11-kit configuration are used, or only the one named by module-name. The
  * first token that matches the module, slot and token attributes is used. The
- * user PIN comes from pin-value.
+ * user PIN comes from pin-value or from the first line of the pin-source file,
+ * not from both.
  */
 
 #include "includes.h"
@@ -150,6 +151,14 @@ static void * ext_password_pkcs11_init(const char *params)
 		goto fail;
 	}
 
+	/* Refuse a URI with both PIN attributes (RFC 7512, 2.3) */
+	if (p11_kit_uri_get_pin_value(data->uri) &&
+	    p11_kit_uri_get_pin_source(data->uri)) {
+		wpa_printf(MSG_ERROR,
+			   "EXT PW PKCS11: pin-value and pin-source cannot be used together");
+		goto fail;
+	}
+
 	path = p11_kit_uri_get_module_path(data->uri);
 	if (path) {
 		data->modules = load_module(path);
@@ -247,11 +256,41 @@ static int find_token(struct ext_password_pkcs11_data *data,
 }
 
 
+/* Get the PIN from pin-value or the first line of the pin-source file */
+static char * get_pin(struct ext_password_pkcs11_data *data)
+{
+	const char *path = p11_kit_uri_get_pin_source(data->uri);
+	const char *value = p11_kit_uri_get_pin_value(data->uri);
+	char *buf, *pin;
+	size_t len;
+
+	if (value)
+		return os_strdup(value);
+	if (!path)
+		return NULL;
+	if (str_starts(path, "file:"))
+		path += 5;
+
+	buf = os_readfile(path, &len);
+	if (!buf) {
+		wpa_printf(MSG_ERROR, "EXT PW PKCS11: Cannot read PIN from %s",
+			   path);
+		return NULL;
+	}
+	pin = dup_binstr(buf, len);
+	bin_clear_free(buf, len);
+	if (!pin)
+		return NULL;
+	pin[strcspn(pin, "\r\n")] = '\0';
+	return pin;
+}
+
+
 static int open_session(struct ext_password_pkcs11_data *data,
 			CK_FUNCTION_LIST **module, CK_SESSION_HANDLE *session)
 {
 	CK_SLOT_ID slot;
-	const char *pin;
+	char *pin;
 	CK_RV rv;
 
 	if (find_token(data, module, &slot) < 0)
@@ -265,14 +304,18 @@ static int open_session(struct ext_password_pkcs11_data *data,
 		return -1;
 	}
 
-	pin = p11_kit_uri_get_pin_value(data->uri);
-	if (!pin)
+	if (!p11_kit_uri_get_pin_value(data->uri) &&
+	    !p11_kit_uri_get_pin_source(data->uri))
 		return 0;
 
+	pin = get_pin(data);
+	if (!pin)
+		goto fail;
 	/* All sessions of the module share the login state, so do not log
 	 * out here. */
 	rv = (*module)->C_Login(*session, CKU_USER, (CK_UTF8CHAR_PTR) pin,
 				os_strlen(pin));
+	str_clear_free(pin);
 	if (rv != CKR_OK && rv != CKR_USER_ALREADY_LOGGED_IN) {
 		wpa_printf(MSG_ERROR, "EXT PW PKCS11: C_Login failed: %s",
 			   p11_kit_strerror(rv));
diff --git a/wpa_supplicant/wpa_supplicant.conf b/wpa_supplicant/wpa_supplicant.conf
index 390bfbc6c..5a73a83c5 100644
--- a/wpa_supplicant/wpa_supplicant.conf
+++ b/wpa_supplicant/wpa_supplicant.conf
@@ -395,13 +395,15 @@ fast_reauth=1
 #     by root under /usr/lib/.
 #   module-name: the registered module to look for the token in
 #   pin-value: the user PIN
+#   pin-source: file:<path> of a file with the user PIN on its first
+#     line. Use it instead of pin-value.
 # Without a PIN, only public objects can be read. Secrets should be kept
 # in private objects, so give a PIN.
 # The objects are created by other tools, for example:
 #   pkcs11-tool --login --write-object psk.txt --type data --label wifi_psk \
 #       --private
-#ext_password_backend=pkcs11:token=wifi?pin-value=1234
-#ext_password_backend=pkcs11:token=wifi?module-path=/usr/lib/softhsm/libsofthsm2.so&pin-value=1234
+#ext_password_backend=pkcs11:token=wifi?pin-source=file:/etc/wpa_supplicant/pin
+#ext_password_backend=pkcs11:token=wifi?module-path=/usr/lib/softhsm/libsofthsm2.so&pin-source=file:/etc/wpa_supplicant/pin
 
 
 # Disable P2P functionality
-- 
2.53.0




More information about the Hostap mailing list