2024-01-17 11:23:23 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# script to check OpenPGP Application features
|
|
|
|
#
|
|
|
|
|
|
|
|
exeName=$(readlink "$0")
|
|
|
|
[[ -z ${exeName} ]] && exeName=$0
|
|
|
|
dirName=$(dirname "${exeName}")
|
|
|
|
|
|
|
|
gnupg_home_dir="$(realpath "${dirName}/gnupg")"
|
|
|
|
|
|
|
|
VERBOSE=false
|
2024-02-29 15:55:40 +00:00
|
|
|
EXPERT=false
|
2024-01-17 11:23:23 +00:00
|
|
|
|
|
|
|
#===============================================================================
|
|
|
|
#
|
|
|
|
# help - Prints script help and usage
|
|
|
|
#
|
|
|
|
#===============================================================================
|
|
|
|
# shellcheck disable=SC2154 # var is referenced but not assigned
|
|
|
|
help() {
|
|
|
|
echo
|
|
|
|
echo "Usage: ${exeName} <options>"
|
|
|
|
echo
|
|
|
|
echo "Options:"
|
|
|
|
echo
|
2024-02-29 15:55:40 +00:00
|
|
|
echo " -c <init|reset|card|encrypt|decryptsign|verify> : Requested command"
|
|
|
|
echo " -e : Expert mode mode"
|
2024-01-17 11:23:23 +00:00
|
|
|
echo " -v : Verbose mode"
|
|
|
|
echo " -h : Displays this help"
|
|
|
|
echo
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
#===============================================================================
|
|
|
|
#
|
2024-02-29 15:55:40 +00:00
|
|
|
# reset - Kill running process, ensure clear next operation
|
2024-01-17 11:23:23 +00:00
|
|
|
#
|
|
|
|
#===============================================================================
|
2024-02-29 15:55:40 +00:00
|
|
|
reset() {
|
2024-01-17 11:23:23 +00:00
|
|
|
# Kill running process
|
|
|
|
killall scdaemon gpg-agent 2>/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
#===============================================================================
|
|
|
|
#
|
|
|
|
# init - Init the gnupg config, start from an empty keyring
|
|
|
|
#
|
|
|
|
#===============================================================================
|
|
|
|
init() {
|
2024-02-29 15:55:40 +00:00
|
|
|
reset
|
2024-01-17 11:23:23 +00:00
|
|
|
|
|
|
|
# Cleanup old gnupg home directory
|
|
|
|
dir=$(basename "${gnupg_home_dir}")
|
|
|
|
rm -fr "${dir}" foo.txt*
|
|
|
|
mkdir "${dir}"
|
|
|
|
chmod 700 "${dir}"
|
|
|
|
|
|
|
|
{
|
|
|
|
echo reader-port \"Ledger token\"
|
|
|
|
echo allow-admin
|
|
|
|
echo enable-pinpad-varlen
|
|
|
|
echo card-timeout 1
|
|
|
|
} > "${dir}/scdaemon.conf"
|
|
|
|
}
|
|
|
|
|
|
|
|
#===============================================================================
|
|
|
|
#
|
|
|
|
# card - Show/edit the card status and configuration
|
|
|
|
#
|
|
|
|
#===============================================================================
|
|
|
|
card() {
|
2024-02-29 15:55:40 +00:00
|
|
|
local expert_mode=""
|
2024-01-17 11:23:23 +00:00
|
|
|
|
2024-02-29 15:55:40 +00:00
|
|
|
[[ ${EXPERT} == true ]] && expert_mode="--expert"
|
|
|
|
|
|
|
|
gpg --homedir "${gnupg_home_dir}" ${expert_mode} --card-edit
|
2024-01-17 11:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#===============================================================================
|
|
|
|
#
|
|
|
|
# encrypt - Encrypt a clear file
|
|
|
|
#
|
|
|
|
#===============================================================================
|
|
|
|
encrypt() {
|
|
|
|
local recipient=""
|
2024-02-29 15:55:40 +00:00
|
|
|
local verbose_mode=""
|
|
|
|
reset
|
2024-01-17 11:23:23 +00:00
|
|
|
rm -fr foo*
|
|
|
|
echo CLEAR > foo.txt
|
|
|
|
|
|
|
|
[[ ${VERBOSE} == true ]] && verbose_mode="--verbose"
|
|
|
|
|
|
|
|
recipient=$(gpg --homedir "${gnupg_home_dir}" --card-status | grep "General key info" | awk '{print $NF}')
|
|
|
|
|
|
|
|
echo "Encrypt with recipient '${recipient}'"
|
|
|
|
|
|
|
|
gpg --homedir "${gnupg_home_dir}" ${verbose_mode} --encrypt --recipient "${recipient}" foo.txt
|
|
|
|
}
|
|
|
|
|
|
|
|
#===============================================================================
|
|
|
|
#
|
|
|
|
# decrypt - Decrypt a file and compare with original clear content
|
|
|
|
#
|
|
|
|
#===============================================================================
|
|
|
|
decrypt() {
|
2024-02-29 15:55:40 +00:00
|
|
|
local verbose_mode=""
|
2024-01-17 11:23:23 +00:00
|
|
|
|
2024-02-29 15:55:40 +00:00
|
|
|
reset
|
2024-01-17 11:23:23 +00:00
|
|
|
|
|
|
|
[[ ${VERBOSE} == true ]] && verbose_mode="--verbose"
|
|
|
|
|
|
|
|
gpg --homedir "${gnupg_home_dir}" ${verbose_mode} --decrypt foo.txt.gpg > foo_dec.txt
|
|
|
|
|
|
|
|
# Check with original clear file
|
|
|
|
diff foo.txt foo_dec.txt >/dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
|
|
|
echo "Success !"
|
|
|
|
else
|
|
|
|
echo "Decryption error!"
|
|
|
|
fi
|
|
|
|
rm -fr foo*
|
|
|
|
}
|
|
|
|
|
|
|
|
#===============================================================================
|
|
|
|
#
|
|
|
|
# sign - Sign a file
|
|
|
|
#
|
|
|
|
#===============================================================================
|
|
|
|
sign() {
|
2024-02-29 15:55:40 +00:00
|
|
|
local verbose_mode=""
|
2024-01-17 11:23:23 +00:00
|
|
|
|
2024-02-29 15:55:40 +00:00
|
|
|
reset
|
2024-01-17 11:23:23 +00:00
|
|
|
rm -fr foo*
|
|
|
|
echo CLEAR > foo.txt
|
|
|
|
|
|
|
|
[[ ${VERBOSE} == true ]] && verbose_mode="--verbose"
|
|
|
|
|
|
|
|
gpg --homedir "${gnupg_home_dir}" ${verbose_mode} --sign foo.txt
|
|
|
|
}
|
|
|
|
|
|
|
|
#===============================================================================
|
|
|
|
#
|
|
|
|
# verify - Verify a file signature
|
|
|
|
#
|
|
|
|
#===============================================================================
|
|
|
|
verify() {
|
2024-02-29 15:55:40 +00:00
|
|
|
local verbose_mode=""
|
2024-01-17 11:23:23 +00:00
|
|
|
|
2024-02-29 15:55:40 +00:00
|
|
|
reset
|
2024-01-17 11:23:23 +00:00
|
|
|
|
|
|
|
[[ ${VERBOSE} == true ]] && verbose_mode="--verbose"
|
|
|
|
|
|
|
|
gpg --homedir "${gnupg_home_dir}" ${verbose_mode} --verify foo.txt.gpg
|
|
|
|
rm -fr foo*
|
|
|
|
}
|
|
|
|
|
|
|
|
#===============================================================================
|
|
|
|
#
|
|
|
|
# Parsing parameters
|
|
|
|
#
|
|
|
|
#===============================================================================
|
|
|
|
|
|
|
|
if (($# < 1)); then
|
|
|
|
help
|
|
|
|
fi
|
|
|
|
|
2024-02-29 15:55:40 +00:00
|
|
|
while getopts ":c:evh" opt; do
|
2024-01-17 11:23:23 +00:00
|
|
|
case $opt in
|
|
|
|
|
|
|
|
c)
|
|
|
|
case ${OPTARG} in
|
2024-02-29 15:55:40 +00:00
|
|
|
init|reset|card|encrypt|decrypt|sign|verify)
|
2024-01-17 11:23:23 +00:00
|
|
|
CMD=${OPTARG}
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Wrong parameter '${OPTARG}'!"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
|
2024-02-29 15:55:40 +00:00
|
|
|
e) EXPERT=true ;;
|
2024-01-17 11:23:23 +00:00
|
|
|
v) VERBOSE=true ;;
|
|
|
|
h) help ;;
|
|
|
|
|
|
|
|
\?) echo "Unknown option: -${OPTARG}" >&2; exit 1;;
|
|
|
|
: ) echo "Missing option argument for -${OPTARG}" >&2; exit 1;;
|
|
|
|
* ) echo "Unimplemented option: -${OPTARG}" >&2; exit 1;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
#===============================================================================
|
|
|
|
#
|
|
|
|
# Main
|
|
|
|
#
|
|
|
|
#===============================================================================
|
|
|
|
|
|
|
|
# execute the command
|
|
|
|
${CMD}
|