Skip to contentSkip to conversation

storage: implement network profile encryption

PATCH Superseded James Prestwood 3 participants 15 messages Feb 3, 2022 – Feb 7, 2022 Kbuild

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

James Prestwood

author
#0 lore raw

PATCH v3 1/7 storage: implement network profile encryption

Some users don't like the idea of storing network credentials in plaintext on the file system. As far as IWD goes the credential directory permissions should be set up such that nobody but the owner/root have access but nevertheless they are still plaintext.

For added security these credentials can be encrypted using a secret key. In this patch the origination of the key does not matter, but in this patch series IWD will support a systemd provided key.

The encryption itself will operate on the entire [Security] group as well as all embedded groups. Once encrypted the [Security] group will be replaced with two key/values:

EncryptedSalt - A random string of bytes used for the encryption EncryptedSecurity - A string of bytes containing the encrypted [Security]
group, as well as all embedded groups.

After the profile has been encrypted these values should not be modified. Note that any values added to [Security] after encryption has no effect. Once the profile is encrypted there is no way to modify [Security] without manually decrypting first, or just removing it entirely which effectively treated a 'new' profile.

The encryption/decryption is done using AES-SIV with a salt value and the network SSID as the IV.

Once a key is set any profiles opened will automatically be encrypted and re-written to disk. Modules using network_storage_open will be provided the decrypted profile, and will be unaware it was ever encrypted in the first place. Similarly when network_storage_sync is called the profile will by automatically encrypted and written to disk without the caller needing to do anything special.

A few private storage.c helpers were added to serve several purposes:

__storage_set_encryption_key():
This sets the encryption key direct from systemd then uses extract and expand to create a new fixed length key to do encryption/decryption.

__storage_decrypt():
Low level API to decrypt an l_settings object using a previously set key and the SSID/name for the network. This returns a 'changed' out parameter signifying that the settings need to be encrypted and re-written to disk. The purpose of exposing this is for a standalone decryption tool which does not re-write any settings.

__storage_open():
Wrapper around __storage_decrypt() that handles re-writing a new profile to disk. This was exposed in order to support hotspot profiles.

__storage_encrypt():
Encrypts an l_settings object and returns the full profile as data

---
 Makefile.am   |   3 +-
 src/storage.c | 268 +++++++++++++++++++++++++++++++++++++++++++++++---
 src/storage.h |   8 ++
 3 files changed, 267 insertions(+), 12 deletions(-)

v3:
* Created and exposed __storage_open for use in hotspot.
* Use extract-and-expand APIs to create encryption key

Makefile.am +2 -1
index 89c053a6..35938d22 100644
@@ -381,7 +381,8 @@ tools_hwsim_SOURCES = tools/hwsim.c src/mpdu.h \
 					src/nl80211util.h src/nl80211util.c \
 					src/storage.h src/storage.c \
 					src/common.h src/common.c \
-					src/band.h src/band.c
+					src/band.h src/band.c \
+					src/crypto.h src/crypto.c
 tools_hwsim_LDADD = $(ell_ldadd)
 
 if DBUS_POLICY
src/storage.c +257 -11

314 lines not on this page · raw message

src/storage.h +8 -0
index e1ec2cd4..93f23c6e 100644
@@ -50,3 +50,11 @@ int storage_network_remove(enum security type, const char *ssid);
 
 struct l_settings *storage_known_frequencies_load(void);
 void storage_known_frequencies_sync(struct l_settings *known_freqs);
+
+void __storage_set_encryption_key(const uint8_t *key, size_t key_len);
+int __storage_decrypt(struct l_settings *settings, const char *ssid,
+				bool *changed);
+char *__storage_encrypt(struct l_settings *settings, const char *ssid,
+				size_t *out_len);
+bool __storage_open(struct l_settings *settings, const char *path,
+			enum security type, const char *name);
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v3 2/7 hotspot: implement hotspot profile encryption

Using __storage_open() hotspot can also support profile encyption. The hotspot consortium name is used as the 'ssid' since this stays consistent between hotspot networks for any profile.

---
 src/hotspot.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
src/hotspot.c +21 -5
index 21f50690..832d7a31 100644
@@ -95,12 +95,18 @@ static struct l_settings *hotspot_network_open(struct network_info *info)
 
 	settings = l_settings_new();
 
-	if (!l_settings_load_from_file(settings, config->filename)) {
-		l_settings_free(settings);
-		return NULL;
-	}
+	if (!l_settings_load_from_file(settings, config->filename))
+		goto error;
+
+	if (!__storage_open(settings, config->filename, SECURITY_8021X,
+				config->name))
+		goto error;
 
 	return settings;
+
+error:
+	l_settings_free(settings);
+	return NULL;
 }
 
 static void hotspot_network_sync(struct network_info *info,
@@ -111,7 +117,12 @@ static void hotspot_network_sync(struct network_info *info,
 	struct hs20_config *config = l_container_of(info, struct hs20_config,
 							super);
 
-	data = l_settings_to_data(settings, &length);
+	data = __storage_encrypt(settings, config->name, &length);
+	if (!data) {
+		l_error("Unable to sync profile %s", config->filename);
+		return;
+	}
+
 	write_file(data, length, true, "%s", config->filename);
 	l_free(data);
 }
@@ -338,6 +349,11 @@ static struct hs20_config *hs20_config_new(struct l_settings *settings,
 		goto free_values;
 	}
 
+	if (!__storage_open(settings, filename, SECURITY_8021X, name)) {
+		l_error("Could not open hotspot profile %s", filename);
+		goto free_values;
+	}
+
 	config = l_new(struct hs20_config, 1);
 
 	if (hessid_str) {
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH v3 3/7 main: add SystemdEncrypt option, and initialize key

In reply to James Prestwood

Recently systemd added the ability to pass secret credentials to services via LoadCredentialEncrypted/SetCredentialEncrypted. Once set up the service is able to read the decrypted credentials from a file. The file path is found in the environment variable CREDENTIALS_DIRECTORY + an identifier. The value of SystemdEncrypt should be set to the systemd key ID used when the credential was created.

When SystemdEncrypt is set IWD will attempt to read the decrypted secret from systemd. If at any point this fails warnings will be printed but IWD will continue normally. Its expected that any failures will result in the inability to connect to any networks which have previously encrypted the passphrase/PSK without re-entering the passphrase manually. This could happen, for example, if the systemd secret was changed.

Once the secret is read in it is set into storage to be used for profile encryption/decryption.

---
 src/main.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
src/main.c +64 -0
index d0dbedc8..a345c950 100644
@@ -29,6 +29,10 @@
 #include <errno.h>
 #include <getopt.h>
 #include <signal.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
 #include <ell/ell.h>
 
 #include <ell/useful.h>
@@ -45,6 +49,7 @@
 #include "src/storage.h"
 #include "src/anqp.h"
 #include "src/netconfig.h"
+#include "src/crypto.h"
 
 #include "src/backtrace.h"
 
@@ -397,6 +402,63 @@ done:
 	return r;
 }
 
+/*
+ * Initialize a systemd encryption key for encrypting/decrypting credentials.
+ */
+static void setup_system_key(void)
+{
+	int fd;
+	struct stat st;
+	const char *cred_dir;
+	void *key = NULL;
+	_auto_(l_free) char *path = NULL;
+	_auto_(l_free) char *key_id = NULL;
+
+	key_id = l_settings_get_string(iwd_config, "General", "SystemdEncrypt");
+	if (!key_id)
+		return;
+
+	cred_dir = getenv("CREDENTIALS_DIRECTORY");
+	if (!cred_dir) {
+		l_warn("SystemdEncrypt enabled but CREDENTIALS_DIRECTORY not "
+			"set, check iwd.service file");
+		return;
+	}
+
+	path = l_strdup_printf("%s/%s", cred_dir, key_id);
+
+	if (stat(path, &st) < 0) {
+		l_warn("SystemdEncrypt enabled but could not stat %s",
+			path);
+		return;
+	}
+
+	fd = open(path, O_RDONLY, 0);
+	if (fd < 0) {
+		l_warn("SystemdEncrypt enabled but cannot open secret (%d)",
+				fd);
+		return;
+	}
+
+	if (fstat(fd, &st) < 0 || st.st_size == 0) {
+		close(fd);
+		return;
+	}
+
+	key = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+	if (key == MAP_FAILED) {
+		l_warn("SystemdEncrypt enabled but can't mmap secret");
+		close(fd);
+		return;
+	}
+
+	close(fd);
+
+	__storage_set_encryption_key(key, st.st_size);
+
+	munmap(key, st.st_size);
+}
+
 int main(int argc, char *argv[])
 {
 	int exit_status;
@@ -529,6 +591,8 @@ int main(int argc, char *argv[])
 	l_dbus_set_disconnect_handler(dbus, dbus_disconnected, NULL, NULL);
 	dbus_init(dbus);
 
+	setup_system_key();
+
 	exit_status = l_main_run_with_signal(signal_handler, NULL);
 
 	iwd_modules_exit();
Signature · 1 line
2.31.1
author
#3 lore raw

PATCH v3 4/7 tools: add decrypt-profile tool

In reply to James Prestwood

This tool will decrypt an IWD network profile which was previously encrypted using a systemd provided key. Either a text passphrase can be provided (--pass) or a file containing the secret (--file).

This can be useful for debugging, or recovering an encrypted profile after enabling SystemdEncrypt.

---
 Makefile.am                 |   8 +-
 tools/iwd-decrypt-profile.c | 240 ++++++++++++++++++++++++++++++++++++
 2 files changed, 247 insertions(+), 1 deletion(-)
 create mode 100644 tools/iwd-decrypt-profile.c

v3:
* Reworked order of operations parsing arguments and how reading from stdin/--infile worked.

Makefile.am +7 -1
index 35938d22..a0e54594 100644
@@ -362,7 +362,7 @@ man_MANS += wired/ead.8
 endif
 endif
 
-noinst_PROGRAMS += tools/probe-req
+noinst_PROGRAMS += tools/probe-req tools/iwd-decrypt-profile
 
 tools_probe_req_SOURCES = tools/probe-req.c src/mpdu.h src/mpdu.c \
 					src/ie.h src/ie.c \
@@ -372,6 +372,12 @@ tools_probe_req_SOURCES = tools/probe-req.c src/mpdu.h src/mpdu.c \
 					src/band.h src/band.c
 tools_probe_req_LDADD = $(ell_ldadd)
 
+tools_iwd_decrypt_profile_SOURCES = tools/iwd-decrypt-profile.c \
+					src/common.h src/common.c \
+					src/crypto.h src/crypto.c \
+					src/storage.h src/storage.c
+tools_iwd_decrypt_profile_LDADD = ${ell_ldadd}
+
 if HWSIM
 bin_PROGRAMS += tools/hwsim
 
tools/iwd-decrypt-profile.c new file +240 -0

240 lines not on this page · raw message

Signature · 1 line
2.31.1
author
#4 lore raw

PATCH v3 5/7 doc: document SystemdEncrypt

In reply to James Prestwood
---
 src/iwd.config.rst  | 16 ++++++++++++++++
 src/iwd.network.rst |  6 +++++-
 2 files changed, 21 insertions(+), 1 deletion(-)
src/iwd.config.rst +16 -0
index 54943702..a3d19fe5 100644
@@ -188,6 +188,22 @@ The group ``[General]`` contains general settings.
        by the kernel so if kernels/drivers exist which don't support OCV it can
        be disabled here.
 
+   * - SystemdEncrypt
+     - Value: Systemd key ID
+
+       Enables network profile encryption using a systemd provided secret key.
+       Once enabled all PSK/8021x network profiles will be encrypted
+       automatically. Once the profile is encrypted there is no way of going
+       back using IWD alone. A tool, decrypt-profile, is provided assuming the
+       secret is known which will decrypt a profile. This decrypted profile
+       could manually be set to /var/lib/iwd to 'undo' any profile encryption,
+       but its going to be a manual process.
+
+       Setting up systemd to provide the secret is left up to the user as IWD
+       has no way of performing this automatically. The systemd options required
+       are LoadCredentialEncrypted or SetCredentialEncrypted, and the secret
+       identifier should be named whatever SystemdEncrypt is set to.
+
 Network
 -------
 
src/iwd.network.rst +5 -1
index e3d1a90a..34f8de1a 100644
@@ -179,7 +179,11 @@ Network Authentication Settings
 -------------------------------
 
 The group ``[Security]`` contains settings for Wi-Fi security and
-authentication configuration.
+authentication configuration. This group can be encrypted by enabling
+``SystemdEncrypt``, see *iwd.config* for details on this option. If this section
+is encrypted (only contains EncryptedSalt/EncryptedSecurity) it should not be
+modified. Modifying these values will result in the inability to connect to that
+network.
 
 .. list-table::
    :header-rows: 0
Signature · 1 line
2.31.1
author
#5 lore raw

PATCH v3 6/7 auto-t: add test for encrypted profiles

In reply to James Prestwood
---
 .../testEncryptedProfiles/connection_test.py  | 154 ++++++++++++++++++
 autotests/testEncryptedProfiles/hw.conf       |   6 +
 autotests/testEncryptedProfiles/iwd-secret    |   1 +
 autotests/testEncryptedProfiles/main.conf     |   2 +
 autotests/testEncryptedProfiles/ssidCCMP.conf |  10 ++
 autotests/testEncryptedProfiles/ssidCCMP.psk  |   2 +
 6 files changed, 175 insertions(+)
 create mode 100644 autotests/testEncryptedProfiles/connection_test.py
 create mode 100644 autotests/testEncryptedProfiles/hw.conf
 create mode 100644 autotests/testEncryptedProfiles/iwd-secret
 create mode 100644 autotests/testEncryptedProfiles/main.conf
 create mode 100644 autotests/testEncryptedProfiles/ssidCCMP.conf
 create mode 100644 autotests/testEncryptedProfiles/ssidCCMP.psk
autotests/testEncryptedProfiles/connection_test.py new file +154 -0
new file mode 100644
index 00000000..dd4892ca
@@ -0,0 +1,154 @@
+#!/usr/bin/python3
+
+import unittest
+import sys
+
+sys.path.append('../util')
+import iwd
+import os
+from iwd import IWD
+from iwd import NetworkType
+from iwd import PSKAgent
+
+class Test(unittest.TestCase):
+    def profile_is_encrypted(self, profile):
+        with open('/tmp/iwd/' + profile) as f:
+            contents = f.read()
+
+        if 'Passphrase' in contents:
+            return False
+
+        return True
+
+    def validate(self, wd):
+        devices = wd.list_devices(1)
+        device = devices[0]
+
+        ordered_network = device.get_ordered_network('ssidCCMP')
+
+        self.assertEqual(ordered_network.type, NetworkType.psk)
+
+        condition = 'not obj.connected'
+        wd.wait_for_object_condition(ordered_network.network_object, condition)
+
+        ordered_network.network_object.connect()
+
+        condition = 'obj.state == DeviceState.connected'
+        wd.wait_for_object_condition(device, condition)
+
+        device.disconnect()
+
+        condition = 'not obj.connected'
+        wd.wait_for_object_condition(ordered_network.network_object, condition)
+
+    # Tests that an existing plaintext profile gets encrypted
+    def test_new_profile(self):
+        IWD.copy_to_storage('ssidCCMP.psk')
+
+        mtime = os.path.getmtime('/tmp/iwd/' + 'ssidCCMP.psk')
+        self.assertFalse(self.profile_is_encrypted('ssidCCMP.psk'))
+
+        wd = IWD(True)
+
+        # Make sure profile was accepted
+        condition = 'len(obj.list_known_networks()) == 1'
+        wd.wait_for_object_condition(wd, condition)
+
+        # Check the file was modified (should be encrypted now)
+        self.assertNotEqual(mtime, os.path.getmtime('/tmp/iwd/' + 'ssidCCMP.psk'))
+
+        self.validate(wd)
+
+        self.assertTrue(self.profile_is_encrypted('ssidCCMP.psk'))
+
+    # Tests that a new connection with agent gets written to an encrypted profile
+    def test_agent_profile(self):
+        wd = IWD(True)
+
+        psk_agent = PSKAgent("secret123")
+        wd.register_psk_agent(psk_agent)
+
+        with self.assertRaises(FileNotFoundError):
+            self.profile_is_encrypted('ssidCCMP.psk')
+
+        self.validate(wd)
+
+        self.assertTrue(self.profile_is_encrypted('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'
+        os.system('echo "%s" > /tmp/iwd/ssidCCMP.psk' % bad_config)
+
+        wd = IWD(True)
+
+        condition = 'len(obj.list_known_networks()) == 1'
+        wd.wait_for_object_condition(wd, condition)
+
+        # IWD should still encrypt the profile automatically
+        self.assertTrue(self.profile_is_encrypted('ssidCCMP.psk'))
+
+        # This should fail
+        with self.assertRaises(iwd.FailedEx):
+            self.validate(wd)
+
+        psk_agent = PSKAgent("secret123")
+        wd.register_psk_agent(psk_agent)
+
+        self.validate(wd)
+
+        self.assertTrue(self.profile_is_encrypted('ssidCCMP.psk'))
+
+    # Tests that a profile that doesn't decrypt wont become a known network
+    def test_decryption_failure(self):
+        bad_config = \
+'''
+[Security]
+EncryptedSalt=000102030405060708090a0b0c0d0e0f
+EncryptedSecurity=aabbccddeeff00112233445566778899
+'''
+        os.system('echo "%s" > /tmp/iwd/ssidCCMP.psk' % bad_config)
+
+        wd = IWD(True)
+
+        self.assertEqual(wd.list_known_networks(), [])
+
+    def test_runtime_profile(self):
+        wd = IWD(True)
+
+        self.assertEqual(wd.list_known_networks(), [])
+
+        # Add profile after IWD starts
+        IWD.copy_to_storage('ssidCCMP.psk')
+
+        self.validate(wd)
+
+        # Should now be encrypted
+        self.assertTrue(self.profile_is_encrypted('ssidCCMP.psk'))
+
+        with open('/tmp/iwd/ssidCCMP.psk') as f:
+            profile = f.read()
+
+        # Edit the profile, corrupting it
+        profile.replace('EncryptedSecurity=', 'EncryptedSecurity=00')
+
+        devices = wd.list_devices(1)
+        device = devices[0]
+        condition = 'obj.state == DeviceState.disconnected'
+        wd.wait_for_object_condition(device, condition)
+
+    def tearDown(self):
+        IWD.clear_storage()
+
+    @classmethod
+    def setUpClass(cls):
+        os.environ['CREDENTIALS_DIRECTORY'] = '/tmp'
+
+    @classmethod
+    def tearDownClass(cls):
+        IWD.clear_storage()
+
+if __name__ == '__main__':
+    unittest.main(exit=True)
autotests/testEncryptedProfiles/hw.conf new file +6 -0
new file mode 100644
index 00000000..de81e1e2
@@ -0,0 +1,6 @@
+[SETUP]
+num_radios=2
+start_iwd=0
+
+[HOSTAPD]
+rad0=ssidCCMP.conf
autotests/testEncryptedProfiles/iwd-secret new file +1 -0
new file mode 100644
index 00000000..c2afca48
@@ -0,0 +1 @@
+secret123
autotests/testEncryptedProfiles/main.conf new file +2 -0
new file mode 100644
index 00000000..35d40c5e
@@ -0,0 +1,2 @@
+[General]
+SystemdEncrypt=iwd-secret
autotests/testEncryptedProfiles/ssidCCMP.conf new file +10 -0
new file mode 100644
index 00000000..c79f5e55
@@ -0,0 +1,10 @@
+hw_mode=g
+channel=1
+ssid=ssidCCMP
+
+wpa=2
+wpa_pairwise=CCMP
+wpa_passphrase=secret123
+
+ieee80211w=2
+wpa_key_mgmt=WPA-PSK-SHA256
autotests/testEncryptedProfiles/ssidCCMP.psk new file +2 -0
new file mode 100644
index 00000000..abafdb66
@@ -0,0 +1,2 @@
+[Security]
+Passphrase=secret123
Signature · 1 line
2.31.1
author
#6 lore raw

PATCH v3 7/7 hotspot: refactor dir watch to be like knownnetworks

In reply to James Prestwood

The create/removed/modified cases all can be combined like they are in known networks. Create/modified were both doing nearly the same thing, and removed can easily be detected if the config was already found.

This is now basically the same logic as in known networks.

---
 src/hotspot.c | 57 ++++++++++++++++-----------------------------------
 1 file changed, 18 insertions(+), 39 deletions(-)
src/hotspot.c +18 -39
index 832d7a31..1f9f74c5 100644
@@ -29,6 +29,7 @@
 #include <stdio.h>
 
 #include <ell/ell.h>
+#include "ell/useful.h"
 
 #include "src/iwd.h"
 #include "src/module.h"
@@ -409,7 +410,7 @@ static void hs20_dir_watch_cb(const char *filename,
 				enum l_dir_watch_event event,
 				void *user_data)
 {
-	struct l_settings *new;
+	_auto_(l_settings_free) struct l_settings *new = NULL;
 	uint64_t connected_time;
 	struct hs20_config *hs20;
 	struct network_config config;
@@ -425,53 +426,31 @@ static void hs20_dir_watch_cb(const char *filename,
 		return;
 
 	full_path = storage_get_hotspot_path("%s", filename);
+	hs20 = l_queue_find(hs20_settings, match_filename, full_path);
 
 	switch (event) {
 	case L_DIR_WATCH_EVENT_CREATED:
-		new = l_settings_new();
-
-		if (!l_settings_load_from_file(new, full_path)) {
-			l_settings_free(new);
-			return;
-		}
-
-		__network_config_parse(new, full_path, &config);
-
-		hs20 = hs20_config_new(new, full_path, &config);
-		l_settings_free(new);
-
-		if (!hs20)
-			return;
-
-		l_queue_push_head(hs20_settings, hs20);
-		break;
 	case L_DIR_WATCH_EVENT_REMOVED:
-		hs20 = l_queue_remove_if(hs20_settings, match_filename,
-						full_path);
-		if (!hs20)
-			return;
-
-		known_networks_remove(&hs20->super);
-		break;
 	case L_DIR_WATCH_EVENT_MODIFIED:
-		hs20 = l_queue_find(hs20_settings, match_filename, full_path);
-		if (!hs20)
-			return;
-
 		new = l_settings_new();
 
-		if (!l_settings_load_from_file(new, full_path)) {
-			l_settings_free(new);
-			return;
+		if (l_settings_load_from_file(new, full_path)) {
+			__network_config_parse(new, full_path, &config);
+
+			if (hs20)
+				known_network_update(&hs20->super, &config);
+			else {
+				hs20 = hs20_config_new(new, full_path, &config);
+				if (!hs20)
+					return;
+
+				l_queue_push_head(hs20_settings, hs20);
+			}
+		} else if (hs20) {
+			l_queue_remove(hs20_settings, hs20);
+			known_networks_remove(&hs20->super);
 		}
 
-		__network_config_parse(new, full_path, &config);
-		known_network_update(&hs20->super, &config);
-
-		/* TODO: Update hotspot specific settings */
-
-		l_settings_free(new);
-
 		break;
 	case L_DIR_WATCH_EVENT_ACCESSED:
 		break;
Signature · 1 line
2.31.1
#7 lore raw
In reply to James Prestwood

Hi James,

Quoted 58 lines · James Prestwood

Some users don't like the idea of storing network credentials in plaintext on the file system. As far as IWD goes the credential directory permissions should be set up such that nobody but the owner/root have access but nevertheless they are still plaintext.

For added security these credentials can be encrypted using a secret key. In this patch the origination of the key does not matter, but in this patch series IWD will support a systemd provided key.

The encryption itself will operate on the entire [Security] group as well as all embedded groups. Once encrypted the [Security] group will be replaced with two key/values:

EncryptedSalt - A random string of bytes used for the encryption EncryptedSecurity - A string of bytes containing the encrypted [Security]
group, as well as all embedded groups.

After the profile has been encrypted these values should not be modified. Note that any values added to [Security] after encryption has no effect. Once the profile is encrypted there is no way to modify [Security] without manually decrypting first, or just removing it entirely which effectively treated a 'new' profile.

The encryption/decryption is done using AES-SIV with a salt value and the network SSID as the IV.

Once a key is set any profiles opened will automatically be encrypted and re-written to disk. Modules using network_storage_open will be provided the decrypted profile, and will be unaware it was ever encrypted in the first place. Similarly when network_storage_sync is called the profile will by automatically encrypted and written to disk without the caller needing to do anything special.

A few private storage.c helpers were added to serve several purposes:

__storage_set_encryption_key():
This sets the encryption key direct from systemd then uses extract and expand to create a new fixed length key to do encryption/decryption.

__storage_decrypt():
Low level API to decrypt an l_settings object using a previously set key and the SSID/name for the network. This returns a 'changed' out parameter signifying that the settings need to be encrypted and re-written to disk. The purpose of exposing this is for a standalone decryption tool which does not re-write any settings.

__storage_open():
Wrapper around __storage_decrypt() that handles re-writing a new profile to disk. This was exposed in order to support hotspot profiles.

__storage_encrypt():
Encrypts an l_settings object and returns the full profile as data

---
  Makefile.am   |   3 +-
  src/storage.c | 268 +++++++++++++++++++++++++++++++++++++++++++++++---
  src/storage.h |   8 ++
  3 files changed, 267 insertions(+), 12 deletions(-)
Quoted 4 lines

v3:
* Created and exposed __storage_open for use in hotspot.
* Use extract-and-expand APIs to create encryption key

This is looking pretty good to me now. Just a few minor points:

src/storage.c const char *storage_network_ssid_from_path(const char *path, · line 381

Quoted 10 lines
src/storage.c +10 -0
+/*
+ * Encrypt needed groups of 'settings' without modifying the object. Returns
+ * the entire settings object as data, with encrypted groups as a bytestring
+ * set as the value to [Security].EncryptedSecurity. This also includes any
+ * embedded groups.
+ *
+ * Note: If encryption is not enabled or there is no Security group this is
+ *       effectively l_settings_to_data.
+ */
+char *__storage_encrypt(struct l_settings *settings, const char *ssid,

should the settings object be const?

src/storage.c const char *storage_network_ssid_from_path(const char *path, · line 388

Quoted 7 lines
src/storage.c +7 -0
+				size_t *out_len)
+{
+	struct iovec ad[2];
+	uint8_t salt[32];
+	size_t len;
+	_auto_(l_free) struct l_settings *to_encrypt = NULL;
+	_auto_(l_free) struct l_settings *original = NULL;

I think you mean l_settings_free here.

Quoted 110 lines
src/storage.c +110 -0
+	_auto_(l_free) char *plaintext = NULL;
+	_auto_(l_free) uint8_t *enc = NULL;
+	_auto_(l_strv_free) char **groups = NULL;
+	char **i;
+
+	if (!system_key_set || !l_settings_has_group(settings, "Security"))
+		return l_settings_to_data(settings, out_len);
+
+	/*
+	 * Make two copies of the settings: One will contain only data to be
+	 * encrypted (to_encrypt), the other will contain data to be left
+	 * unencrypted (original). At the end any encrypted data will be set
+	 * into 'original' as EncryptedSecurity.
+	 */
+	to_encrypt = l_settings_clone(settings);
+	original = l_settings_clone(settings);
+
+	groups = l_settings_get_groups(to_encrypt);
+	for (i = groups; *i; i++) {
+		if (encrypt_group(*i))
+			l_settings_remove_group(original, *i);
+		else
+			l_settings_remove_group(to_encrypt, *i);
+	}
+
+	l_settings_remove_embedded_groups(original);
+
+	plaintext = l_settings_to_data(to_encrypt, &len);
+	if (!plaintext)
+		return NULL;
+
+	l_getrandom(salt, 32);
+
+	ad[0].iov_base = (void *) salt;
+	ad[0].iov_len = 32;
+	ad[1].iov_base = (void *) ssid;
+	ad[1].iov_len = strlen(ssid);
+
+	enc = l_malloc(len + 16);
+
+	if (!aes_siv_encrypt(system_key, sizeof(system_key), plaintext, len,
+				ad, 2, enc)) {
+		l_error("Could not encrypt [Security] group");
+		return NULL;
+	}
+
+	l_settings_set_bytes(original, "Security", "EncryptedSalt", salt, 32);
+	l_settings_set_bytes(original, "Security", "EncryptedSecurity",
+				enc, len + 16);
+
+	return l_settings_to_data(original, out_len);
+}
+
+/*
+ * Decrypt data in [Security].EncryptedSecurity. This data also includes
+ * embedded groups potentially. Once decrypted the data is put back into the
+ * object.
+ *
+ * Note: if encryption is not enabled or there is no Security group settings
+ *       is not modified.
+ */
+int __storage_decrypt(struct l_settings *settings, const char *ssid,
+				bool *changed)
+{
+	_auto_(l_settings_free) struct l_settings *security = NULL;
+	_auto_(l_free) uint8_t *encrypted = NULL;
+	_auto_(l_free) uint8_t *decrypted = NULL;
+	_auto_(l_free) uint8_t *salt = NULL;
+	_auto_(l_strv_free) char **embedded = NULL;
+	size_t elen, slen;
+	struct iovec ad[2];
+
+	if (!system_key_set)
+		goto done;
+
+	if (!l_settings_has_group(settings, "Security"))
+		goto done;
+
+	encrypted = l_settings_get_bytes(settings, "Security",
+						"EncryptedSecurity", &elen);
+	salt = l_settings_get_bytes(settings, "Security",
+						"EncryptedSalt", &slen);
+
+	/*
+	 * Either profile has never been loaded after enabling encryption or is
+	 * missing Encrypted{Salt,Security} values. If either are missing this
+	 * profile is corrupted and must be fixed.
+	 */
+	if (!(encrypted && salt)) {
+		/* Profile corrupted */
+		if (encrypted || salt) {
+			l_warn("Profile %s is corrupted reconfigure manually",
+					ssid);
+			return -EBADMSG;
+		}
+
+		if (changed)
+			*changed = true;
+
+		return 0;
+	}
+
+	decrypted = l_malloc(elen - 16 + 1);
+
+	ad[0].iov_base = (void *)salt;
+	ad[0].iov_len = slen;
+	ad[1].iov_base = (void *)ssid;
+	ad[1].iov_len = strlen(ssid);
+
+	if (!aes_siv_decrypt(system_key, sizeof(system_key), encrypted, elen,

src/storage.c const char *storage_network_ssid_from_path(const char *path, · line 508

Quoted 10 lines
src/storage.c +10 -0
+				ad, 2, decrypted)) {
+		l_error("Could not decrypt %s profile, did the secret change?",
+				ssid);
+		return -ENOKEY;
+	}
+
+	decrypted[elen - 16] = '\0';
+
+	/* Remove encrypted [Security], and copy the decrypted one */
+	l_settings_remove_group(settings, "Security");

Do you want to replace this with a for loop removing all groups listed in the encrypt_groups array?

Should you also be removing any embedded groups just in case?

Quoted 10 lines
src/storage.c +10 -0
+
+	/*
+	 * Load decrypted data into existing settings. This is not how the API
+	 * is indended to be used (since this could result in duplicate groups)
+	 * but since the Security group was just removed and EncryptedSecurity
+	 * should only contain a Security group its safe to use it this way.
+	 */
+	if (!l_settings_load_from_data(settings, (const char *) decrypted,
+					elen - 16)) {
+		l_error("Could not load decrypted security group");

src/storage.c const char *storage_network_ssid_from_path(const char *path, · line 528

Quoted 10 lines
src/storage.c +10 -0
+		return -EBADMSG;
+	}
+
+done:
+	if (changed)
+		*changed = false;
+
+	return 0;
+}
+

Regards,
-Denis

#8 lore raw
In reply to James Prestwood

Hi!

It is my understanding that all secret data is to be kept as securely as possible. I'm not really seeing this carried throughout the source code. Maybe this specific code does not need that level of security?

If I'm right, then:

The secret data should be kept in a mmap(), and mlock(), structure. The prevents secret data from hitting swap memory. After every use of the secret data; it must be explicit_bzero() and then free(). This is so critical and tedious that it should be a macro or something from libell. And those are just initial thoughts for the basic safety of the secret data.

Best regards,
-Joe

#9 lore raw
Quoted 6 lines · Joseph Benden

Hi!

It is my understanding that all secret data is to be kept as securely as possible. I'm not really seeing this carried throughout the source code. Maybe this specific code does not need that level of security?

Quoted 9 lines

If I'm right, then:

The secret data should be kept in a mmap(), and mlock(), structure. The prevents secret data from hitting swap memory. After every use of the secret data; it must be explicit_bzero() and then free(). This is so critical and tedious that it should be a macro or something from libell. And those are just initial thoughts for the basic safety of the secret data.

Also copies must be mlock()'d as well. Also minimize the number of copies, due to all the overhead needed on each one.

Quoted 6 lines

Best regards,
-Joe
_______________________________________________
iwd mailing list -- iwd(a)lists.01.org
To unsubscribe send an email to iwd-leave(a)lists.01.org

author
#10 lore raw
In reply to Joseph Benden

Hi Joseph,

Quoted 7 lines · Joseph Benden

Hi!

It is my understanding that all secret data is to be kept as securely as
possible. I'm not really seeing this carried throughout the source code.
Maybe this specific code does not need that level of security?

Currently any profile (e.g. /var/lib/iwd/foo.psk) containing secret data is unencypted, plaintext. We rely on the file system permissions to 'secure' this data (which this patch series is 'fixing' for anyone who wants these encrypted).

Quoted 11 lines

If I'm right, then:

The secret data should be kept in a mmap(), and mlock(), structure. The
prevents secret data from hitting swap memory. After every use of the secret data; it must be explicit_bzero() and then free(). This is so critical and tedious that it should be a macro or something from libell.
And those are just initial thoughts for the basic safety of the secret data.

So this is how the systemd feature works: The IWD service is given a path to a file. This file is decrypted by systemd and contains the 'secret'. IWD then reads this secret in, does some hashing, and stores the hashed secret in RAM. Once IWD is done it never accesses the file again.

So I don't think its even possible for IWD to do this mmap/mlock dance you're describing. It really doesn't own this secret file, nor does it keep the file contents around long. It just derives a fixed length key and never uses the file again.

As far as explicit_bzero'ing the hashed secret after every use, then reading it back in, and hashing it again when needed... this seems a bit crazy :)

Thanks,
James

Quoted 6 lines

Best regards,
-Joe
_______________________________________________
iwd mailing list -- iwd(a)lists.01.org
To unsubscribe send an email to iwd-leave(a)lists.01.org

author
#11 lore raw
Quoted 38 lines · James Prestwood

Hi Joseph,

Quoted 8 lines · Joseph Benden

Hi!

It is my understanding that all secret data is to be kept as securely
as
possible. I'm not really seeing this carried throughout the source code.
Maybe this specific code does not need that level of security?

Currently any profile (e.g. /var/lib/iwd/foo.psk) containing secret data is unencypted, plaintext. We rely on the file system permissions to 'secure' this data (which this patch series is 'fixing' for anyone who wants these encrypted).

Quoted 13 lines

If I'm right, then:

The secret data should be kept in a mmap(), and mlock(), structure. The
prevents secret data from hitting swap memory. After every use of the
secret data; it must be explicit_bzero() and then free(). This is so
critical and tedious that it should be a macro or something from libell.
And those are just initial thoughts for the basic safety of the secret data.

So this is how the systemd feature works: The IWD service is given a path to a file. This file is decrypted by systemd and contains the 'secret'. IWD then reads this secret in, does some hashing, and stores
the hashed secret in RAM. Once IWD is done it never accesses the file again.

Quoted 7 lines

So I don't think its even possible for IWD to do this mmap/mlock dance
you're describing. It really doesn't own this secret file, nor does it
keep the file contents around long. It just derives a fixed length key
and never uses the file again.

Sorry I misunderstood. We could do this (the mmap/mlock part) but as far as doing it every time we need the secret, I don't see that being feasible.

Quoted 16 lines

As far as explicit_bzero'ing the hashed secret after every use, then reading it back in, and hashing it again when needed... this seems a bit crazy :)

Thanks,
James

Quoted 6 lines

Best regards,
-Joe
_______________________________________________
iwd mailing list -- iwd(a)lists.01.org
To unsubscribe send an email to iwd-leave(a)lists.01.org

#12 lore raw
In reply to James Prestwood
Quoted 10 lines · James Prestwood

Hi Joseph,

Quoted 7 lines · Joseph Benden

Hi!

It is my understanding that all secret data is to be kept as securely as
possible. I'm not really seeing this carried throughout the source code.
Maybe this specific code does not need that level of security?

Quoted 4 lines

Currently any profile (e.g. /var/lib/iwd/foo.psk) containing secret data is unencypted, plaintext. We rely on the file system permissions to 'secure' this data (which this patch series is 'fixing' for anyone who wants these encrypted).

Icky... really shouldn't do it this way; secure all data at rest - even against root account, if at all possible.

Quoted 17 lines
Quoted 11 lines

If I'm right, then:

The secret data should be kept in a mmap(), and mlock(), structure. The
prevents secret data from hitting swap memory. After every use of the secret data; it must be explicit_bzero() and then free(). This is so critical and tedious that it should be a macro or something from libell.
And those are just initial thoughts for the basic safety of the secret data.

So this is how the systemd feature works: The IWD service is given a path to a file. This file is decrypted by systemd and contains the 'secret'. IWD then reads this secret in, does some hashing, and stores the hashed secret in RAM. Once IWD is done it never accesses the file again.

Quoted 4 lines

So I don't think its even possible for IWD to do this mmap/mlock dance you're describing. It really doesn't own this secret file, nor does it keep the file contents around long. It just derives a fixed length key and never uses the file again.

So this is a can of worms... As soon as the secret data hits disk, it is out-of-ones own control; considered revealed and/or exploited. I sure do hope systemd does the right things here... (facepalm)

Quoted 4 lines

As far as explicit_bzero'ing the hashed secret after every use, then reading it back in, and hashing it again when needed... this seems a bit crazy :)

Maybe crazy, most certainly needed. If not now, probably needed in the near future (after some horrible event).

So let's talk about data flows for a moment.

IWD service starts and initializes a secret data vault. This is a region of memory which is mmap()-ed, mlock() protected, and which happens to point to the start of a fixed struct - that is for IWD service's secret data store. [I envision libell having support for application secret vaults]

The external secret data enters from a file; it is read to the secret's vault, and is securely processed to have the derived secret data (these two values are in the IWD service's secret data store).

Providing compatible with systemd's process; the secret data file is shredded (see DoD -style procedures for examples), and unlink()-ed from the file-system. Also (shred) explicit_bzero()-ing the filenames used and any and all memory used that is not inside of the IWD service's secret data store. [I envision libell having a file_shred() function, using the 6/8-pass (iirc) or so methods.]

Now, when the secret derived data is needed for incoming/outgoing frames; it is always used directly from the service's secret data store. Try your darn-ist to not copy it out of its place in the vault, and if it is: it must be securely used and tracked for correctness.

Finally at program shutdown, the IWD service's secret data store is shredded (this is explicit_bzero()-ed), then munlock(), and munmap()-ed.

Best regards,
-Joe

author
#13 lore raw

Hi Joe,

Quoted 103 lines · Joseph Benden
Quoted 12 lines · James Prestwood

Hi Joseph,

Quoted 9 lines · Joseph Benden

Hi!

It is my understanding that all secret data is to be kept as securely
as
possible. I'm not really seeing this carried throughout the source
code.
Maybe this specific code does not need that level of security?

Quoted 6 lines

Currently any profile (e.g. /var/lib/iwd/foo.psk) containing secret data is unencypted, plaintext. We rely on the file system permissions
to 'secure' this data (which this patch series is 'fixing' for anyone
who wants these encrypted).

Icky... really shouldn't do it this way; secure all data at rest - even
against root account, if at all possible.

Quoted 23 lines
Quoted 14 lines

If I'm right, then:

The secret data should be kept in a mmap(), and mlock(), structure.
The
prevents secret data from hitting swap memory. After every use of the
secret data; it must be explicit_bzero() and then free(). This is so
critical and tedious that it should be a macro or something from libell.
And those are just initial thoughts for the basic safety of the secret data.

So this is how the systemd feature works: The IWD service is given a
path to a file. This file is decrypted by systemd and contains the 'secret'. IWD then reads this secret in, does some hashing, and stores
the hashed secret in RAM. Once IWD is done it never accesses the file
again.

Quoted 7 lines

So I don't think its even possible for IWD to do this mmap/mlock dance
you're describing. It really doesn't own this secret file, nor does it
keep the file contents around long. It just derives a fixed length key
and never uses the file again.

So this is a can of worms... As soon as the secret data hits disk, it is
out-of-ones own control; considered revealed and/or exploited. I sure do
hope systemd does the right things here... (facepalm)

Quoted 6 lines

As far as explicit_bzero'ing the hashed secret after every use, then
reading it back in, and hashing it again when needed... this seems a
bit crazy :)

Maybe crazy, most certainly needed. If not now, probably needed in the
near future (after some horrible event).

So let's talk about data flows for a moment.

IWD service starts and initializes a secret data vault. This is a region
of memory which is mmap()-ed, mlock() protected, and which happens to point to the start of a fixed struct - that is for IWD service's secret
data store. [I envision libell having support for application secret vaults]

The external secret data enters from a file; it is read to the secret's
vault, and is securely processed to have the derived secret data (these
two values are in the IWD service's secret data store).

Providing compatible with systemd's process; the secret data file is shredded (see DoD -style procedures for examples), and unlink()-ed from
the file-system. Also (shred) explicit_bzero()-ing the filenames used and any and all memory used that is not inside of the IWD service's secret data store. [I envision libell having a file_shred() function, using the 6/8-pass (iirc) or so methods.]

Now, when the secret derived data is needed for incoming/outgoing frames; it is always used directly from the service's secret data store.
Try your darn-ist to not copy it out of its place in the vault, and if
it is: it must be securely used and tracked for correctness.

Quoted 3 lines

Finally at program shutdown, the IWD service's secret data store is shredded (this is explicit_bzero()-ed), then munlock(), and munmap()- ed.

I totally understand but we do have to balance what is actually feasible to do right now. At least with how this systemd feature works the above isn't possible at least from IWD's perspective.

It also comes down the work vs benefit and how far we are willing to go to protect these "secrets", which are wifi credentials. If someone has root access on your box its all over, leaking your wifi password isn't going to change much. Not trying to downplay the importance of security, but this is my honest take on it.

I see your point from an enterprise security perspective, and maybe this is something we need to look into in the future but for now we just gotta do what we can. Encrypting credentials with systemd is certainly better than plaintext. Also this feature works with TPM so even better (but yes, they way systemd did it still write a file to disk).

For a person who absolutely cannot store credentials on disk you have the option to not write any secrets to the profile, i.e. enter your passphrase/username etc each time you connect.

Thanks,
James

Quoted 7 lines

Best regards,
-Joe

_______________________________________________
iwd mailing list -- iwd(a)lists.01.org
To unsubscribe send an email to iwd-leave(a)lists.01.org

#14 lore raw
In reply to Joseph Benden

Hi Joseph,

Quoted 11 lines · Joseph Benden
Quoted 10 lines · James Prestwood

Hi Joseph,

Quoted 7 lines · Joseph Benden

Hi!

It is my understanding that all secret data is to be kept as securely as
possible. I'm not really seeing this carried throughout the source code.
Maybe this specific code does not need that level of security?

Quoted 8 lines
Quoted 4 lines

Currently any profile (e.g. /var/lib/iwd/foo.psk) containing secret data is unencypted, plaintext. We rely on the file system permissions to 'secure' this data (which this patch series is 'fixing' for anyone who wants these encrypted).

Icky... really shouldn't do it this way; secure all data at rest - even against root account, if at all possible.

There are lots of interesting ideas in this thread... I'm not aware of any networking daemons actually doing any of the stuff you describe here. iwd could be the first of course.

Quoted 4 lines

So this is a can of worms... As soon as the secret data hits disk, it is out-of-ones own control; considered revealed and/or exploited. I sure do hope systemd does the right things here... (facepalm)

Well, you may want to check what systemd is doing first. I suspect the data never actually hits disk, but goes into tmpfs.

Quoted 11 lines
Quoted 5 lines

As far as explicit_bzero'ing the hashed secret after every use, then reading it back in, and hashing it again when needed... this seems a bit crazy :)

Maybe crazy, most certainly needed. If not now, probably needed in the near future (after some horrible event).

So let's talk about data flows for a moment.

Quoted 9 lines

IWD service starts and initializes a secret data vault. This is a region of memory which is mmap()-ed, mlock() protected, and which happens to point to the start of a fixed struct - that is for IWD service's secret data store. [I envision libell having support for application secret vaults]

The external secret data enters from a file; it is read to the secret's vault, and is securely processed to have the derived secret data (these two values are in the IWD service's secret data store).

Sounds useful. Any chance you would be willing to contribute?

Quoted 5 lines

Providing compatible with systemd's process; the secret data file is shredded (see DoD -style procedures for examples), and unlink()-ed from the file-system. Also (shred) explicit_bzero()-ing the filenames used and any and all memory used that is not inside of the IWD service's secret data store. [I envision libell having a file_shred() function, using the 6/8-pass (iirc) or so methods.]

See above. I doubt this is needed. But even if systemd somehow writes the secret to disk, it is not up to iwd to destroy it.

Quoted 5 lines

Now, when the secret derived data is needed for incoming/outgoing frames; it is always used directly from the service's secret data store. Try your darn-ist to not copy it out of its place in the vault, and if it is: it must be securely used and tracked for correctness.

Again, this sounds useful and could be fairly easily accomplished for many of the common scenarios. For example, most of the derived keys for the current connection are already stored in a single object and such memory could be locked if desired. But this is a separate topic from what this patch set is trying to accomplish. Feel free to start a new thread on this topic specifically.

Quoted 4 lines

Finally at program shutdown, the IWD service's secret data store is shredded (this is explicit_bzero()-ed), then munlock(), and munmap()-ed.

See above. systemd owns the secret, so there's nothing for iwd to do here.

Regards,
-Denis