-
Notifications
You must be signed in to change notification settings - Fork 13
/
sdbootutil-enroll
executable file
·111 lines (92 loc) · 2.87 KB
/
sdbootutil-enroll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
get_credential() {
local var="${1:?}"
local name="${2:?}"
local keyid
keyid="$(keyctl id %user:"$name" 2> /dev/null)" || true
if [ -e "$CREDENTIALS_DIRECTORY/$name" ]; then
read -r "$var" < "$CREDENTIALS_DIRECTORY/$name"
elif [ -n "$keyid" ]; then
read -r "$var" <<<"$(keyctl pipe "$keyid")"
fi
}
have_luks2() {
lsblk --noheadings -o PATH,FSTYPE | grep -q crypto_LUKS
}
write_issue_file()
{
local recovery_key="$1"
if [ -e '/usr/sbin/issue-generator' ]; then
mkdir -p "/run/issue.d/"
issuefile="/run/issue.d/90-diskencrypt.conf"
else
issuefile='/dev/stdout'
fi
echo "$recovery_key" > "$issuefile"
issue-generator
}
[ ! -e "/var/lib/YaST2/reconfig_system" ] || exit 0
have_luks2 || exit 0
# disk-encryption-tool-dracut uses "cryptenroll" for the keyring
# parameter, if we use a different one we need to move it back to
# "cryptenroll", to do the enrollment via sdbootutil without
# requesting a password
enroll_keyid="$(keyctl id %user:cryptenroll 2> /dev/null)" || exit 0
[ -n "$enroll_keyid" ] || {
echo "Enrollment key not registered in the keyring. Aborting" > /dev/stderr
exit 1
}
# Proceed with the enrollment
rk=
get_credential rk "sdbootutil-enroll.rk"
pw=
get_credential pw "sdbootutil-enroll.pw"
tpm2_pin=
get_credential tpm2_pin "sdbootutil-enroll.tpm2+pin"
tpm2=
get_credential tpm2 "sdbootutil-enroll.tpm2"
fido2=
get_credential fido2 "sdbootutil-enroll.fido2"
[ -z "$rk" ] || {
echo "Enrolling recovery key"
# Note that if --no-reuse-initrd is used, then a new initrd
# will be created and will break the measurement of the
# initial components if later the TPM2 enrollment is called
extra=
if [ -z "$tpm2_pin" ] && [ -z "$tpm2" ] && [ -z "$fido2" ]; then
extra="--no-reuse-initrd"
fi
recovery_key="$(sdbootutil enroll --method=recovery-key "$extra")"
write_issue_file "$recovery_key"
}
[ -z "$pw" ] || {
echo "Enrolling password"
# Note that if --no-reuse-initrd is used, then a new initrd
# will be created and will break the measurement of the
# initial components if later the TPM2 enrollment is called
extra=
if [ -z "$tpm2_pin" ] && [ -z "$tpm2" ] && [ -z "$fido2" ]; then
extra="--no-reuse-initrd"
fi
PW="$pw" sdbootutil enroll --method=password "$extra"
}
if [ -n "$tpm2_pin" ]; then
echo "Enrolling TPM2 with PIN"
SDB_ADD_INITIAL_COMPONENT=1 PIN="$crypt_tpm_pin" sdbootutil enroll --method=tpm2+pin
elif [ -n "$tpm2" ]; then
echo "Enrolling TPM2"
SDB_ADD_INITIAL_COMPONENT=1 sdbootutil enroll --method=tpm2
fi
[ -z "$fido2" ] || {
echo "Enrolling a FIDO2 key"
sdbootutil enroll --method=fido2
}
# Clean the enrollment key. disk-encryption-tool creates it in the
# keyslot 0 with the name "enrollment-key", that is showed by
# systemd-cryptenroll as "other"
while read -r dev; do
slots=$(systemd-cryptenroll "$dev")
if grep -q "other" <<<"$slots"; then
systemd-cryptenroll --wipe-slot=0 "$dev"
fi
done < <(sdbootutil list-devices)