-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash-kernel-signer.sh
executable file
·553 lines (503 loc) · 18.9 KB
/
bash-kernel-signer.sh
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#!/usr/bin/env bash
##
#
# Copyright 2021 Deren Vural
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##
# Root privileges check
[[ $EUID -ne 0 ]] && echo "This script must be run as root." && exit 1
# Global error message var
ERROR_MSG=""
## Functions
function sign_kernel()
{
## Sign loop
stop="False"
prev_out=""
until [[ "$stop" == "True" ]]; do
tput reset
echo "=========BASH KERNEL SIGNING UTILITY=========="
# Search for kernels
mapfile -t kernels < <( find "$kernel_location" -name "vmlinuz-*" | sort -n )
unsigned_kernels=()
valid_signed_kernels=()
invalid_signed_kernels=()
valid_validity_checks=()
invalid_validity_checks=()
# For each detected kernel
for unvalidated_kernel in "${kernels[@]}"; do
# Validate kernel signatures
mapfile -t validity_check < <(sbverify --cert "$cert_location" "${unvalidated_kernel}" 2>&1)
# Increment signed/unsigned kernels
if [[ "${#validity_check[@]}" = 1 && "${validity_check[0]}" = "Signature verification OK" ]]; then
# Add to valid signed kernels
valid_signed_kernels+=("$unvalidated_kernel")
valid_validity_checks+=("${validity_check[0]}")
elif [[ "${#validity_check[@]}" = 1 && "${validity_check[0]}" = "Signature verification failed" ]]; then
# Add to invalid signed kernels
invalid_signed_kernels+=("$unvalidated_kernel")
invalid_validity_checks+=("${validity_check[0]}")
elif [[ "${#validity_check[@]}" = 2 && "${validity_check[0]}" = "No signature table present" ]]; then
# Add to unsiged kernels
unsigned_kernels+=("$unvalidated_kernel")
else
# SOME UNKNOWN ERROR?
echo "??error??"
fi
done
# Print all kernels
declare -i counter
echo " Number of kernels available for signing: ${#unsigned_kernels[@]}"
if [[ "${#unsigned_kernels[@]}" == 0 ]]; then
echo " -none-"
else
counter=0
for kernel in "${unsigned_kernels[@]}"; do
id=$(( "$counter" + 1 ))
echo " $id - $kernel"
(( counter++ ))
done
fi
echo " Number of signed kernels: ${#valid_signed_kernels[@]}"
if [[ "${#valid_signed_kernels[@]}" == 0 ]]; then
echo " -none-"
else
counter=0
for kernel in "${valid_signed_kernels[@]}"; do
echo " $kernel"
echo " -> ${valid_validity_checks[$counter]}"
(( counter++ ))
done
fi
echo " Number of invalid signed kernels: ${#invalid_signed_kernels[@]}"
if [[ "${#invalid_signed_kernels[@]}" == 0 ]]; then
echo " -none-"
else
counter=0
for kernel in "${invalid_signed_kernels[@]}"; do
echo " $kernel"
echo " -> ${invalid_validity_checks[$counter]}"
(( counter++ ))
done
fi
echo "=============================================="
echo "$prev_out"
echo "=============================================="
echo "0 - Exit"
read -p "Which kernel would you like to sign?:" -r user_input
if [[ "$user_input" == "0" ]]; then
ERROR_MSG="cancelled.."
return 1
elif [[ "$user_input" =~ ^[0-9]+$ ]] && test "$user_input" -le "${#unsigned_kernels[@]}"; then
# Sign kernel
selection=$(( user_input - 1 ))
datetime=$(date +"%Y-%m-%d+%T")
sbsign --key "$key_location" --cert "$cert_location" --output "${unsigned_kernels[$selection]}.signed$datetime" "${unsigned_kernels[$selection]}"
prev_out="$?"
else
prev_out="invalid input.."
fi
done
return 0
}
function purge_kernel()
{
## Purge loop
stop="False"
prev_out=""
until [[ "$stop" == "True" ]]; do
tput reset
echo "=========BASH KERNEL SIGNING UTILITY=========="
# Search for kernels
mapfile -t kernels < <( find "$kernel_location" -name "vmlinuz-*" | sort -n )
# Only verify keys if keys exist
if [[ "$valid_keys" == "True" ]]; then
unsigned_kernels=()
valid_signed_kernels=()
invalid_signed_kernels=()
valid_validity_checks=()
invalid_validity_checks=()
# For each detected kernel
for unvalidated_kernel in "${kernels[@]}"; do
# Validate kernel signatures
mapfile -t validity_check < <(sbverify --cert "$cert_location" "${unvalidated_kernel}" 2>&1)
# Increment signed/unsigned kernels
if [[ "${#validity_check[@]}" = 1 && "${validity_check[0]}" = "Signature verification OK" ]]; then
# Add to valid signed kernels
valid_signed_kernels+=("$unvalidated_kernel")
valid_validity_checks+=("${validity_check[0]}")
elif [[ "${#validity_check[@]}" = 1 && "${validity_check[0]}" = "Signature verification failed" ]]; then
# Add to invalid signed kernels
invalid_signed_kernels+=("$unvalidated_kernel")
invalid_validity_checks+=("${validity_check[0]}")
elif [[ "${#validity_check[@]}" = 2 && "${validity_check[0]}" = "No signature table present" ]]; then
# Add to unsinged kernels
unsigned_kernels+=("$unvalidated_kernel")
else
# SOME UNKNOWN ERROR?
echo "??error??"
fi
done
# Print all kernels
declare -i counter
echo " Number of kernels available for signing: ${#unsigned_kernels[@]}"
if [[ "${#unsigned_kernels[@]}" == 0 ]]; then
echo " -none-"
else
for kernel in "${unsigned_kernels[@]}"; do
echo " $kernel"
done
fi
echo " Number of signed kernels: ${#valid_signed_kernels[@]}"
if [[ "${#valid_signed_kernels[@]}" == 0 ]]; then
echo " -none-"
else
counter=0
for kernel in "${valid_signed_kernels[@]}"; do
id=$(( "$counter" + 1 ))
echo " $id - $kernel"
echo " -> ${valid_validity_checks[$counter]}"
(( counter++ ))
done
fi
echo " Number of invalid signed kernels: ${#invalid_signed_kernels[@]}"
if [[ "${#invalid_signed_kernels[@]}" == 0 ]]; then
echo " -none-"
else
counter=0
for kernel in "${invalid_signed_kernels[@]}"; do
echo " $kernel"
echo " -> ${invalid_validity_checks[$counter]}"
(( counter++ ))
done
fi
else
echo " Kernels Present: ${#kernels[@]}"
for kernel in "${kernels[@]}"; do
echo " $kernel"
done
echo "Signature Database key and/or certificate not detected.."
fi
echo "=============================================="
echo "$prev_out"
echo "=============================================="
echo "0 - Exit"
read -p "Which signed kernel would you like to purge?:" -r user_input
if [[ "$user_input" == "0" ]]; then
ERROR_MSG="cancelled.."
return 1
elif [[ ! "$valid_keys" == "True" ]]; then
prev_out="missing/invalid keys, cannot check kernels.."
elif [[ "$user_input" =~ ^[0-9]+$ ]] && test "$user_input" -le "${#valid_signed_kernels[@]}"; then
# Purge signed kernel
selection=$(( user_input - 1 ))
sudo rm -f "${valid_signed_kernels[$selection]}"
prev_out="$?"
else
prev_out="invalid input.."
fi
done
return 0
}
function create_keys()
{
# Get user input
tput reset
read -p "Please specify (existing) directory for new keys & certificates (0 to cancel):" -r user_input
# Validate folder exists
if [[ "$user_input" == "0" ]]; then
ERROR_MSG="cancelled.."
return 1
elif [[ $(stat -c "%a" "$user_input") == "700" && -w "$user_input" && -r "$user_input" ]]; then
# Read old keys
echo "reading old keys..."
efi-readvar -v PK -o "${user_input}/old_PK.esl"
efi-readvar -v KEK -o "${user_input}/old_KEK.esl"
efi-readvar -v db -o "${user_input}/old_db.esl"
efi-readvar -v dbx -o "${user_input}/old_dbx.esl"
# (continue)
read -n 1 -s -r -p "Old keys successfully read into files, press any key to continue.."
# Generate keys and certificates
echo -e "\ngenerating keys & certificates..."
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=new platform key/" -keyout "${user_input}/new_PK.key" -out "${user_input}/new_PK.crt" -days 3650 -nodes -sha256
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=new key exchange key/" -keyout "${user_input}/new_KEK.key" -out "${user_input}/new_KEK.crt" -days 3650 -nodes -sha256
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=new kernel signing key/" -keyout "${user_input}/new_db.key" -out "${user_input}/new_db.crt" -days 3650 -nodes -sha256
# Change permissions to read-only for root (precaution)
sudo chmod -v 400 "${user_input}/new_PK.key"
sudo chmod -v 400 "${user_input}/new_KEK.key"
sudo chmod -v 400 "${user_input}/new_db.key"
# (continue)
read -n 1 -s -r -p "Keys successfully generated, press any key to continue.."
# Create update files
echo -e "\ncreating update files for keystore.."
# PK
cert-to-efi-sig-list -g "$(uuidgen)" "${user_input}/new_PK.crt" "${user_input}/new_PK.esl"
sign-efi-sig-list -k "${user_input}/new_PK.key" -c "${user_input}/new_PK.crt" PK "${user_input}/new_PK.esl" "${user_input}/new_PK.auth"
# KEK
cert-to-efi-sig-list -g "$(uuidgen)" "${user_input}/new_KEK.crt" "${user_input}/new_KEK.esl"
sign-efi-sig-list -a -k "${user_input}/new_PK.key" -c "${user_input}/new_PK.crt" KEK "${user_input}/new_KEK.esl" "${user_input}/new_KEK.auth"
# db
cert-to-efi-sig-list -g "$(uuidgen)" "${user_input}/new_db.crt" "${user_input}/new_db.esl"
sign-efi-sig-list -a -k "${user_input}/new_KEK.key" -c "${user_input}/new_KEK.crt" db "${user_input}/new_db.esl" "${user_input}/new_db.auth"
# dbx
sign-efi-sig-list -k "${user_input}/new_KEK.key" -c "${user_input}/new_KEK.crt" dbx "${user_input}/old_dbx.esl" "${user_input}/old_dbx.auth"
# (continue)
read -n 1 -s -r -p "Update files successfully generated, press any key to continue.."
# Create DER (Distinguished Encoding Rules) files, needed for some BIOSes
openssl x509 -outform DER -in "${user_input}/new_PK.crt" -out "${user_input}/new_PK.cer"
openssl x509 -outform DER -in "${user_input}/new_KEK.crt" -out "${user_input}/new_KEK.cer"
openssl x509 -outform DER -in "${user_input}/new_db.crt" -out "${user_input}/new_db.cer"
# (continue)
echo -e "\n"
read -n 1 -s -r -p "DER versions successfully generated, press any key to continue"
echo -e "\n"
# Create compound esl files & auth counterparts
cat "${user_input}/old_KEK.esl" "${user_input}/new_KEK.esl" > "${user_input}/compound_KEK.esl"
cat "${user_input}/old_db.esl" "${user_input}/new_db.esl" > "${user_input}/compound_db.esl"
sign-efi-sig-list -k "${user_input}/new_PK.key" -c "${user_input}/new_PK.crt" KEK "${user_input}/compound_KEK.esl" "${user_input}/compound_KEK.auth"
sign-efi-sig-list -k "${user_input}/new_KEK.key" -c "${user_input}/new_KEK.crt" db "${user_input}/compound_db.esl" "${user_input}/compound_db.auth"
# (continue)
echo -e "\nNew esl & auth files successfully generated!"
# Set the new locations in config file
echo "Adding ${user_input}/new_db.key and ${user_input}/new_db.crt to config file!"
sed_location="${user_input//\//\\/}" #replace all '/' with '\/'
sed_command_key='1s/.*/key_location="'
sed_command_key=$sed_command_key"${sed_location}"
sed_command_key=$sed_command_key'\/new_db.key"/'
sed_command_cert='2s/.*/cert_location="'
sed_command_cert=$sed_command_cert"${sed_location}"
sed_command_cert=$sed_command_cert'\/new_db.crt"/'
sed -i "${sed_command_key}" "$SCRIPT_DIR/keylocations.cfg"
sed -i "${sed_command_cert}" "$SCRIPT_DIR/keylocations.cfg"
echo "Added ${user_input}/new_db.key and ${user_input}/new_db.crt to config file!"
# Give user links for adding to keystore
echo "See Sakaki's guide (https://wiki.gentoo.org/wiki/User:Sakaki/Sakaki's_EFI_Install_Guide/Configuring_Secure_Boot#Installing_New_Keys_into_the_Keystore) on how to update your keystore!"
read -n 1 -s -r -p "(press any key to continue)"
else
ERROR_MSG="invalid directory, please exit and create new directory (check permissions!).."
return 1
fi
return 0
}
## Check for signing keys
# Check if files specified in config
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
if [[ ! -f "$SCRIPT_DIR/keylocations.cfg" ]];then
# Otherwise print error
echo "missing config file.."
# Create new config file
echo "creating new config file.."
touch "$SCRIPT_DIR/keylocations.cfg"
if [[ ! -f "$SCRIPT_DIR/keylocations.cfg" ]];then
echo "creation failed! unknown error.."
exit 1
fi
# Update ownership
USER="$(who | awk '{print $1;}')"
chown "$USER:$USER" "$SCRIPT_DIR/keylocations.cfg"
# Add variables
echo 'key_location=""' >> "$SCRIPT_DIR/keylocations.cfg"
echo 'cert_location=""' >> "$SCRIPT_DIR/keylocations.cfg"
echo 'kernel_location=""' >> "$SCRIPT_DIR/keylocations.cfg"
# Warn user
echo "!!PLEASE UPDATE CONFIG FILE WITH KERNEL LOCATION!!"
fi
# Import config file
source "$SCRIPT_DIR/keylocations.cfg"
# Check keys exist
if [[ -z "$key_location" || -z "$cert_location" ]]; then
# empty key & cert file locations in config
valid_keys="False"
else
# Key & cert files are specified
valid_keys="True"
# Check key & cert valid locations
if [[ ! -f $key_location || ! -f $cert_location ]]; then
# Otherwise print error
echo "invalid Signature Database key/certificate locations, check config file.."
exit 1
fi
fi
# Check valid locations
if [[ -z $kernel_location ]]; then
# Otherwise print error
echo "empty kernel location, check config file.."
exit 1
else
# Check files exist
if [[ ! -d $kernel_location ]]; then
# Otherwise print error
echo "missing kernel location, check config file.."
exit 1
fi
fi
## Main Loop
stop="False"
prev_out=""
while [[ "$stop" == "False" ]]; do
tput reset
echo "=========BASH KERNEL SIGNING UTILITY=========="
# Search for kernels
mapfile -t kernels < <( find "$kernel_location" -name "vmlinuz-*" | sort -n )
# Only verify keys if keys exist
if [[ "$valid_keys" == "True" ]]; then
unsigned_kernels=()
valid_signed_kernels=()
invalid_signed_kernels=()
valid_validity_checks=()
invalid_validity_checks=()
# For each detected kernel
for unvalidated_kernel in "${kernels[@]}"; do
# Validate kernel signatures
mapfile -t validity_check < <(sbverify --cert "$cert_location" "${unvalidated_kernel}" 2>&1)
# Increment signed/unsigned kernels
if [[ "${#validity_check[@]}" = 1 && "${validity_check[0]}" = "Signature verification OK" ]]; then
# Add to valid signed kernels
valid_signed_kernels+=("$unvalidated_kernel")
valid_validity_checks+=("${validity_check[0]}")
elif [[ "${#validity_check[@]}" = 1 && "${validity_check[0]}" = "Signature verification failed" ]]; then
# Add to invalid signed kernels
invalid_signed_kernels+=("$unvalidated_kernel")
invalid_validity_checks+=("${validity_check[0]}")
elif [[ "${#validity_check[@]}" = 2 && "${validity_check[0]}" = "No signature table present" ]]; then
# Add to unsiged kernels
unsigned_kernels+=("$unvalidated_kernel")
else
# SOME UNKNOWN ERROR?
echo "??error??"
fi
done
# Print all kernels
declare -i counter
echo " Number of kernels available for signing: ${#unsigned_kernels[@]}"
if [[ "${#unsigned_kernels[@]}" == 0 ]]; then
echo " -none-"
else
for kernel in "${unsigned_kernels[@]}"; do
echo " $kernel"
done
fi
echo " Number of signed kernels: ${#valid_signed_kernels[@]}"
if [[ "${#valid_signed_kernels[@]}" == 0 ]]; then
echo " -none-"
else
counter=0
for kernel in "${valid_signed_kernels[@]}"; do
echo " $kernel"
echo " -> ${valid_validity_checks[$counter]}"
(( counter++ ))
done
fi
echo " Number of invalid signed kernels: ${#invalid_signed_kernels[@]}"
if [[ "${#invalid_signed_kernels[@]}" == 0 ]]; then
echo " -none-"
else
counter=0
for kernel in "${invalid_signed_kernels[@]}"; do
echo " $kernel"
echo " -> ${invalid_validity_checks[$counter]}"
(( counter++ ))
done
fi
echo "Signature Database key & certificate detected.."
else
echo " Kernels Present: ${#kernels[@]}"
for kernel in "${kernels[@]}"; do
echo " $kernel"
done
echo "Signature Database key and/or certificate not detected.."
fi
echo "=============================================="
echo "$prev_out"
echo "=============================================="
echo "1 - Sign a kernel"
echo "2 - Purge signed kernel"
echo "3 - Create new keys"
echo "4 - Install/Remove unsigned kernel"
echo "5 - Modify Grub"
echo "6 - Reboot"
echo "0 - Exit"
read -p "enter input:" -r user_input
if [[ "$user_input" == "1" ]]; then
if [[ "$valid_keys" == "True" ]]; then
# sign kernels
sign_kernel
if [[ $? == 0 ]]; then
prev_out="success!"
else
prev_out="failure: $ERROR_MSG"
fi
else
prev_out="create new keys and append to existing/default keys first!"
fi
elif [[ "$user_input" == "2" ]]; then
# purge kernels
purge_kernel
if [[ $? == 0 ]]; then
prev_out="success!"
else
prev_out="failure: $ERROR_MSG"
fi
elif [[ "$user_input" == "3" ]]; then
# create keys
create_keys
if [[ $? == 0 ]]; then
prev_out="success!"
# Import config file
source "$SCRIPT_DIR/keylocations.cfg"
# Key & cert files now exist
valid_keys="True"
else
prev_out="failure: $ERROR_MSG"
fi
elif [[ "$user_input" == "4" ]]; then
# check mainline present
command_exists="$(command -v mainline-gtk)"
if [[ -n "$command_exists" ]]; then
# redirect to mainline-gtk app
mainline-gtk
if [[ $? == 0 ]]; then
prev_out="success!"
else
prev_out="failure: $?"
fi
else
prev_out="mainline-gtk not present!"
fi
elif [[ "$user_input" == "5" ]]; then
# check grub-customizer present
command_exists="$(command -v grub-customizer)"
if [[ -n "$command_exists" ]]; then
# redirect to grub-customizer app
grub-customizer
if [[ $? == 0 ]]; then
prev_out="success!"
else
prev_out="failure: $?"
fi
else
prev_out="grub-customizer not present!"
fi
elif [[ "$user_input" == "6" ]]; then
reboot
elif [[ "$user_input" == "0" ]]; then
# exit
tput reset
stop="True"
echo "Goodbye!.."
else
prev_out="invalid input.."
fi
done