Blame SOURCES/gpgverify

838cdc
#!/bin/bash
838cdc
838cdc
# Copyright 2018 B. Persson, Bjorn@Rombobeorn.se
838cdc
#
838cdc
# This material is provided as is, with absolutely no warranty expressed
838cdc
# or implied. Any use is at your own risk.
838cdc
#
838cdc
# Permission is hereby granted to use or copy this shellscript
838cdc
# for any purpose, provided the above notices are retained on all copies.
838cdc
# Permission to modify the code and to distribute modified code is granted,
838cdc
# provided the above notices are retained, and a notice that the code was
838cdc
# modified is included with the above copyright notice.
838cdc
838cdc
838cdc
function print_help {
838cdc
    cat <<'EOF'
838cdc
Usage: gpgverify --keyring=<pathname> --signature=<pathname> --data=<pathname>
838cdc
838cdc
gpgverify is a wrapper around gpgv designed for easy and safe scripting. It
838cdc
verifies a file against a detached OpenPGP signature and a keyring. The keyring
838cdc
shall contain all the keys that are trusted to certify the authenticity of the
838cdc
file, and must not contain any untrusted keys.
838cdc
838cdc
The differences, compared to invoking gpgv directly, are that gpgverify accepts
838cdc
the keyring in either ASCII-armored or unarmored form, and that it will not
838cdc
accidentally use a default keyring in addition to the specified one.
838cdc
838cdc
Parameters:
838cdc
  --keyring=<pathname>    keyring with all the trusted keys and no others
838cdc
  --signature=<pathname>  detached signature to verify
838cdc
  --data=<pathname>       file to verify against the signature
838cdc
EOF
838cdc
}
838cdc
838cdc
838cdc
fatal_error() {
838cdc
    message="$1"  # an error message
838cdc
    status=$2     # a number to use as the exit code
838cdc
    echo "gpgverify: $message" >&2
838cdc
    exit $status
838cdc
}
838cdc
838cdc
838cdc
require_parameter() {
838cdc
    term="$1"   # a term for a required parameter
838cdc
    value="$2"  # Complain and terminate if this value is empty.
838cdc
    if test -z "${value}" ; then
838cdc
        fatal_error "No ${term} was provided." 2
838cdc
    fi
838cdc
}
838cdc
838cdc
838cdc
check_status() {
838cdc
    action="$1"  # a string that describes the action that was attempted
838cdc
    status=$2    # the exit code of the command
838cdc
    if test $status -ne 0 ; then
838cdc
        fatal_error "$action failed." $status
838cdc
    fi
838cdc
}
838cdc
838cdc
838cdc
# Parse the command line.
838cdc
keyring=
838cdc
signature=
838cdc
data=
838cdc
for parameter in "$@" ; do
838cdc
    case "${parameter}" in
838cdc
        (--help)
838cdc
            print_help
838cdc
            exit
838cdc
            ;;
838cdc
        (--keyring=*)
838cdc
            keyring="${parameter#*=}"
838cdc
            ;;
838cdc
        (--signature=*)
838cdc
            signature="${parameter#*=}"
838cdc
            ;;
838cdc
        (--data=*)
838cdc
            data="${parameter#*=}"
838cdc
            ;;
838cdc
        (*)
838cdc
            fatal_error "Unknown parameter: \"${parameter}\"" 2
838cdc
            ;;
838cdc
    esac
838cdc
done
838cdc
require_parameter 'keyring' "${keyring}"
838cdc
require_parameter 'signature' "${signature}"
838cdc
require_parameter 'data file' "${data}"
838cdc
838cdc
# Make a temporary working directory.
838cdc
workdir="$(mktemp --directory)"
838cdc
check_status 'Making a temporary directory' $?
838cdc
workring="${workdir}/keyring.gpg"
838cdc
838cdc
# Decode any ASCII armor on the keyring. This is harmless if the keyring isn't
838cdc
# ASCII-armored.
838cdc
gpg2 --homedir="${workdir}" --yes --output="${workring}" --dearmor "${keyring}"
838cdc
check_status 'Decoding the keyring' $?
838cdc
838cdc
# Verify the signature using the decoded keyring.
838cdc
gpgv2 --homedir="${workdir}" --keyring="${workring}" "${signature}" "${data}"
838cdc
check_status 'Signature verification' $?
838cdc
838cdc
# (--homedir isn't actually necessary. --dearmor processes only the input file,
838cdc
# and if --keyring is used and contains a slash, then gpgv2 uses only that
838cdc
# keyring. Thus neither command will look for a default keyring, but --homedir
838cdc
# makes extra double sure that no default keyring will be touched in case
838cdc
# another version of GPG works differently.)
838cdc
838cdc
# Clean up. (This is not done in case of an error that may need inspection.)
838cdc
rm --recursive --force ${workdir}