-# generate a subkey with the 'a' usage flags set
-# FIXME: this needs some tweaking to clean it up
-gen_subkey(){
- local keyID
- local gpgOut
- local userID
-
- keyID="$1"
-
- gpgOut=$(gpg --quiet --fixed-list-mode --list-keys --with-colons \
- "$keyID" 2> /dev/null)
-
- # fail if there only "tru" lines are output from gpg, which
- # indicates the key was not found.
- if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
- failure "Key ID '$keyID' not found."
- fi
-
- # fail if multiple pub lines are returned, which means the id given
- # is not unique
- if [ $(echo "$gpgOut" | grep '^pub:' | wc -l) -gt '1' ] ; then
- failure "Key ID '$keyID' is not unique."
- fi
-
- # prompt if an authentication subkey already exists
- if echo "$gpgOut" | egrep "^(pub|sub):" | cut -d: -f 12 | grep -q a ; then
- echo "An authentication subkey already exists for key '$keyID'."
- read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
- if [ "${OK/y/Y}" != 'Y' ] ; then
- failure "aborting."
- fi
- fi
-
- # set subkey defaults
- KEY_TYPE="RSA"
- KEY_LENGTH=${KEY_LENGTH:-}
- KEY_USAGE="auth"
- # prompt about key expiration if not specified
- if [ -z "$KEY_EXPIRE" ] ; then
- cat <<EOF
-Please specify how long the key should be valid.
- 0 = key does not expire
- <n> = key expires in n days
- <n>w = key expires in n weeks
- <n>m = key expires in n months
- <n>y = key expires in n years
-EOF
- while [ -z "$KEY_EXPIRE" ] ; do
- read -p "Key is valid for? (0) " KEY_EXPIRE
- if ! test_gpg_expire ${KEY_EXPIRE:=0} ; then
- echo "invalid value"
- unset KEY_EXPIRE
- fi
- done
- elif ! test_gpg_expire "$KEY_EXPIRE" ; then
- failure "invalid key expiration value '$KEY_EXPIRE'."
- fi
-
- # generate the list of commands that will be passed to edit-key
- editCommands=$(cat <<EOF
-addkey
-7
-S
-E
-A
-Q
-$KEY_LENGTH
-$KEY_EXPIRE
-save
-EOF
-)
-
- log "generating subkey..."
- echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
- log "done."
-}
-