#!/bin/sh # $Id: gen_challenges,v 1.12 2010/07/09 20:07:36 dougb Exp $ # Copyright (c) 2007-2010 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. keyids_to_skip= # # Nothing below here needs to be changed, but improvements are always welcome # # Assumptions: # The UID will contain a valid e-mail address # You have a binary called 'sha256' 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 have a keyring file that contains the keys you want to sign # 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. # Usage: # ./gen_challenges [-n] | # The -n option will cause the script not to send mail so you # can evaluate the output first. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin export PATH 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 if [ -z "$gpg" ]; then echo "I cannot find a gpg[2] binary in $PATH" >&2 exit 1 fi # Because uids have spaces IFS=' ' case "$1" in -n) nomail=yes ; shift ;; esac if [ -z "$token" -o -z "$myemail" -o -z "$myname" ]; then echo 'You forgot to set the custom values in the script!' >&2 exit 1 fi if [ -z "$1" ]; then echo 'I need a PGP keyring or key ID to work with!' >&2 exit 1 fi if [ -r "$1" -o -r "$HOME/.gnupg/$1" ]; then keyring="--keyring $1" list=`$gpg --no-options --no-default-keyring $keyring --list-keys --fixed-list-mode --with-colons` else list=`$gpg --fixed-list-mode --with-colons --list-keys 0x${1#0[Xx]}` fi date=`date +%Y%m%d` echo '' >> challenges-$date date >> challenges-$date echo "Token: $token" >> challenges-$date echo '' >> challenges-$date 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 "Skipping keyid: $skeyid" skip=yes continue 2 fi done caps=`echo $line | cut -f12 -d:` echo '' echo "keyid: $skeyid capabilities: $caps" echo '===============================================' ;; uid*) [ -n "$skip" ] && continue uid=`echo $line | cut -f10 -d:` echo '' echo " uid: $uid" echo '' echo -n ' Prepare a challenge for this user id? [y] ' read ANSWER case "$ANSWER" in [Nn]*) echo '' echo "Skipping keyid: $skeyid uid: $uid" echo '' continue ;; esac email=`echo $uid | sed -e 's#.*<##' -e 's#>.*##'` challenge=`echo $token $keyid $uid | sha256` echo '' echo "$skeyid $uid $challenge" >> challenges-$date 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 all the challenge strings for this key into one reply. 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. 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: http://dougbarton.us/PGP/ EOF case "$caps" in *E*) $gpg $keyring --armor --sign --encrypt -r $keyid \ --trust-model always $body && rm -P $body ;; *) $gpg $keyring --armor --clearsign $body && rm -P $body ;; esac case "$nomail" in yes) continue ;; esac 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 && rm -P $head ${body}* echo "Sent mail to: $email" >> challenges-$date echo '' >> challenges-$date else echo '' >&2 echo 'Creation of the e-mail body and/or headers failed!' >&2 exit 1 fi ;; esac # End of 'case uid*)' done #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Copyright (c) 2007-2010 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.