Skip to contentSkip to conversation

agent: add passphrase storage options

PATCH In review Matthias Kurz 2 participants 6 messages Apr 24 – Apr 28

lore
Share & tools
lore
#0 lore raw

cover agent: add passphrase storage options

Add an agent API variant for PSK passphrase requests that allows the agent to tell IWD whether the passphrase should be persisted in the network profile.

This is needed by NetworkManager's IWD backend for profiles where the Wi-Fi PSK is stored as an agent-owned or not-saved secret. In those cases NetworkManager can provide the passphrase for the current connection attempt, but IWD should not save it into /var/lib/iwd/*.psk.

The new RequestPassphraseWithOptions method returns the passphrase plus an options dictionary. Currently the only option is Store. If Store is false, IWD uses the passphrase for the current connection attempt but does not write PSK secrets back to the profile.

The method falls back to RequestPassphrase for older agents.

NetworkManager-side draft MR:
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/2402

Matthias Kurz (3):
agent: Add passphrase storage options
doc: Document passphrase storage options
auto-t: Test transient PSK agent secrets

 .../testEncryptedProfiles/connection_test.py  |  27 +++++
 autotests/util/iwd.py                         |  14 ++-
 doc/agent-api.txt                             |  12 ++
 src/agent.c                                   | 108 +++++++++++++++---
 src/agent.h                                   |   1 +
 src/network.c                                 |  12 ++
 6 files changed, 159 insertions(+), 15 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/3 agent: Add passphrase storage options

Add RequestPassphraseWithOptions support for PSK passphrase requests. The method lets an agent return the passphrase together with options that affect how IWD handles it.

Currently the only option is Store. When Store is false, use the passphrase for the current connection attempt but keep it out of the network profile.

Fall back to RequestPassphrase if the agent does not implement the new method.

---
 src/agent.c   | 108 +++++++++++++++++++++++++++++++++++++++++++-------
 src/agent.h   |   1 +
 src/network.c |  12 ++++++
 3 files changed, 107 insertions(+), 14 deletions(-)
src/agent.c +94 -14
index 0f718b87..03538db1 100644
@@ -44,11 +44,13 @@ enum agent_request_type {
 struct agent_request {
 	enum agent_request_type type;
 	struct l_dbus_message *message;
+	char *path;
 	unsigned int id;
 	void *user_data;
 	void *user_callback;
 	struct l_dbus_message *trigger;
 	agent_request_destroy_func_t destroy;
+	bool passphrase_with_options;
 };
 
 struct agent {
@@ -126,6 +128,7 @@ static void agent_request_free(void *user_data)
 	if (request->destroy)
 		request->destroy(request->user_data);
 
+	l_free(request->path);
 	l_free(request);
 }
 
@@ -134,19 +137,37 @@ static void passphrase_reply(struct l_dbus_message *reply,
 {
 	const char *error, *text;
 	char *passphrase = NULL;
+	bool store = true;
 	enum agent_result result = AGENT_RESULT_FAILED;
 	agent_request_passphrase_func_t user_callback = request->user_callback;
 
 	if (l_dbus_message_get_error(reply, &error, &text))
 		goto done;
 
-	if (!l_dbus_message_get_arguments(reply, "s", &passphrase))
+	if (request->passphrase_with_options) {
+		struct l_dbus_message_iter options;
+		struct l_dbus_message_iter value;
+		const char *key;
+
+		if (!l_dbus_message_get_arguments(reply, "sa{sv}",
+							&passphrase, &options))
+			goto done;
+
+		while (l_dbus_message_iter_next_entry(&options, &key, &value)) {
+			if (!strcmp(key, "Store")) {
+				if (!l_dbus_message_iter_get_variant(&value,
+								"b", &store))
+					goto done;
+			}
+		}
+	} else if (!l_dbus_message_get_arguments(reply, "s", &passphrase))
 		goto done;
 
 	result = AGENT_RESULT_OK;
 
 done:
-	user_callback(result, passphrase, request->trigger, request->user_data);
+	user_callback(result, passphrase, store, request->trigger,
+			request->user_data);
 }
 
 static void user_name_passwd_reply(struct l_dbus_message *reply,
@@ -224,6 +245,57 @@ static void agent_free(void *data)
 }
 
 static void agent_send_next_request(struct agent *agent);
+static void agent_receive_reply(struct l_dbus_message *message,
+							void *user_data);
+
+static struct l_dbus_message *agent_new_passphrase_message(struct agent *agent,
+					const char *path, bool with_options)
+{
+	struct l_dbus_message *message;
+
+	message = l_dbus_message_new_method_call(dbus_get_bus(),
+							agent->owner,
+							agent->path,
+							IWD_AGENT_INTERFACE,
+							with_options ?
+							"RequestPassphraseWithOptions" :
+							"RequestPassphrase");
+
+	l_dbus_message_set_arguments(message, "o", path);
+
+	return message;
+}
+
+static bool agent_retry_passphrase_without_options(struct agent *agent,
+					struct agent_request *request,
+					struct l_dbus_message *reply)
+{
+	const char *error, *text;
+
+	if (request->type != AGENT_REQUEST_TYPE_PASSPHRASE ||
+			!request->passphrase_with_options)
+		return false;
+
+	if (!l_dbus_message_get_error(reply, &error, &text))
+		return false;
+
+	if (strcmp(error, "org.freedesktop.DBus.Error.UnknownMethod") &&
+			strcmp(error, "org.freedesktop.DBus.Error.UnknownInterface"))
+		return false;
+
+	request->passphrase_with_options = false;
+	request->message = agent_new_passphrase_message(agent, request->path,
+							false);
+
+	agent->pending_id = l_dbus_send_with_reply(dbus_get_bus(),
+							request->message,
+							agent_receive_reply,
+							agent, NULL);
+
+	request->message = NULL;
+
+	return true;
+}
 
 static void request_timeout(struct l_timeout *timeout, void *user_data)
 {
@@ -242,11 +314,17 @@ static void agent_receive_reply(struct l_dbus_message *message,
 							void *user_data)
 {
 	struct agent *agent = user_data;
+	struct agent_request *pending;
 
 	l_debug("agent %p request id %u", agent, agent->pending_id);
 
 	agent->pending_id = 0;
 
+	pending = l_queue_peek_head(agent->requests);
+	if (pending && agent_retry_passphrase_without_options(agent, pending,
+								message))
+		return;
+
 	agent_finalize_pending(agent, message);
 
 	if (!agent->pending_id)
@@ -281,7 +359,9 @@ static unsigned int agent_queue_request(struct agent *agent,
 					int timeout, void *callback,
 					struct l_dbus_message *trigger,
 					void *user_data,
-					agent_request_destroy_func_t destroy)
+					agent_request_destroy_func_t destroy,
+					const char *path,
+					bool passphrase_with_options)
 {
 	struct agent_request *request;
 
@@ -289,11 +369,13 @@ static unsigned int agent_queue_request(struct agent *agent,
 
 	request->type = type;
 	request->message = message;
+	request->path = l_strdup(path);
 	request->id = ++next_request_id;
 	request->user_data = user_data;
 	request->user_callback = callback;
 	request->trigger = l_dbus_message_ref(trigger);
 	request->destroy = destroy;
+	request->passphrase_with_options = passphrase_with_options;
 
 	agent->timeout_secs = timeout;
 
@@ -365,17 +447,12 @@ unsigned int agent_request_passphrase(const char *path,
 
 	l_debug("agent %p owner %s path %s", agent, agent->owner, agent->path);
 
-	message = l_dbus_message_new_method_call(dbus_get_bus(),
-							agent->owner,
-							agent->path,
-							IWD_AGENT_INTERFACE,
-							"RequestPassphrase");
-
-	l_dbus_message_set_arguments(message, "o", path);
+	message = agent_new_passphrase_message(agent, path, true);
 
 	return agent_queue_request(agent, AGENT_REQUEST_TYPE_PASSPHRASE,
 					message, agent_timeout_input_request(),
-					callback, trigger, user_data, destroy);
+					callback, trigger, user_data, destroy,
+					path, true);
 }
 
 unsigned int agent_request_pkey_passphrase(const char *path,
@@ -401,7 +478,8 @@ unsigned int agent_request_pkey_passphrase(const char *path,
 
 	return agent_queue_request(agent, AGENT_REQUEST_TYPE_PASSPHRASE,
 					message, agent_timeout_input_request(),
-					callback, trigger, user_data, destroy);
+					callback, trigger, user_data, destroy,
+					NULL, false);
 }
 
 unsigned int agent_request_user_name_password(const char *path,
@@ -427,7 +505,8 @@ unsigned int agent_request_user_name_password(const char *path,
 
 	return agent_queue_request(agent, AGENT_REQUEST_TYPE_USER_NAME_PASSWD,
 					message, agent_timeout_input_request(),
-					callback, trigger, user_data, destroy);
+					callback, trigger, user_data, destroy,
+					NULL, false);
 }
 
 unsigned int agent_request_user_password(const char *path, const char *user,
@@ -452,7 +531,8 @@ unsigned int agent_request_user_password(const char *path, const char *user,
 
 	return agent_queue_request(agent, AGENT_REQUEST_TYPE_PASSPHRASE,
 					message, agent_timeout_input_request(),
-					callback, trigger, user_data, destroy);
+					callback, trigger, user_data, destroy,
+					NULL, false);
 }
 
 static bool find_request(const void *a, const void *b)
src/agent.h +1 -0
index eb08c6b6..220339db 100644
@@ -29,6 +29,7 @@ enum agent_result {
 
 typedef void (*agent_request_passphrase_func_t) (enum agent_result result,
 					const char *passphrase,
+					bool store,
 					struct l_dbus_message *message,
 					void *user_data);
 typedef void (*agent_request_user_name_passwd_func_t) (enum agent_result result,
src/network.c +12 -0
index a5a2375a..1d14c04c 100644
@@ -84,6 +84,7 @@ struct network {
 	char **nai_realms;
 	uint8_t *rc_ie;
 	bool sync_settings:1;  /* should settings be synced on connect? */
+	bool agent_passphrase_transient:1;
 	bool ask_passphrase:1; /* Whether we should force-ask agent */
 	bool is_hs20:1;
 	bool anqp_pending:1;	/* Set if there is a pending ANQP request */
@@ -324,6 +325,7 @@ static bool __network_set_passphrase(struct network *network,
 
 	network_reset_passphrase(network);
 	network->passphrase = l_strdup(passphrase);
+	network->agent_passphrase_transient = false;
 
 	network->sae_pt_19 = network_generate_sae_pt(network, 19);
 	network->sae_pt_20 = network_generate_sae_pt(network, 20);
@@ -664,6 +666,7 @@ static int network_load_psk(struct network *network, struct scan_bss *bss)
 
 	network_reset_passphrase(network);
 	network_reset_psk(network);
+	network->agent_passphrase_transient = false;
 	network->passphrase = l_steal_ptr(passphrase);
 	network->password_identifier = l_steal_ptr(password_id);
 
@@ -727,6 +730,9 @@ static void network_settings_save(struct network *network,
 	/* We only update the [Security] bits here, wipe the group first */
 	l_settings_remove_group(settings, "Security");
 
+	if (network->agent_passphrase_transient)
+		return;
+
 	if (network->psk)
 		l_settings_set_bytes(settings, "Security", "PreSharedKey",
 					network->psk, 32);
@@ -1316,6 +1322,7 @@ struct scan_bss *network_bss_select(struct network *network,
 
 static void passphrase_callback(enum agent_result result,
 				const char *passphrase,
+				bool store,
 				struct l_dbus_message *message,
 				void *user_data)
 {
@@ -1355,6 +1362,8 @@ static void passphrase_callback(enum agent_result result,
 		goto err;
 	}
 
+	network->agent_passphrase_transient = !store;
+
 	station_connect_network(station, network, bss, message);
 	l_dbus_message_unref(message);
 	return;
@@ -1424,11 +1433,14 @@ static bool eap_secret_info_match_local(const void *a, const void *b)
 }
 
 static void eap_password_callback(enum agent_result result, const char *value,
+					bool store,
 					struct l_dbus_message *message,
 					void *user_data)
 {
 	struct eap_secret_request *req = user_data;
 
+	(void) store;
+
 	req->network->agent_request = 0;
 
 	if (value) {
Signature · 1 line
2.54.0
#2 lore raw

PATCH 2/3 doc: Document passphrase storage options

In reply to Matthias Kurz

Document RequestPassphraseWithOptions and the Store option for agents that want to control whether a PSK passphrase is saved in the network profile.

---
 doc/agent-api.txt | 12 ++++++++++++
 1 file changed, 12 insertions(+)
doc/agent-api.txt +12 -0
index e9bb95ca..77e9bc6d 100644
@@ -90,6 +90,18 @@ Methods		void Release() [noreply]
 
 			Possible Errors: net.connman.iwd.Agent.Error.Canceled
 
+		(string, dict) RequestPassphraseWithOptions(object network)
+
+			This method gets called when trying to connect to
+			a network and passphrase is required.  Agents
+			implementing this method can return options for the
+			passphrase.  If the Store boolean option is present
+			and false, IWD will use the passphrase for this
+			connection attempt but will not save it to the
+			network profile.
+
+			Possible Errors: net.connman.iwd.Agent.Error.Canceled
+
 		string RequestPrivateKeyPassphrase(object network)
 
 			This method gets called when connecting to
Signature · 1 line
2.54.0
#3 lore raw

PATCH 3/3 auto-t: Test transient PSK agent secrets

In reply to Matthias Kurz

Extend the PSK agent test helper to implement RequestPassphraseWithOptions and add coverage for Store=false. The test verifies that IWD can use the agent-provided passphrase without writing PSK secrets back to the profile.

---
 .../testEncryptedProfiles/connection_test.py  | 27 +++++++++++++++++++
 autotests/util/iwd.py                         | 14 +++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)
autotests/testEncryptedProfiles/connection_test.py +27 -0
index eb7fd7c5..17c5ff11 100644
@@ -21,6 +21,17 @@ class Test(unittest.TestCase):
 
         return True
 
+    def profile_has_psk_secret(self, profile):
+        with open('/tmp/iwd/' + profile) as f:
+            contents = f.read()
+
+        return any(secret in contents for secret in [
+            'Passphrase',
+            'PreSharedKey',
+            'SAE-PT-Group19',
+            'SAE-PT-Group20',
+        ])
+
     def validate(self, wd):
         devices = wd.list_devices(1)
         device = devices[0]
@@ -80,6 +91,22 @@ class Test(unittest.TestCase):
 
         wd.unregister_psk_agent(psk_agent)
 
+    # Tests that an agent can request one-time use of a passphrase.
+    def test_agent_transient_profile(self):
+        wd = IWD(True)
+
+        psk_agent = PSKAgent("secret123", store=False)
+        wd.register_psk_agent(psk_agent)
+
+        with self.assertRaises(FileNotFoundError):
+            self.profile_has_psk_secret('ssidCCMP.psk')
+
+        self.validate(wd)
+
+        self.assertFalse(self.profile_has_psk_secret('ssidCCMP.psk'))
+
+        wd.unregister_psk_agent(psk_agent)
+
     # Tests that an invalid profile gets re-written after an agent request
     def test_invalid_profile_rewritten(self):
         bad_config = '[Security]\nPassphrase=incorrect\n'
autotests/util/iwd.py +13 -1
index 37eb4943..54096b01 100755
@@ -1153,7 +1153,7 @@ agent_count = 0
 
 class PSKAgent(dbus.service.Object):
 
-    def __init__(self, passphrases=[], users=[], namespace=ctx):
+    def __init__(self, passphrases=[], users=[], namespace=ctx, store=True):
         global agent_count
 
         if type(passphrases) != list:
@@ -1162,6 +1162,7 @@ class PSKAgent(dbus.service.Object):
         if type(users) != list:
             users = [users]
         self.users = users
+        self.store = store
         self._path = '/test/agent/%s' % agent_count
         self._bus = dbus.bus.BusConnection(address_or_type=namespace.dbus_address)
 
@@ -1193,6 +1194,17 @@ class PSKAgent(dbus.service.Object):
 
         return self.passphrases.pop(0)
 
+    @dbus.service.method(IWD_AGENT_INTERFACE, in_signature='o',
+                                                        out_signature='sa{sv}')
+    def RequestPassphraseWithOptions(self, path):
+        print('Requested PSK with options for ' + path)
+
+        if not self.passphrases:
+            raise CanceledEx("canceled")
+
+        return (self.passphrases.pop(0),
+                {'Store': dbus.Boolean(self.store, variant_level=1)})
+
     @dbus.service.method(IWD_AGENT_INTERFACE, in_signature='o',
                                                               out_signature='s')
     def RequestPrivateKeyPassphrase(self, path):
Signature · 1 line
2.54.0
#4 lore raw
In reply to Matthias Kurz

Hi Mattias,

Quoted 9 lines · Matthias Kurz

Add an agent API variant for PSK passphrase requests that allows the agent to tell IWD whether the passphrase should be persisted in the network profile.

This is needed by NetworkManager's IWD backend for profiles where the Wi-Fi PSK is stored as an agent-owned or not-saved secret. In those cases NetworkManager can provide the passphrase for the current connection attempt, but IWD should not save it into /var/lib/iwd/*.psk.

Quoted 4 lines

The new RequestPassphraseWithOptions method returns the passphrase plus an options dictionary. Currently the only option is Store. If Store is false, IWD uses the passphrase for the current connection attempt but does not write PSK secrets back to the profile.

Do you anticipate additional options needing to be passed? I'm not really liking the idea of adding another agent API. Another option instead of a new DBus API could be that we have a main.conf option, something like:

[General].DisableStorage=true

If NM is always providing the passphrase via the agent API I don't see why we'd need to keep an option within the network profiles right?

Thanks,

James

Quoted 19 lines

The method falls back to RequestPassphrase for older agents.

NetworkManager-side draft MR:
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/2402

Matthias Kurz (3):
agent: Add passphrase storage options
doc: Document passphrase storage options
auto-t: Test transient PSK agent secrets

  .../testEncryptedProfiles/connection_test.py  |  27 +++++
  autotests/util/iwd.py                         |  14 ++-
  doc/agent-api.txt                             |  12 ++
  src/agent.c                                   | 108 +++++++++++++++---
  src/agent.h                                   |   1 +
  src/network.c                                 |  12 ++
  6 files changed, 159 insertions(+), 15 deletions(-)
#5 lore raw

Hi James,

thanks for taking a look.

Quoted 1 line

Do you anticipate additional options needing to be passed?

Not immediately.

Quoted 8 lines

I'm not really liking the idea of adding another agent API. Another option instead of a new DBus API could be that we have a main.conf option, something like:

[General].DisableStorage=true

If NM is always providing the passphrase via the agent API I don't see why we'd need to keep an option within the network profiles right?

I understand the concern about adding another Agent D-Bus method only to carry one storage option. I also agree that if the agent is always able to provide the passphrase, then iwd should not necessarily have to persist that passphrase itself.

I am less convinced that a global DisableStorage=true option is the right model though.

That would apply to the whole iwd daemon, while the ownership question is really per profile. A system can have profiles that are managed directly by iwd and should keep the normal iwd behavior, and other profiles that are mirrored by an external manager such as NetworkManager. For the mirrored profiles, the external manager is the source of truth for the file contents and for the secret storage policy. For native iwd profiles, iwd should still be free to update them as it does today.

A global option would also make deployment awkward, because enabling it for NetworkManager would affect existing hand-written or iwd-managed profiles on the same machine. Conversely, leaving it disabled means externally managed profiles can still be rewritten by iwd.

I came up with a different approach that avoids adding a new Agent API:
a per-profile [Settings].ExternallyManaged=true flag.

With that flag, iwd still reads the profile and can use it for connection setup, but it does not update the file as a side effect of normal network operation. This makes sense independently of NetworkManager: if someone handcrafts an iwd profile and marks it externally managed, iwd should treat the file as externally owned (and treat it as read-only). If NetworkManager mirrors a profile into /var/lib/iwd, it can add the same flag and keep owning the contents.

I will send a small patch series for this shortly.

Thanks,
Matthias