-
Notifications
You must be signed in to change notification settings - Fork 24
/
cryptboot-efikeys
executable file
·158 lines (132 loc) · 4.92 KB
/
cryptboot-efikeys
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
# Check if user is root
if [[ $UID -ne 0 ]]; then
echo "Permission denied (you must be root)"
exit 1
fi
# Default config
SRCDIR="$(dirname "$(readlink -f "$0")")"
EFI_KEYS_DIR="/boot/efikeys"
# Load config file
if [[ -f "/etc/cryptboot.conf" ]]; then
source "/etc/cryptboot.conf"
elif [[ -f "$SRCDIR/cryptboot.conf" ]]; then
source "$SRCDIR/cryptboot.conf"
fi
# Create UEFI Secure Boot keys directory (if it doesn't already exists)
mkdir -p "$EFI_KEYS_DIR"
# Main program
case "$1" in
create)
cd "$EFI_KEYS_DIR"
if [[ -f KEK.esl ]] || [[ -f db.esl ]] || [[ -f PK.auth ]]; then
echo "UEFI Secure Boot keys in '$EFI_KEYS_DIR' already exists!"
read -n1 -r -s -p "Do you want to delete them and generate new ones [y/N]? " key
echo
if [[ "$key" != "y" ]] && [[ "$key" != "Y" ]]; then
exit 1
fi
fi
echo "Creating new UEFI Secure Boot keys..."
read -p "Enter a Common Name: " NAME
echo "$NAME" > NAME
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=$NAME PK/" -keyout PK.key \
-out PK.crt -days 3650 -nodes -sha256
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=$NAME KEK/" -keyout KEK.key \
-out KEK.crt -days 3650 -nodes -sha256
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=$NAME db/" -keyout db.key \
-out db.crt -days 3650 -nodes -sha256
openssl x509 -in PK.crt -out PK.cer -outform DER
openssl x509 -in KEK.crt -out KEK.cer -outform DER
openssl x509 -in db.crt -out db.cer -outform DER
GUID="$(uuidgen --random)"
echo "$GUID" > GUID
cert-to-efi-sig-list -g $GUID PK.crt PK.esl
cert-to-efi-sig-list -g $GUID KEK.crt KEK.esl
cert-to-efi-sig-list -g $GUID db.crt db.esl
echo -n > PK_null.esl
sign-efi-sig-list -k PK.key -c PK.crt PK PK.esl PK.auth
sign-efi-sig-list -k PK.key -c PK.crt PK PK_null.esl PK_null.auth
chmod 0400 *.{key,auth}
sync
echo "New UEFI Secure Boot keys created in '$EFI_KEYS_DIR'"
;;
enroll)
cd "$EFI_KEYS_DIR"
if ! [[ -f KEK.esl ]] || ! [[ -f db.esl ]] || ! [[ -f PK.auth ]]; then
echo "There are no UEFI Secure Boot keys in '$EFI_KEYS_DIR'!"
echo "Please generate them first with '$0 create'"
exit 1
fi
echo "Enrolling UEFI Secure Boot KEK key..."
efi-updatevar -e -f KEK.esl KEK
echo "Enrolling UEFI Secure Boot db key..."
efi-updatevar -e -f db.esl db
echo "Enrolling UEFI Secure Boot PK key..."
efi-updatevar -f PK.auth PK
echo "UEFI Secure Boot keys in '$EFI_KEYS_DIR' enrolled to UEFI firmware."
echo "You should now sign your boot loader and reboot!"
;;
sign)
cd "$EFI_KEYS_DIR"
filename="$2"
if [[ -z "$filename" ]]; then
echo "You have to specify EFI boot image file for signing it with UEFI Secure Boot keys!"
exit 1
fi
if ! [[ -f db.key ]] || ! [[ -f db.crt ]]; then
echo "There are no UEFI Secure Boot keys in '$EFI_KEYS_DIR'!"
echo "Please generate them first with '$0 create' and then enroll them with '$0 enroll'"
exit 1
fi
echo "Signing file '$filename' with UEFI Secure Boot keys..."
sbsign --key db.key --cert db.crt --output "$filename" "$filename"
sync
;;
verify)
cd "$EFI_KEYS_DIR"
filename="$2"
if [[ -z "$filename" ]]; then
echo "You have to specify EFI boot image file to verify its signature!"
exit 1
fi
if ! [[ -f db.crt ]]; then
echo "There are no UEFI Secure Boot keys in '$EFI_KEYS_DIR'!"
echo "Please generate them first with '$0 create' and then enroll them with '$0 enroll'"
exit 1
fi
echo "List of all signatures in '$filename':"
sbverify --list "$filename"
echo
echo "Verifying signature with UEFI Secure Boot keys..."
sbverify --cert db.crt "$filename"
;;
list)
echo "All UEFI Secure Boot keys enrolled in your UEFI firmware:"
efi-readvar
;;
status)
echo -n "UEFI Secure Boot status: "
secureboot="$(od -An -t u1 --read-bytes=1 --skip-bytes=4 /sys/firmware/efi/efivars/SecureBoot-*)"
if [[ "$secureboot" -eq 1 ]]; then
echo "ACTIVE"
exit 0
else
echo "INACTIVE"
exit 1
fi
;;
*)
echo "Usage: $(basename "$0") {create,enroll,sign,verify,list} [file-to-sign-or-verify]"
echo
echo "Manage UEFI Secure Boot keys"
echo
echo "Commands:"
echo " create Generate new UEFI Secure Boot keys"
echo " enroll Enroll new UEFI Secure Boot keys to your UEFI firmware"
echo " (you have to clear old keys in your UEFI firmware setup utility first)"
echo " sign Sign EFI boot image file with your UEFI Secure Boot keys"
echo " verify Verify signature of EFI boot image file with your UEFI Secure Boot keys"
echo " list List all UEFI Secure Boot keys enrolled in your UEFI firmware"
echo " status Check if UEFI Secure Boot is active or inactive"
esac