luks: add test for FIDO2 (#1264)

This commit is contained in:
Michael Hoang authored and GitHub committed 2026-05-25 09:00:11 +00:00
commit 5ba0c9555c
3 files changed
+87 -47

No files matched your search

+9 -1
View File
@@ -96,8 +96,13 @@ let
# Get qemu-common library functions for this pkgs # Get qemu-common library functions for this pkgs
qemu-common = qemu-common-lib pkgs; qemu-common = qemu-common-lib pkgs;
qemuPkg = pkgs.qemu_test.override {
canokeySupport = enableCanokey;
};
# qemuBinary returns a string like: "/nix/store/.../qemu-system-x86_64 -machine accel=kvm:tcg -cpu max" # qemuBinary returns a string like: "/nix/store/.../qemu-system-x86_64 -machine accel=kvm:tcg -cpu max"
qemuBinaryString = qemu-common.qemuBinary pkgs.qemu_test; qemuBinaryString = qemu-common.qemuBinary qemuPkg;
# for installation we skip /dev/vda because it is the test runner disk # for installation we skip /dev/vda because it is the test runner disk
@@ -301,6 +306,9 @@ let
"-device pci-ohci,id=usb-bus" "-device pci-ohci,id=usb-bus"
"-device canokey,bus=usb-bus.0,file=/tmp/canokey-file" "-device canokey,bus=usb-bus.0,file=/tmp/canokey-file"
]; ];
# REMOVEME when Canokey support is enabled upstream
qemu.package = lib.mkForce qemuPkg;
}; };
# useful for debugging via repl # useful for debugging via repl
+60 -46
View File
@@ -62,10 +62,25 @@ let
''; '';
# Use this open command when you want to open it after full enrollment, e.g. at mount time or in standard enrollments. # Use this open command when you want to open it after full enrollment, e.g. at mount time or in standard enrollments.
cryptsetupOpen = createOpenCommand { cryptsetupOpen = ''
inherit keyFileArgs; ${lib.optionalString config.askPassword ''
tokenType = if config.enrollFido2 then "systemd-fido2" else null; if [ -z "''${password:-}" ]; then
}; if [ -z ''${IN_DISKO_TEST+x} ]; then
set +x
echo "Enter password for ${config.device}"
IFS= read -r -s password
export password
set -x
else
export password=disko
fi
fi
''}
${createOpenCommand {
inherit keyFileArgs;
tokenType = if config.enrollFido2 then "systemd-fido2" else null;
}}
'';
# Use this open command when you want to open it immediately after the formatting and before the stage 2 process is finished (i.e. the wipe slot). # Use this open command when you want to open it immediately after the formatting and before the stage 2 process is finished (i.e. the wipe slot).
formatCryptsetupOpen = createOpenCommand { formatCryptsetupOpen = createOpenCommand {
@@ -227,53 +242,63 @@ in
export SLOT_ZERO_TO_DELETE=true export SLOT_ZERO_TO_DELETE=true
''} ''}
cryptsetup -q luksFormat "${config.device}" ${toString config.extraFormatArgs} ${formatKeyFileArgs} cryptsetup -q luksFormat "${config.device}" ${toString config.extraFormatArgs} ${formatKeyFileArgs}
fi
if ! cryptsetup status "${config.name}" >/dev/null; then
${formatCryptsetupOpen} \ ${formatCryptsetupOpen} \
--persistent --persistent
fi fi
# Unlock without relying on the autogenerated password when someone
# calls this script after unmounting or rebooting
if ! cryptsetup status "${config.name}" >/dev/null; then
${cryptsetupOpen}
fi
${toString ( ${toString (
lib.forEach config.additionalKeyFiles (keyFile: '' lib.forEach config.additionalKeyFiles (keyFile: ''
cryptsetup luksAddKey "${config.device}" ${keyFile} ${formatKeyFileArgs} if ! cryptsetup open --test-passphrase --key-file=${keyFile} "${config.device}" 2>/dev/null; then
cryptsetup luksAddKey "${config.device}" ${keyFile} ${formatKeyFileArgs}
fi
'') '')
)} )}
${lib.optionalString config.enrollRecovery '' ${lib.optionalString config.enrollRecovery ''
systemd-cryptenroll \ if ! systemd-cryptenroll "${config.device}" 2>/dev/null | grep -qw recovery; then
--recovery-key \ systemd-cryptenroll \
--unlock-key-file=${formatKeyFile} \ --recovery-key \
"${config.device}" --unlock-key-file=${formatKeyFile} \
if [ -z ''${IN_DISKO_TEST+x} ]; then "${config.device}"
set +x; read -p "Press Enter when you scanned the QR code offscreen or that the recovery key is stored securely."; set -x if [ -z ''${IN_DISKO_TEST+x} ]; then
set +x; read -p "Press Enter when you scanned the QR code offscreen or that the recovery key is stored securely."; set -x
fi
fi fi
''} ''}
${lib.optionalString config.enrollFido2 '' ${lib.optionalString config.enrollFido2 ''
wait_for_token() { if ! systemd-cryptenroll "${config.device}" 2>/dev/null | grep -qw fido2; then
set +f wait_for_token() {
echo "Waiting for FIDO2 token insertion..." set +f
echo "Waiting for FIDO2 token insertion..."
# Check if any FIDO2 device is available via /dev/hidraw* # Check if any FIDO2 device is available via /dev/hidraw*
while true; do while true; do
if ls /dev/hidraw* &>/dev/null; then if ls /dev/hidraw* &>/dev/null; then
echo "FIDO2 device detected." echo "FIDO2 device detected."
break break
else else
echo "FIDO2 device not detected, waiting..." echo "FIDO2 device not detected, waiting..."
sleep 2 sleep 2
fi fi
done done
set -f set -f
} }
wait_for_token wait_for_token
systemd-cryptenroll \ systemd-cryptenroll \
--fido2-device=auto \ --fido2-device=auto \
''${SLOT_ZERO_TO_DELETE:+--wipe-slot=0} \ ''${SLOT_ZERO_TO_DELETE:+--wipe-slot=0} \
--unlock-key-file=${formatKeyFile} \ --unlock-key-file=${formatKeyFile} \
${toString config.extraFido2EnrollArgs} \ ${toString config.extraFido2EnrollArgs} \
"${config.device}" "${config.device}"
fi
''} ''}
${lib.optionalString (config.content != null) config.content._create} ${lib.optionalString (config.content != null) config.content._create}
''; '';
@@ -287,17 +312,6 @@ in
{ {
dev = '' dev = ''
if ! cryptsetup status "${config.name}" >/dev/null 2>/dev/null; then if ! cryptsetup status "${config.name}" >/dev/null 2>/dev/null; then
${lib.optionalString config.askPassword ''
if [ -z ''${IN_DISKO_TEST+x} ]; then
set +x
echo "Enter password for ${config.device}"
IFS= read -r -s password
export password
set -x
else
export password=disko
fi
''}
${cryptsetupOpen} ${cryptsetupOpen}
fi fi
${lib.optionalString (config.content != null) contentMount.dev or ""} ${lib.optionalString (config.content != null) contentMount.dev or ""}
+18
View File
@@ -0,0 +1,18 @@
{
pkgs ? import <nixpkgs> { },
diskoLib ? pkgs.callPackage ../lib { },
}:
diskoLib.testLib.makeDiskoTest {
inherit pkgs;
name = "luks-fido2";
disko-config = ../example/luks-fido2.nix;
enableCanokey = true;
extraTestScript = ''
machine.succeed("cryptsetup isLuks /dev/vda2");
machine.succeed("mountpoint /");
machine.succeed("systemd-cryptenroll /dev/vda2 | grep -qw fido2")
# Recovery should be disabled
machine.fail("systemd-cryptenroll /dev/vda2 | grep -qw recovery")
'';
}