[PATCH 09/14] ext_password_pkcs11: store passwords

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


Add the set operation. It stores a password as a data object on the
token, with the password name as label. The object is private when a
PIN is set, and public when not.

The backend creates a new object and then destroys the old objects
with the same label. It does not update CKA_VALUE in place, because
SoftHSM and OP-TEE refuse that, and tpm2-pkcs11 accepts it but loses
the value.

Tested with SoftHSM 2.6.1 on x86_64, the OP-TEE PKCS#11 TA with
libckteec on an STM32MP133 and tpm2-pkcs11 on an Infineon SLB9670 TPM
2.0 on an STM32MP157. After storing and replacing a password, there is
one private object. It cannot be seen without the PIN, and it reads
back the new value.

Signed-off-by: Angelo Compagnucci <angelo.compagnucci at gmail.com>
---
 src/utils/ext_password_pkcs11.c    | 96 ++++++++++++++++++++++++++++--
 wpa_supplicant/defconfig           |  2 +-
 wpa_supplicant/wpa_supplicant.conf |  8 +--
 3 files changed, 95 insertions(+), 11 deletions(-)

diff --git a/src/utils/ext_password_pkcs11.c b/src/utils/ext_password_pkcs11.c
index 5f1f61f44..ddb6e23f9 100644
--- a/src/utils/ext_password_pkcs11.c
+++ b/src/utils/ext_password_pkcs11.c
@@ -7,7 +7,8 @@
  *
  * Passwords are data objects (CKO_DATA) on a PKCS#11 token. The object label
  * (CKA_LABEL) is the password name and the object value (CKA_VALUE) is the
- * password.
+ * password. Objects stored by the backend are token objects. They are private
+ * when a PIN is set.
  *
  * The backend parameter is a PKCS#11 URI (RFC 7512) that selects the token. The
  * "pkcs11:" prefix is optional:
@@ -34,6 +35,7 @@
 #include "ext_password_i.h"
 
 
+#define PKCS11_MAX_OBJECTS 8
 #define PKCS11_MAX_PASSWORD_LEN 4096
 
 
@@ -286,18 +288,21 @@ static char * get_pin(struct ext_password_pkcs11_data *data)
 }
 
 
-static int open_session(struct ext_password_pkcs11_data *data,
-			CK_FUNCTION_LIST **module, CK_SESSION_HANDLE *session)
+static int open_session(struct ext_password_pkcs11_data *data, int rw,
+			CK_FUNCTION_LIST **module, CK_SESSION_HANDLE *session,
+			int *logged_in)
 {
 	CK_SLOT_ID slot;
 	char *pin;
 	CK_RV rv;
 
+	*logged_in = 0;
 	if (find_token(data, module, &slot) < 0)
 		return -1;
 
-	rv = (*module)->C_OpenSession(slot, CKF_SERIAL_SESSION, NULL, NULL,
-				      session);
+	rv = (*module)->C_OpenSession(slot, CKF_SERIAL_SESSION |
+				      (rw ? CKF_RW_SESSION : 0),
+				      NULL, NULL, session);
 	if (rv != CKR_OK) {
 		wpa_printf(MSG_ERROR, "EXT PW PKCS11: C_OpenSession failed: %s",
 			   p11_kit_strerror(rv));
@@ -321,6 +326,7 @@ static int open_session(struct ext_password_pkcs11_data *data,
 			   p11_kit_strerror(rv));
 		goto fail;
 	}
+	*logged_in = 1;
 	return 0;
 
 fail:
@@ -374,11 +380,12 @@ static struct wpabuf * ext_password_pkcs11_get(void *ctx, const char *name)
 	CK_OBJECT_HANDLE object;
 	CK_ATTRIBUTE value = { CKA_VALUE, NULL, 0 };
 	CK_ULONG count = 0;
+	int logged_in;
 	CK_RV rv;
 
 	wpa_printf(MSG_DEBUG, "EXT PW PKCS11: get(%s)", name);
 
-	if (open_session(data, &module, &session) < 0)
+	if (open_session(data, 0, &module, &session, &logged_in) < 0)
 		return NULL;
 
 	if (find_objects(module, session, name, &object, 1, &count) < 0)
@@ -418,9 +425,86 @@ out:
 }
 
 
+/**
+ * ext_password_pkcs11_set - Store a password on the PKCS#11 token
+ * @ctx: The PKCS#11 password backend
+ * @name: CKA_LABEL of the data object holding the password
+ * @pw: Password to store
+ * Returns: 0 on success, -1 on failure
+ *
+ * Create a new object, then destroy the old objects with the same label.
+ * CKA_VALUE is not updated in place, because SoftHSM and OP-TEE refuse it,
+ * and tpm2-pkcs11 accepts it but loses the value.
+ */
+static int ext_password_pkcs11_set(void *ctx, const char *name,
+				   const struct wpabuf *pw)
+{
+	struct ext_password_pkcs11_data *data = ctx;
+	CK_OBJECT_HANDLE objects[PKCS11_MAX_OBJECTS], object;
+	CK_FUNCTION_LIST *module;
+	CK_SESSION_HANDLE session;
+	CK_ULONG count = 0, i;
+	CK_OBJECT_CLASS class = CKO_DATA;
+	CK_BBOOL ck_true = CK_TRUE, priv;
+	CK_ATTRIBUTE value = {
+		CKA_VALUE, (void *) wpabuf_head(pw), wpabuf_len(pw)
+	};
+	int logged_in, ret = -1;
+	CK_RV rv;
+
+	wpa_printf(MSG_DEBUG, "EXT PW PKCS11: set(%s)", name);
+
+	if (open_session(data, 1, &module, &session, &logged_in) < 0)
+		return -1;
+
+	if (find_objects(module, session, name, objects, ARRAY_SIZE(objects),
+			 &count) < 0)
+		goto out;
+
+	/* Without a login the object can only be public */
+	priv = logged_in ? CK_TRUE : CK_FALSE;
+	{
+		CK_ATTRIBUTE templ[] = {
+			{ CKA_CLASS, &class, sizeof(class) },
+			{ CKA_TOKEN, &ck_true, sizeof(ck_true) },
+			{ CKA_PRIVATE, &priv, sizeof(priv) },
+			{ CKA_LABEL, (void *) name, os_strlen(name) },
+			value,
+		};
+
+		rv = module->C_CreateObject(session, templ, ARRAY_SIZE(templ),
+					    &object);
+	}
+	if (rv != CKR_OK) {
+		wpa_printf(MSG_ERROR,
+			   "EXT PW PKCS11: Cannot store password '%s': %s",
+			   name, p11_kit_strerror(rv));
+		goto out;
+	}
+	if (!logged_in)
+		wpa_printf(MSG_INFO,
+			   "EXT PW PKCS11: No PIN configured, '%s' stored as a public object",
+			   name);
+
+	for (i = 0; i < count; i++) {
+		rv = module->C_DestroyObject(session, objects[i]);
+		if (rv != CKR_OK)
+			wpa_printf(MSG_ERROR,
+				   "EXT PW PKCS11: Cannot remove an old '%s': %s",
+				   name, p11_kit_strerror(rv));
+	}
+	ret = 0;
+
+out:
+	module->C_CloseSession(session);
+	return ret;
+}
+
+
 const struct ext_password_backend ext_password_pkcs11 = {
 	.name = "pkcs11",
 	.init = ext_password_pkcs11_init,
 	.deinit = ext_password_pkcs11_deinit,
 	.get = ext_password_pkcs11_get,
+	.set = ext_password_pkcs11_set,
 };
diff --git a/wpa_supplicant/defconfig b/wpa_supplicant/defconfig
index 0ff5a994d..2678b19aa 100644
--- a/wpa_supplicant/defconfig
+++ b/wpa_supplicant/defconfig
@@ -566,7 +566,7 @@ CONFIG_WIFI_DISPLAY=y
 #CONFIG_EXT_PASSWORD_TEST=y
 # File-based backend to read passwords from an external file.
 #CONFIG_EXT_PASSWORD_FILE=y
-# PKCS#11 external password backend. It reads passwords from data objects
+# PKCS#11 external password backend. It keeps passwords in data objects
 # on a PKCS#11 token, such as a TPM, a TEE or a smart card. It needs
 # libp11-kit (pkg-config p11-kit-1). See ext_password_backend in
 # wpa_supplicant.conf.
diff --git a/wpa_supplicant/wpa_supplicant.conf b/wpa_supplicant/wpa_supplicant.conf
index b505bc615..3445ab420 100644
--- a/wpa_supplicant/wpa_supplicant.conf
+++ b/wpa_supplicant/wpa_supplicant.conf
@@ -377,7 +377,7 @@ fast_reauth=1
 # format of wpa_supplicant.conf and accepts simple `key=passphrase` formatted
 # passwords.
 #ext_password_backend=file:/path/to/passwords.conf
-# PKCS#11 backend (CONFIG_EXT_PASSWORD_PKCS11=y). It reads passwords from data
+# PKCS#11 backend (CONFIG_EXT_PASSWORD_PKCS11=y). It keeps passwords in data
 # objects (CKO_DATA) on a PKCS#11 token, such as a TPM, a TEE or a smart card.
 # The object label (CKA_LABEL) is the password name and the object value
 # (CKA_VALUE) is the password. For psk=ext:<name>, the value is a passphrase of
@@ -398,9 +398,9 @@ fast_reauth=1
 #   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:
+# 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.
+# Other tools can also create the objects, for example:
 #   pkcs11-tool --login --write-object psk.txt --type data --label wifi_psk \
 #       --private
 #ext_password_backend=pkcs11:token=wifi?pin-source=file:/etc/wpa_supplicant/pin
-- 
2.53.0




More information about the Hostap mailing list