git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/luksroot: GPG Smartcard support for luks encrypted volumes
commit147621f7db67344b781c6626b88ff6d0bad7aa70 authorMarkus Schmidl <markus.schmidl@ymail.com> dateThu 9 May 2019, 20:15 (8 years ago) parent3e9184b
1 file changed+171−4
nixos/modules/system/boot/luksroot.nix+171−4
nixos/modules/system/boot/luksroot.nix+171−4
@@ -76,6 +76,33 @@ let
7676 fi
7777 return 0
7878 }
79+
80+ wait_gpgcard() {
81+ local secs="''${1:-10}"
82+
83+ gpg --card-status > /dev/null 2> /dev/null
84+ if [ $? != 0 ]; then
85+ echo -n "Waiting $secs seconds for GPG Card to appear"
86+ local success=false
87+ for try in $(seq $secs); do
88+ echo -n .
89+ sleep 1
90+ gpg --card-status > /dev/null 2> /dev/null
91+ if [ $? == 0 ]; then
92+ success=true
93+ break
94+ fi
95+ done
96+ if [ $success == true ]; then
97+ echo " - success";
98+ return 0
99+ else
100+ echo " - failure";
101+ return 1
102+ fi
103+ fi
104+ return 0
105+ }
79106 '';
80107
81108 preCommands = ''
@@ -93,6 +120,13 @@ let
93120 # For Yubikey salt storage
94121 mkdir -p /crypt-storage
95122
123+ ${optionalString luks.gpgSupport ''
124+ export GPG_TTY=$(tty)
125+ export GNUPGHOME=/crypt-ramfs/.gnupg
126+
127+ gpg-agent --daemon --scdaemon-program $out/bin/scdaemon > /dev/null 2> /dev/null
128+ ''}
129+
96130 # Disable all input echo for the whole stage. We could use read -s
97131 # instead but that would ocasionally leak characters between read
98132 # invocations.
@@ -105,7 +139,7 @@ let
105139 umount /crypt-ramfs 2>/dev/null
106140 '';
107141
108− openCommand = name': { name, device, header, keyFile, keyFileSize, keyFileOffset, allowDiscards, yubikey, fallbackToPassword, ... }: assert name' == name;
142+ openCommand = name': { name, device, header, keyFile, keyFileSize, keyFileOffset, allowDiscards, yubikey, gpgCard, fallbackToPassword, ... }: assert name' == name;
109143 let
110144 csopen = "cryptsetup luksOpen ${device} ${name} ${optionalString allowDiscards "--allow-discards"} ${optionalString (header != null) "--header=${header}"}";
111145 cschange = "cryptsetup luksChangeKey ${device} ${optionalString (header != null) "--header=${header}"}";
@@ -182,7 +216,7 @@ let
182216 ''}
183217 }
184218
185− ${if luks.yubikeySupport && (yubikey != null) then ''
219+ ${optionalString (luks.yubikeySupport && (yubikey != null)) ''
186220 # Yubikey
187221 rbtohex() {
188222 ( od -An -vtx1 | tr -d ' \n' )
@@ -278,7 +312,7 @@ let
278312 umount /crypt-storage
279313 }
280314
281− open_yubikey() {
315+ open_with_hardware() {
282316 if wait_yubikey ${toString yubikey.gracePeriod}; then
283317 do_open_yubikey
284318 else
@@ -286,8 +320,75 @@ let
286320 open_normally
287321 fi
288322 }
323+ ''}
289324
290− open_yubikey
325+ ${optionalString (luks.gpgSupport && (gpgCard != null)) ''
326+
327+ do_open_gpg_card() {
328+ # Make all of these local to this function
329+ # to prevent their values being leaked
330+ local pin
331+ local opened
332+
333+ gpg --import /gpg-keys/${device}/pubkey.asc > /dev/null 2> /dev/null
334+
335+ gpg --card-status > /dev/null 2> /dev/null
336+
337+ for try in $(seq 3); do
338+ echo -n "PIN for GPG Card associated with device ${device}: "
339+ pin=
340+ while true; do
341+ if [ -e /crypt-ramfs/passphrase ]; then
342+ echo "reused"
343+ pin=$(cat /crypt-ramfs/passphrase)
344+ break
345+ else
346+ # and try reading it from /dev/console with a timeout
347+ IFS= read -t 1 -r pin
348+ if [ -n "$pin" ]; then
349+ ${if luks.reusePassphrases then ''
350+ # remember it for the next device
351+ echo -n "$pin" > /crypt-ramfs/passphrase
352+ '' else ''
353+ # Don't save it to ramfs. We are very paranoid
354+ ''}
355+ echo
356+ break
357+ fi
358+ fi
359+ done
360+ echo -n "Verifying passphrase for ${device}..."
361+ echo -n "$pin" | gpg -q --batch --passphrase-fd 0 --pinentry-mode loopback -d /gpg-keys/${device}/cryptkey.gpg 2> /dev/null | ${csopen} --key-file=- > /dev/null 2> /dev/null
362+ if [ $? == 0 ]; then
363+ echo " - success"
364+ ${if luks.reusePassphrases then ''
365+ # we don't rm here because we might reuse it for the next device
366+ '' else ''
367+ rm -f /crypt-ramfs/passphrase
368+ ''}
369+ break
370+ else
371+ echo " - failure"
372+ # ask for a different one
373+ rm -f /crypt-ramfs/passphrase
374+ fi
375+ done
376+
377+ [ "$opened" == false ] && die "Maximum authentication errors reached"
378+ }
379+
380+ open_with_hardware() {
381+ if wait_gpgcard ${toString gpgCard.gracePeriod}; then
382+ do_open_gpg_card
383+ else
384+ echo "No GPG Card found, falling back to normal open procedure"
385+ open_normally
386+ fi
387+ }
388+ ''}
389+
390+ ${if (luks.yubikeySupport && (yubikey != null)) || (luks.gpgSupport && (gpgCard != null)) then ''
391+ open_with_hardware
291392 '' else ''
292393 open_normally
293394 ''}
@@ -473,6 +574,36 @@ in
473574 '';
474575 };
475576
577+ gpgCard = mkOption {
578+ default = null;
579+ description = ''
580+ The option to use this LUKS device with a GPG encrypted luks password by the GPG Smartcard.
581+ If null (the default), GPG-Smartcard will be disabled for this device.
582+ '';
583+
584+ type = with types; nullOr (submodule {
585+ options = {
586+ gracePeriod = mkOption {
587+ default = 10;
588+ type = types.int;
589+ description = "Time in seconds to wait for the GPG Smartcard.";
590+ };
591+
592+ encryptedPass = mkOption {
593+ default = "";
594+ type = types.path;
595+ description = "Path to the GPG encrypted passphrase.";
596+ };
597+
598+ publicKey = mkOption {
599+ default = "";
600+ type = types.path;
601+ description = "Path to the Public Key.";
602+ };
603+ };
604+ });
605+ };
606+
476607 yubikey = mkOption {
477608 default = null;
478609 description = ''
@@ -554,6 +685,14 @@ in
554685 }));
555686 };
556687
688+ boot.initrd.luks.gpgSupport = mkOption {
689+ default = false;
690+ type = types.bool;
691+ description = ''
692+ Enables support for authenticating with a GPG encrypted password.
693+ '';
694+ };
695+
557696 boot.initrd.luks.yubikeySupport = mkOption {
558697 default = false;
559698 type = types.bool;
@@ -567,6 +706,12 @@ in
567706
568707 config = mkIf (luks.devices != {} || luks.forceLuksSupportInInitrd) {
569708
709+ assertions =
710+ [ { assertion = !(luks.gpgSupport && luks.yubikeySupport);
711+ message = "Yubikey and GPG Card may not be used at the same time.";
712+ }
713+ ];
714+
570715 # actually, sbp2 driver is the one enabling the DMA attack, but this needs to be tested
571716 boot.blacklistedKernelModules = optionals luks.mitigateDMAAttacks
572717 ["firewire_ohci" "firewire_core" "firewire_sbp2"];
@@ -603,6 +748,23 @@ in
603748 EOF
604749 chmod +x $out/bin/openssl-wrap
605750 ''}
751+
752+ ${optionalString luks.gpgSupport ''
753+ copy_bin_and_libs ${pkgs.gnupg}/bin/gpg
754+ copy_bin_and_libs ${pkgs.gnupg}/bin/gpg-agent
755+ copy_bin_and_libs ${pkgs.gnupg}/libexec/scdaemon
756+
757+ ${concatMapStringsSep "\n" (x:
758+ if x.gpgCard != null then
759+ ''
760+ mkdir -p $out/secrets/gpg-keys/${x.device}
761+ cp -a ${x.gpgCard.encryptedPass} $out/secrets/gpg-keys/${x.device}/cryptkey.gpg
762+ cp -a ${x.gpgCard.publicKey} $out/secrets/gpg-keys/${x.device}/pubkey.asc
763+ ''
764+ else ""
765+ ) (attrValues luks.devices)
766+ }
767+ ''}
606768 '';
607769
608770 boot.initrd.extraUtilsCommandsTest = ''
@@ -612,6 +774,11 @@ in
612774 $out/bin/ykinfo -V
613775 $out/bin/openssl-wrap version
614776 ''}
777+ ${optionalString luks.gpgSupport ''
778+ $out/bin/gpg --version
779+ $out/bin/gpg-agent --version
780+ $out/bin/scdaemon --version
781+ ''}
615782 '';
616783
617784 boot.initrd.preFailCommands = postCommands;