Skip to content

Commit 486b52f

Browse files
committed
WiP initrd/bin/oem-factory-reset: add qrcode+secet output loop until user press y (end of reownership wizard secret output)
Signed-off-by: Thierry Laurion <insurgo@riseup.net> works: - oem and user mode passphrase generation - qrcode missing: - unattended - luks reencryption + passphrase change for OEM mode (only input to be provided) with SINGLE passphrase when in unattended mode - same for user reownership when previously OEM reset unattended Signed-off-by: Thierry Laurion <insurgo@riseup.net>
1 parent 7df501b commit 486b52f

2 files changed

Lines changed: 27 additions & 20 deletions

File tree

initrd/bin/oem-factory-reset

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ CANCEL="--no-button Cancel"
2323
HEIGHT="0"
2424
WIDTH="80"
2525

26+
# Default values
2627
USER_PIN_DEF=123456
2728
ADMIN_PIN_DEF=12345678
2829
TPM_PASS_DEF=12345678
29-
USER_PIN=""
30-
ADMIN_PIN=""
31-
TPM_PASS=""
3230
GPG_GEN_KEY_IN_MEMORY="n"
3331
GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD="n"
3432

@@ -50,11 +48,16 @@ handle_mode() {
5048
case $mode in
5149
oem)
5250
DEBUG "OEM mode selected"
53-
# Add OEM mode specific logic here
51+
CUSTOM_SINGLE_PASS=$(generate_passphrase --number_words 2 --max_length $MAX_HOTP_GPG_PIN_LENGTH)
52+
USER_PIN=$CUSTOM_SINGLE_PASS
53+
ADMIN_PIN=$CUSTOM_SINGLE_PASS
54+
TPM_PASS=$CUSTOM_SINGLE_PASS
5455
;;
5556
user)
5657
DEBUG "User mode selected"
57-
# Add User mode specific logic here
58+
USER_PIN=$(generate_passphrase --number_words 2 --max_length $MAX_HOTP_GPG_PIN_LENGTH)
59+
ADMIN_PIN=$(generate_passphrase --number_words 2 --max_length $MAX_HOTP_GPG_PIN_LENGTH)
60+
TPM_PASS=$ADMIN_PIN
5861
;;
5962
*)
6063
warn "Unknown mode: $mode"
@@ -81,6 +84,9 @@ done
8184
# Handle the --mode parameter if provided
8285
if [[ -n "$MODE" ]]; then
8386
handle_mode "$MODE"
87+
else
88+
# Default to User Re-Ownership mode
89+
handle_mode "user"
8490
fi
8591

8692
#Override RSA_KEY_LENGTH to 2048 bits for Canokey under qemu testing boards until canokey fixes
@@ -719,9 +725,10 @@ generate_checksums() {
719725
fi
720726

721727
DEBUG "Detach-signing boot files under kexec.sig: ${param_files}"
722-
if sha256sum $param_files 2>/dev/null | DO_WITH_DEBUG gpg \
728+
729+
if sha256sum $param_files 2>/dev/null | gpg \
723730
--pinentry-mode loopback \
724-
--passphrase "${USER_PIN}" \
731+
--passphrase-file <(echo -n "$USER_PIN") \
725732
--digest-algo SHA256 \
726733
--detach-sign \
727734
-a \
@@ -1371,14 +1378,18 @@ if [ "$GPG_GEN_KEY_IN_MEMORY" = "y" ]; then
13711378
passphrases+="GPG key material backup passphrase: ${ADMIN_PIN}\n"
13721379
fi
13731380

1374-
# Show qrcode of configured secrets and ask user to confirm scanning of and loop until confirmed with qrenc $passphrases
1381+
# Show configured secrets in whiptail and loop until user confirms qr code was scanned
13751382
while true; do
13761383
whiptail --msgbox "
13771384
$(echo -e "$passphrases" | fold -w $((WIDTH - 5)))" \
13781385
$HEIGHT $WIDTH --title "Configured secrets"
1379-
qrencode "$passphrases"
1386+
# strip the initial newline of passphrases
1387+
qr_code=$(echo -e "$passphrases" | sed '1s/^\n//')
1388+
#Tell user to scan the QR code containing all configured secrets
1389+
echo -e "\nScan the QR code below to save the secrets to a secure location"
1390+
qrenc "$qr_code"
13801391
# Prompt user to confirm scanning of qrcode on console prompt not whiptail: y/n
1381-
echo -e -n "Please confirm you have scanned the QR code above [y/N]: "
1392+
echo -e -n "Please confirm you have scanned the QR code above and/or written down the secrets? [y/N]: "
13821393
read -n 1 prompt_output
13831394
echo
13841395
if [ "$prompt_output" == "y" -o "$prompt_output" == "Y" ]; then

initrd/etc/functions

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ generate_passphrase() {
887887
local dictionary_file="$2"
888888
local word=""
889889

890-
word=$(grep "^$rolls" "$dictionary_file" | awk '{print $2}')
890+
word=$(grep "^$rolls" "$dictionary_file" | awk -F ' ' '{print $2}')
891891
echo "$word"
892892
}
893893

@@ -898,17 +898,14 @@ generate_passphrase() {
898898
local rolls=""
899899
local random_bytes
900900

901-
# Read num_rolls bytes from /dev/urandom in one go
902-
random_bytes=$(dd if=/dev/urandom bs=1 count="$num_rolls" 2>/dev/null | hexdump -e '1/1 "%u\n"')
901+
# Read num_rolls bytes from /dev/random, fed by CPU RRAND in one go
902+
random_bytes=$(dd if=/dev/random bs=1 count="$num_rolls" 2>/dev/null | hexdump -e '1/1 "%u\n"')
903903

904904
# Process each byte to generate a dice roll
905905
while read -r byte; do
906906
roll=$((byte % 6 + 1))
907-
DEBUG "Randomized dice roll: $roll"
908907
rolls+=$roll
909908
done <<<"$random_bytes"
910-
911-
DEBUG "Generated dice rolls: $rolls"
912909
echo "$rolls"
913910
}
914911

@@ -978,15 +975,12 @@ generate_passphrase() {
978975
exit 1
979976
fi
980977

981-
digits=${#key}
982-
DEBUG "Number of digits in dice rolls: $digits"
978+
digits=${#key} #Number of digits in dice rolls
983979

984980
for ((i = 0; i < num_words; ++i)); do
985981
key=$(generate_dice_rolls "$digits")
986982
word=$(get_word_from_dictionary "$key" "$dictionary_file")
987-
DEBUG "Retrieved word: $word"
988983
if [[ "$lowercase" == "false" ]]; then
989-
DEBUG "Capitalizing the first letter of the word"
990984
word=${word^} # Capitalize the first letter
991985
fi
992986
passphrase+="$word "
@@ -997,6 +991,8 @@ generate_passphrase() {
997991
fi
998992
done
999993

994+
#Remove passphrase trailing space from passphrase+="$word"
995+
passphrase=${passphrase% }
1000996
echo "$passphrase"
1001997
return 0
1002998
}

0 commit comments

Comments
 (0)