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
+56 -16

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
+29 -15
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 = ''
${lib.optionalString config.askPassword ''
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; inherit keyFileArgs;
tokenType = if config.enrollFido2 then "systemd-fido2" else null; 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,20 +242,27 @@ 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: ''
if ! cryptsetup open --test-passphrase --key-file=${keyFile} "${config.device}" 2>/dev/null; then
cryptsetup luksAddKey "${config.device}" ${keyFile} ${formatKeyFileArgs} cryptsetup luksAddKey "${config.device}" ${keyFile} ${formatKeyFileArgs}
fi
'') '')
)} )}
${lib.optionalString config.enrollRecovery '' ${lib.optionalString config.enrollRecovery ''
if ! systemd-cryptenroll "${config.device}" 2>/dev/null | grep -qw recovery; then
systemd-cryptenroll \ systemd-cryptenroll \
--recovery-key \ --recovery-key \
--unlock-key-file=${formatKeyFile} \ --unlock-key-file=${formatKeyFile} \
@@ -248,8 +270,10 @@ in
if [ -z ''${IN_DISKO_TEST+x} ]; then 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 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 ''
if ! systemd-cryptenroll "${config.device}" 2>/dev/null | grep -qw fido2; then
wait_for_token() { wait_for_token() {
set +f set +f
echo "Waiting for FIDO2 token insertion..." echo "Waiting for FIDO2 token insertion..."
@@ -274,6 +298,7 @@ in
--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")
'';
}