[PATCH 05/14] ext_password: add a PKCS#11 token backend
Angelo Compagnucci
angelo.compagnucci at gmail.com
Mon Nov 23 06:24:39 PST 2026
Add a backend that reads passwords from data objects (CKO_DATA) on a
PKCS#11 token. The object label (CKA_LABEL) is the password name. With
it, a PSK or an EAP password can live in a TPM or a TEE based token
instead of in the configuration or in a plain file.
The backend parameter is a PKCS#11 URI (RFC 7512) that selects the
token. p11-kit parses it. The user PIN is in pin-value:
ext_password_backend=pkcs11:token=wifi?pin-value=1234
Like other p11-kit users, the backend looks in the PKCS#11 modules
registered in the p11-kit configuration. It uses the first token that
matches the module, slot and token attributes of the URI. module-name
limits the search to one registered module. module-path loads that
module instead. It is loaded as a managed module, so other users of
the same module in the process, such as the OpenSSL PKCS#11 engine,
are not affected. With CONFIG_TLS_ENGINE_TRUSTED_PATH, module-path must
pass the same os_lib_path_trusted() check as the PKCS#11 engine and
module paths. The build needs libp11-kit (pkg-config p11-kit-1).
Tested with these tokens:
- SoftHSM 2.6.1 on x86_64
- the OP-TEE PKCS#11 TA with libckteec on an STM32MP133
- tpm2-pkcs11 on an Infineon SLB9670 TPM 2.0 on an STM32MP157
On all of them, the tests covered reading a password with module-path,
a missing password, and private objects that cannot be seen without a
PIN. On SoftHSM, they also covered the registered modules, module-name,
selecting the token by label, manufacturer, library manufacturer and
slot-id, a URI that matches no token, an unsupported attribute, a wrong
PIN, and a module outside /usr/lib/ refused with
CONFIG_TLS_ENGINE_TRUSTED_PATH.
Signed-off-by: Angelo Compagnucci <angelo.compagnucci at gmail.com>
---
src/utils/ext_password.c | 3 +
src/utils/ext_password_i.h | 4 +
src/utils/ext_password_pkcs11.c | 383 +++++++++++++++++++++++++++++
wpa_supplicant/Makefile | 13 +
wpa_supplicant/defconfig | 8 +-
wpa_supplicant/wpa_supplicant.conf | 25 ++
6 files changed, 435 insertions(+), 1 deletion(-)
create mode 100644 src/utils/ext_password_pkcs11.c
diff --git a/src/utils/ext_password.c b/src/utils/ext_password.c
index cbf92de8c..3f59167d7 100644
--- a/src/utils/ext_password.c
+++ b/src/utils/ext_password.c
@@ -23,6 +23,9 @@ static const struct ext_password_backend *backends[] = {
#ifdef CONFIG_EXT_PASSWORD_FILE
&ext_password_file,
#endif /* CONFIG_EXT_PASSWORD_FILE */
+#ifdef CONFIG_EXT_PASSWORD_PKCS11
+ &ext_password_pkcs11,
+#endif /* CONFIG_EXT_PASSWORD_PKCS11 */
NULL
};
diff --git a/src/utils/ext_password_i.h b/src/utils/ext_password_i.h
index 872ccd195..da0ee738a 100644
--- a/src/utils/ext_password_i.h
+++ b/src/utils/ext_password_i.h
@@ -30,4 +30,8 @@ extern const struct ext_password_backend ext_password_test;
extern const struct ext_password_backend ext_password_file;
#endif /* CONFIG_EXT_PASSWORD_FILE */
+#ifdef CONFIG_EXT_PASSWORD_PKCS11
+extern const struct ext_password_backend ext_password_pkcs11;
+#endif /* CONFIG_EXT_PASSWORD_PKCS11 */
+
#endif /* EXT_PASSWORD_I_H */
diff --git a/src/utils/ext_password_pkcs11.c b/src/utils/ext_password_pkcs11.c
new file mode 100644
index 000000000..79b117b6b
--- /dev/null
+++ b/src/utils/ext_password_pkcs11.c
@@ -0,0 +1,383 @@
+/*
+ * External password backend for PKCS#11 tokens
+ * Copyright (c) 2026, Angelo Compagnucci <angelo.compagnucci at gmail.com>
+ *
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
+ *
+ * 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.
+ *
+ * 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
+ *
+ * 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.
+ */
+
+#include "includes.h"
+#ifdef CONFIG_TLS_ENGINE_TRUSTED_PATH
+#include <limits.h>
+#endif /* CONFIG_TLS_ENGINE_TRUSTED_PATH */
+#include <p11-kit/p11-kit.h>
+#include <p11-kit/uri.h>
+
+#include "utils/common.h"
+#include "ext_password_i.h"
+
+
+#define PKCS11_MAX_PASSWORD_LEN 4096
+
+
+struct ext_password_pkcs11_data {
+ CK_FUNCTION_LIST **modules; /* NULL terminated */
+ int registered; /* modules of the p11-kit configuration */
+ P11KitUri *uri;
+};
+
+
+static void ext_password_pkcs11_deinit(void *ctx)
+{
+ struct ext_password_pkcs11_data *data = ctx;
+
+ if (data->modules && data->registered) {
+ p11_kit_modules_finalize_and_release(data->modules);
+ } else if (data->modules) {
+ p11_kit_module_finalize(data->modules[0]);
+ p11_kit_module_release(data->modules[0]);
+ os_free(data->modules);
+ }
+ p11_kit_uri_free(data->uri);
+ os_free(data);
+}
+
+
+/* Load the module named by module-path, as a NULL terminated list */
+static CK_FUNCTION_LIST ** load_module(const char *path)
+{
+ CK_FUNCTION_LIST **modules;
+ CK_RV rv;
+#ifdef CONFIG_TLS_ENGINE_TRUSTED_PATH
+ char real_path[PATH_MAX];
+
+ if (os_lib_path_trusted(path, real_path) < 0)
+ return NULL;
+ path = real_path;
+#endif /* CONFIG_TLS_ENGINE_TRUSTED_PATH */
+
+ modules = os_calloc(2, sizeof(*modules));
+ if (!modules)
+ return NULL;
+
+ /* Load a managed module, so that other users of the same module in
+ * this process, such as the OpenSSL PKCS#11 engine, are not affected */
+ modules[0] = p11_kit_module_load(path, 0);
+ if (!modules[0]) {
+ wpa_printf(MSG_ERROR, "EXT PW PKCS11: Cannot load %s: %s",
+ path, p11_kit_message());
+ os_free(modules);
+ return NULL;
+ }
+ rv = p11_kit_module_initialize(modules[0]);
+ if (rv != CKR_OK) {
+ wpa_printf(MSG_ERROR,
+ "EXT PW PKCS11: Cannot initialize %s: %s",
+ path, p11_kit_strerror(rv));
+ p11_kit_module_release(modules[0]);
+ os_free(modules);
+ return NULL;
+ }
+
+ wpa_printf(MSG_DEBUG, "EXT PW PKCS11: Loaded %s", path);
+ return modules;
+}
+
+
+/**
+ * ext_password_pkcs11_init - Initialize the PKCS#11 password backend
+ * @params: PKCS#11 URI of the token
+ * Returns: Pointer to the initialized backend or %NULL on failure
+ */
+static void * ext_password_pkcs11_init(const char *params)
+{
+ struct ext_password_pkcs11_data *data;
+ const char *path;
+ char *uri = NULL;
+ size_t len = 0;
+ int res;
+
+ if (!params)
+ params = "";
+
+ data = os_zalloc(sizeof(*data));
+ if (!data)
+ return NULL;
+ data->uri = p11_kit_uri_new();
+ if (!data->uri)
+ goto fail;
+
+ /* "pkcs11:" was taken as the backend name, so add it back */
+ if (!str_starts(params, "pkcs11:")) {
+ len = 7 + os_strlen(params) + 1;
+ uri = os_malloc(len);
+ if (!uri)
+ goto fail;
+ os_snprintf(uri, len, "pkcs11:%s", params);
+ params = uri;
+ }
+
+ res = p11_kit_uri_parse(params,
+ P11_KIT_URI_FOR_MODULE_WITH_VERSION |
+ P11_KIT_URI_FOR_SLOT | P11_KIT_URI_FOR_TOKEN,
+ data->uri);
+ /* The URI may hold the PIN */
+ bin_clear_free(uri, len);
+ if (res != P11_KIT_URI_OK) {
+ wpa_printf(MSG_ERROR, "EXT PW PKCS11: Invalid PKCS#11 URI: %s",
+ p11_kit_uri_message(res));
+ goto fail;
+ }
+ if (p11_kit_uri_any_unrecognized(data->uri)) {
+ wpa_printf(MSG_ERROR,
+ "EXT PW PKCS11: Unsupported attribute in the PKCS#11 URI");
+ goto fail;
+ }
+
+ path = p11_kit_uri_get_module_path(data->uri);
+ if (path) {
+ data->modules = load_module(path);
+ } else {
+ data->modules = p11_kit_modules_load_and_initialize(0);
+ data->registered = 1;
+ if (data->modules && !data->modules[0])
+ wpa_printf(MSG_INFO,
+ "EXT PW PKCS11: No PKCS#11 module registered with p11-kit");
+ }
+ if (!data->modules) {
+ if (!path)
+ wpa_printf(MSG_ERROR,
+ "EXT PW PKCS11: Cannot load the PKCS#11 modules: %s",
+ p11_kit_message());
+ goto fail;
+ }
+
+ return data;
+
+fail:
+ ext_password_pkcs11_deinit(data);
+ return NULL;
+}
+
+
+/* Check if the module matches module-name and the library attributes */
+static int module_match(struct ext_password_pkcs11_data *data,
+ CK_FUNCTION_LIST *module)
+{
+ const char *wanted = p11_kit_uri_get_module_name(data->uri);
+ CK_INFO info;
+
+ if (wanted) {
+ char *name = p11_kit_module_get_name(module);
+ int match = name && os_strcmp(name, wanted) == 0;
+
+ free(name);
+ if (!match)
+ return 0;
+ }
+
+ return module->C_GetInfo(&info) == CKR_OK &&
+ p11_kit_uri_match_module_info(data->uri, &info);
+}
+
+
+/* Find the first module and slot with a token that matches the URI */
+static int find_token(struct ext_password_pkcs11_data *data,
+ CK_FUNCTION_LIST **module, CK_SLOT_ID *slot)
+{
+ CK_SLOT_ID slot_id = p11_kit_uri_get_slot_id(data->uri);
+ CK_FUNCTION_LIST **m;
+
+ for (m = data->modules; *m; m++) {
+ CK_SLOT_ID *slots;
+ CK_ULONG count = 0, i;
+
+ if (!module_match(data, *m) ||
+ (*m)->C_GetSlotList(CK_TRUE, NULL, &count) != CKR_OK ||
+ count == 0)
+ continue;
+
+ slots = os_calloc(count, sizeof(*slots));
+ if (!slots)
+ return -1;
+ if ((*m)->C_GetSlotList(CK_TRUE, slots, &count) != CKR_OK)
+ count = 0;
+
+ for (i = 0; i < count; i++) {
+ CK_SLOT_INFO slot_info;
+ CK_TOKEN_INFO token_info;
+
+ if (slot_id != (CK_SLOT_ID) -1 && slot_id != slots[i])
+ continue;
+ if ((*m)->C_GetSlotInfo(slots[i], &slot_info) !=
+ CKR_OK ||
+ !p11_kit_uri_match_slot_info(data->uri,
+ &slot_info) ||
+ (*m)->C_GetTokenInfo(slots[i], &token_info) !=
+ CKR_OK ||
+ !p11_kit_uri_match_token_info(data->uri,
+ &token_info))
+ continue;
+ *module = *m;
+ *slot = slots[i];
+ os_free(slots);
+ return 0;
+ }
+ os_free(slots);
+ }
+
+ wpa_printf(MSG_ERROR, "EXT PW PKCS11: No token matches the PKCS#11 URI");
+ return -1;
+}
+
+
+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;
+ CK_RV rv;
+
+ if (find_token(data, module, &slot) < 0)
+ return -1;
+
+ rv = (*module)->C_OpenSession(slot, CKF_SERIAL_SESSION, NULL, NULL,
+ session);
+ if (rv != CKR_OK) {
+ wpa_printf(MSG_ERROR, "EXT PW PKCS11: C_OpenSession failed: %s",
+ p11_kit_strerror(rv));
+ return -1;
+ }
+
+ pin = p11_kit_uri_get_pin_value(data->uri);
+ if (!pin)
+ return 0;
+
+ /* 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));
+ 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));
+ goto fail;
+ }
+ return 0;
+
+fail:
+ (*module)->C_CloseSession(*session);
+ return -1;
+}
+
+
+/* Find up to max data objects with the label name */
+static int find_objects(CK_FUNCTION_LIST *module, CK_SESSION_HANDLE session,
+ const char *name, CK_OBJECT_HANDLE *objects,
+ CK_ULONG max, CK_ULONG *count)
+{
+ CK_OBJECT_CLASS class = CKO_DATA;
+ CK_ATTRIBUTE templ[] = {
+ { CKA_CLASS, &class, sizeof(class) },
+ { CKA_LABEL, (void *) name, os_strlen(name) },
+ };
+ CK_RV rv;
+
+ rv = module->C_FindObjectsInit(session, templ, ARRAY_SIZE(templ));
+ if (rv != CKR_OK) {
+ wpa_printf(MSG_ERROR,
+ "EXT PW PKCS11: C_FindObjectsInit failed: %s",
+ p11_kit_strerror(rv));
+ return -1;
+ }
+ rv = module->C_FindObjects(session, objects, max, count);
+ module->C_FindObjectsFinal(session);
+ if (rv != CKR_OK) {
+ wpa_printf(MSG_ERROR, "EXT PW PKCS11: C_FindObjects failed: %s",
+ p11_kit_strerror(rv));
+ return -1;
+ }
+ return 0;
+}
+
+
+/**
+ * ext_password_pkcs11_get - Read a password from the PKCS#11 token
+ * @ctx: The PKCS#11 password backend
+ * @name: CKA_LABEL of the data object holding the password
+ * Returns: Buffer containing the password or %NULL if not found
+ */
+static struct wpabuf * ext_password_pkcs11_get(void *ctx, const char *name)
+{
+ struct ext_password_pkcs11_data *data = ctx;
+ struct wpabuf *pw = NULL;
+ CK_FUNCTION_LIST *module;
+ CK_SESSION_HANDLE session;
+ CK_OBJECT_HANDLE object;
+ CK_ATTRIBUTE value = { CKA_VALUE, NULL, 0 };
+ CK_ULONG count = 0;
+ CK_RV rv;
+
+ wpa_printf(MSG_DEBUG, "EXT PW PKCS11: get(%s)", name);
+
+ if (open_session(data, &module, &session) < 0)
+ return NULL;
+
+ if (find_objects(module, session, name, &object, 1, &count) < 0)
+ goto out;
+ if (count == 0) {
+ wpa_printf(MSG_ERROR,
+ "EXT PW PKCS11: Password for '%s' was not found",
+ name);
+ goto out;
+ }
+
+ rv = module->C_GetAttributeValue(session, object, &value, 1);
+ if (rv != CKR_OK || value.ulValueLen == CK_UNAVAILABLE_INFORMATION ||
+ value.ulValueLen > PKCS11_MAX_PASSWORD_LEN) {
+ wpa_printf(MSG_ERROR,
+ "EXT PW PKCS11: Cannot read password '%s': %s",
+ name, p11_kit_strerror(rv));
+ goto out;
+ }
+
+ pw = ext_password_alloc(value.ulValueLen);
+ if (!pw)
+ goto out;
+ value.pValue = wpabuf_put(pw, value.ulValueLen);
+ rv = module->C_GetAttributeValue(session, object, &value, 1);
+ if (rv != CKR_OK || value.ulValueLen != wpabuf_len(pw)) {
+ wpa_printf(MSG_ERROR,
+ "EXT PW PKCS11: Cannot read password '%s': %s",
+ name, p11_kit_strerror(rv));
+ ext_password_free(pw);
+ pw = NULL;
+ }
+
+out:
+ module->C_CloseSession(session);
+ return pw;
+}
+
+
+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,
+};
diff --git a/wpa_supplicant/Makefile b/wpa_supplicant/Makefile
index 548a4872d..7ab987735 100644
--- a/wpa_supplicant/Makefile
+++ b/wpa_supplicant/Makefile
@@ -1974,6 +1974,14 @@ CFLAGS += -DCONFIG_EXT_PASSWORD_FILE
NEED_EXT_PASSWORD=y
endif
+ifdef CONFIG_EXT_PASSWORD_PKCS11
+OBJS += ../src/utils/ext_password_pkcs11.o
+CFLAGS += -DCONFIG_EXT_PASSWORD_PKCS11
+CFLAGS += $(shell $(PKG_CONFIG) --cflags p11-kit-1)
+LIBS += $(shell $(PKG_CONFIG) --libs p11-kit-1)
+NEED_EXT_PASSWORD=y
+endif
+
ifdef NEED_EXT_PASSWORD
OBJS += ../src/utils/ext_password.o
CFLAGS += -DCONFIG_EXT_PASSWORD
@@ -2305,6 +2313,11 @@ ifdef CONFIG_EXT_PASSWORD_FILE
LIBPASNSO += ../src/utils/ext_password_file.c
endif
+ifdef CONFIG_EXT_PASSWORD_PKCS11
+LIBPASNSO += ../src/utils/ext_password_pkcs11.c
+LIBPASNSO_DEP += $(shell $(PKG_CONFIG) --libs p11-kit-1)
+endif
+
ifdef CONFIG_EXT_PASSWORD_TEST
LIBPASNSO += ../src/utils/ext_password_test.c
endif
diff --git a/wpa_supplicant/defconfig b/wpa_supplicant/defconfig
index 3ecd1e5f7..0ff5a994d 100644
--- a/wpa_supplicant/defconfig
+++ b/wpa_supplicant/defconfig
@@ -566,6 +566,11 @@ 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
+# 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.
+#CONFIG_EXT_PASSWORD_PKCS11=y
# Enable Fast Session Transfer (FST)
#CONFIG_FST=y
@@ -709,7 +714,8 @@ CONFIG_DPP2=y
# paths (root-owned, non-writable regular file under /usr/lib with root-owned
# ancestor directories) before dlopen()ing them. Disabled by default to avoid
# affecting deployments that run wpa_supplicant as a non-root user with shared
-# libraries in a custom directory owned by that user.
+# libraries in a custom directory owned by that user. It also applies to the
+# module-path of the pkcs11 external password backend.
#CONFIG_TLS_ENGINE_TRUSTED_PATH=y
# Post quantum cryptography (IEEE P802.11bt)
diff --git a/wpa_supplicant/wpa_supplicant.conf b/wpa_supplicant/wpa_supplicant.conf
index 4fc3cca52..390bfbc6c 100644
--- a/wpa_supplicant/wpa_supplicant.conf
+++ b/wpa_supplicant/wpa_supplicant.conf
@@ -377,6 +377,31 @@ 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
+# 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
+# 8 to 63 ASCII characters or a PSK of 64 hex digits.
+# The parameter is a PKCS#11 URI (RFC 7512) that selects the token. The
+# "pkcs11:" prefix is optional. p11-kit parses the URI. wpa_supplicant uses the
+# first token that matches the attributes in the URI path:
+# module: library-manufacturer, library-description, library-version
+# 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
+# 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
+# pin-value: the user PIN
+# 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
# Disable P2P functionality
--
2.53.0
More information about the Hostap
mailing list