Doug Barton

PGP Challenge Message Script

You will want to download the script from the link below because the HTML version plays havoc with the < and > characters in it.

Link to the script file
#!/bin/sh

# Copyright (c) 2007-2014 Douglas Barton, dougb@dougbarton.us
# All rights reserved
# Please see detailed copyright below

#
# Change these values to something meaningful for you
#

# The token is the "magic cookie" used to make sure that the message
# recipient cannot fake the challenge response. It can be anything.
token=''
myemail=''
myname=''

#
# Optional
#

# If not set, will be discovered dynamically
#gpg=

# In case your key is on the keyring.
# Letters must be upper case, leave off the 0x, one per line.
#keyids_to_skip='1234ABCD
#87654321
#CDEF6789'
# OR
#keyids_to_skip='0A1B2C3D'

#
# Nothing below here needs to be changed, but improvements are always welcome
#

# Assumptions:
# You have a keyring file that contains the keys you want to sign
# OR
# You have a keyid or list of keyids to sign
#
# The UIDs on each key will contain a valid e-mail address
# You have a binary called 'sha256[sum]' that produces a useful hash
# You have a binary called 'gpg[2]' from GnuPG -- more recent is better
# You have a binary called 'sendmail' that takes the usual arguments
# You are running this in a directory where you have write permissions
# You are running this script on a "secure" system. That is, there are
# some shortcuts taken (like not using mktemp) that one would not take
# if running this script in a hostile environment. This may be improved
# in a future version.
#
# There is no need to import the keys before running the script
#
# The challenges list is always appended to (in case you are doing keys
# from more than one ring), so delete it first if you want a clean start.

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
export PATH

usage () { cat << EOF >&2

	Usage: ${0##*/} [-n] [-u ] [-y] |keyring.gpg

	-h	Display this help message.
	-n	Do not send e-mail, leave files locally for review
	-u	Key Id to sign messages with, if other than the default
	-y	Send messages for all uids of all keys

	Required input can be an individual key, or a keyring file.
	If the file is not located in \$GNUPGHOME (usually ~/.gnupg)
	specify the full path.

EOF
}

fail () { $echo "\nERROR: $*" >&2 ; usage ; exit 1 ; }

[ -z "$token" -o -z "$myemail" -o -z "$myname" ] &&
	fail 'You forgot to set the custom values in the script!'

if [ -z "$gpg" ]; then
	for dir in `echo $PATH | sed 's#:# #g'`; do
		if [ -x "${dir}/gpg2" ]; then
			gpg="${dir}/gpg2"
			break
		fi
	done
fi
if [ -z "$gpg" ]; then
	for dir in `echo $PATH | sed 's#:# #g'`; do
		if [ -x "${dir}/gpg" ]; then
			gpg="${dir}/gpg"
			break
		fi
	done
fi

[ -n "$gpg" ] || fail "I cannot find a gpg[2] binary in $PATH"

case `uname -s` in
Linux)	echo=echo ; alias sha=sha256sum
	if [ -x /usr/bin/shred ]; then
		delete='/usr/bin/shred -uz'
	else
		delete=/bin/rm
	fi ;;
FreeBSD) echo='echo -e' ; alias sha=sha256 ; delete='/bin/rm -P' ;;
Darwin)	 echo='echo' ; alias sha='shasum -a 256' ; delete='/bin/rm -P' ;;
esac

while getopts 'hnu:y' cla ; do
	case "$cla" in
	h) usage ; exit 0 ;;
	n) nomail=nomail ;;
	u) uo='-u' ; uv="0x${OPTARG#0[Xx]}" ; shift ;;
	y) sign_every_uid=sign_every_uid ;;
	*) fail 'Unknown option' ;;
	esac
	shift
done

[ -n "$1" ] || fail 'I need a PGP keyring or key ID to work with!'

if [ -r "$1" -o -r "${GNUPGHOME}/$1" ]; then
	ko='--keyring' ; kv="$1"
	list=`$gpg --no-options --no-default-keyring $ko $kv --fixed-list-mode --with-colons --list-keys`
else
	for key in "$@"; do
		keylist="$keylist 0x${key#0[Xx]}"
	done
	list=`$gpg --fixed-list-mode --with-colons --list-keys $keylist`
fi

longdate=`date`
date=`date +%Y%m%d`

challenges_file="challenges-${date}"

[ -e "$challenges_file" ] && echo '' >> $challenges_file
echo $longdate >> $challenges_file
echo "Token: $token" >> $challenges_file
echo '' >> $challenges_file

# Because uids have spaces
IFS='
'

for line in $list; do
	case "$line" in
	pub*)	unset skip
		keyid=`echo $line | cut -f5 -d:`
		skeyid="${keyid#[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]}"

		for k in $keyids_to_skip; do
			if [ "$k" = "$skeyid" ]; then
				$echo "\nKeyid $skeyid is in the list of keys to skip"
				echo '==============================================='
				skip=skip ; continue 2
			fi
		done

		case "$line" in
		pub:e:*)	$echo "\nKeyid $skeyid has expired"
				echo '==============================================='
				skip=skip_expired ; continue
				;;
		pub:r:*)	$echo "\nKeyid $skeyid has been revoked"
				echo '==============================================='
				skip=skip_revoked ; continue
				;;
		esac

		caps=`echo $line | cut -f12 -d:`
		$echo "\nKeyid $skeyid has capabilities: $caps"
		echo '==============================================='
		;;
	uid*)	[ -n "$skip" ] && continue

		# Deal with UIDs that have colons in them
		uid=`echo $line | cut -f10- -d:` ; uid="${uid%:}"
		$echo "\n	uid: $uid\n"

		case "$line" in
		uid:e:*)	echo 'EXPIRED -- skipping'
				continue ;;
		uid:r:*)	echo 'REVOKED -- skipping'
				continue ;;
		*\<*@*\>*)	;;	# The uid has an e-mail address, Ok to proceed
		*)		echo 'NO E-MAIL ADDRESS -- skipping'
				continue ;;
		esac

		if [ -z "$sign_every_uid" ]; then
			echo '	Prepare a challenge for this user id? y/n [y] \c'
			read ANSWER

			case "$ANSWER" in
			[Nn]*)	echo ''
				echo "Skipping keyid: $skeyid uid: $uid"
				echo ''
				continue ;;
			esac
		else
			echo '	Preparing a challenge for this user id'
		fi

		email=${uid##*<} ; email=${email%%>*}
		challenge=`echo $token $longdate $keyid $uid | sha`
		[ $? -gt 1 ] && fail 'Generation of challenge failed'

		echo ''
		echo "$skeyid $uid ${challenge%% *}" >> $challenges_file

		body="cm-${skeyid}-${email}-body-$date"
		head="cm-${skeyid}-${email}-head-$date"

		cat << EOF > $body
PGP Key signing challenge message for:
$uid

For key with ID 0x$skeyid

Challenge String:
${challenge%% *}

In order to feel most comfortable signing your key I prefer
to first send you a message with a challenge string in it,
encrypted if your key supports that, and then ask you to send
back a reply signed by the key with the ID mentioned above
that includes the challenge string for each PGP UID you would
like me to sign.

The easiest way to proceed is probably as follows:
1. If you have more than one UID on this key gather the
challenge strings sent to each of the UIDs on this key.
You can return them all in one message.
2. Sign the reply message using this key (ID 0x${skeyid}).
3. I will then sign your key and send it to you.

If you have more than one key that you would like me to sign
repeat the process for each of them. That is, please send
separate messages, each signed by the relevant key.

The reason I like to use this procedure is that in modern PGP
(most notably in software that follows the OpenPGP standard,
including GnuPG) the encryption and signing subkeys are
usually different, and can be separately managed.  This is
why I prefer to send an encrypted challenge where possible,
and ask that you sign the reply.


Regards,

$myname


Source for the script that generated this message can be found at:
https://dougbarton.us/PGP/
EOF
		case "$caps" in
		*E*)	$gpg $ko $kv --armor --sign $uo $uv --encrypt -r $keyid \
				--trust-model always $body && eval $delete $body ;;
		*)	$gpg $ko $kv --armor --clearsign $uo $uv $body && eval $delete $body ;;
		esac

		[ -n "$nomail" ] && continue

		cat << EOF > $head
To: $email
Bcc: $myemail
Subject: PGP challenge message for $uid
EOF
		if [ -r "$head" -a -r "${body}.asc" ]; then
			cat $head ${body}.asc | sendmail -oi -f $myemail -t &&
			    eval $delete $head ${body}*

			echo "Sent mail to: $email" >> $challenges_file
			echo '' >> $challenges_file
		else
			fail 'Creation of the e-mail body and/or headers failed!'
		fi
		;;
	esac	# End of 'case uid*)'
done

exit 0

# Copyright (c) 2007-2014 Douglas Barton
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# $Id: gen_challenges,v 1.28 2014/10/13 08:46:30 doug Exp $