9

I am writing a bash script which generates unattended GPG keys, I have looked through Unattended Usage of GPG and was surprised to find that 'Currently only one subkey can be handled.'

I am unable to find if there is a way to modify a GPG key to add a second subkey using the unattended generation functions available, or if I'll have to add the subkey manually myself.

I have attempted to use here documents, here strings and creating a file with one input per line as well as using printf similar to below:

printf 'addkey' | gpg2 --edit-key '[email protected]'

With none of these solutions working:

balthasar@magi:~$ printf 'addkey' | gpg2 --edit-key '[email protected]'
printf 'addkey' | gpg2 --edit-key '[email protected]'
gpg (GnuPG) 2.0.26; Copyright (C) 2013 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Secret key is available.

pub  4096R/AB96CED4  created: 2016-12-02  expires: 2017-09-02  usage: SC  
                     trust: ultimate      validity: ultimate
sub  2048R/71804CF2  created: 2016-12-02  expires: 2017-09-02  usage: E   
[ultimate] (1). test3 <[email protected]>

gpg> 

My GPG Key File

Key-Type: RSA
Key-Length: 4096
Key-Usage: sign
Subkey-Type: RSA
Subkey-Length: 2048
Subkey-Usage: encrypt
Name-Real: Foo Bar
Name-Email: [email protected]
Expire-Date: 2017-09-02
%ask-passphrase
%commit

UPDATE: 4, December 2016

I have passed the first hurdle of getting some input to GPG by using the following snippet:

~$ stty -echo; gpg2 --edit-key <keyname> "addkey"

However I am unable to 'pipe' input after running the addkey command.

3 Answers 3

11

Save for the year 2019, this how you do it in an ephemeral home directory:

export GNUPGHOME=$(mktemp -d)
gpg --batch --passphrase '' \
    --quick-generate-key "Firstname Lastname <[email protected]>" ed25519 cert 1y

FPR=$(gpg --list-options show-only-fpr-mbox --list-secret-keys | awk '{print $1}')

gpg --batch --passphrase '' \
    --quick-add-key $FPR ed25519 sign 1y
gpg --batch --passphrase '' \
    --quick-add-key $FPR cv25519 encrypt 1y

The trick is to use --quick-add-key.

An authentication subkey can be added similarly. We get:

$ gpg -K
/tmp/tmp.JSOrV6s0iL/pubring.kbx
-------------------------------
sec   ed25519 2019-04-11 [C] [expires: 2020-04-10]
      7E00D8318E2A2825F40981D00C6CA12AC7F293F3
uid           [ultimate] Firstname Lastname <[email protected]>
ssb   ed25519 2019-04-11 [S] [expires: 2020-04-10]
ssb   cv25519 2019-04-11 [E] [expires: 2020-04-10]

$ gpg --version
gpg (GnuPG) 2.2.12
libgcrypt 1.8.4
6

To have gpg2 read your commands from stdin, use --command-fd=0. To have gpg2 read the passphrase of the master key from where it reads your commands from, use --pinentry-mode=loopback. (gpg2 --help does not list these switches, but they are documented in the man page.) Using these switches, you can communicate with gpg2 non-interactively.

I added a subkey to an existing master key by running bash commands similar to the following ones:

key=0123456789ABCDEF
passphrase="my passphrase"
{
    echo addkey
    echo 4     # RSA (sign only)
    echo 4096  # key length
    echo 0     # key does not expire
    echo y     # is this correct?
    echo y     # really create?
    echo "$passphrase"
    echo save
} | gpg2 --command-fd=0 --status-fd=1 --pinentry-mode=loopback --edit-key $key
0

Assuming you have a expect installed in your system and ${MY_KEY} and ${MY_PASSPHRASE} are set properly, you can start with this.

/usr/bin/expect <<EOF
set timeout 30
spawn gpg --command-fd=0 --status-fd=1 --pinentry-mode=loopback --edit-key ${MY_KEY}
expect {
    timeout                         {send_user "\n!!! timeout !!!\n"; exit 127}
    "Bad passphrase*"               {exit 127}
    "GET_LINE keyedit.prompt*"      {send -- "addkey\r"; exp_continue}
    "GET_LINE keygen.algo*"         {send -- "4\r"; exp_continue}
    "GET_LINE keygen.size*"         {send -- "4096\r"; exp_continue}
    "GET_LINE keygen.valid*"        {send -- "0\r"; exp_continue}
    "Is this correct?*"             {send -- "y\r"; exp_continue}
    "Really create?*"               {send -- "y\r"; exp_continue}
    "KEY_CREATED S*"                {send -- "save\r"; exp_continue}
    "GET_HIDDEN passphrase.enter*"  {send -- "${MY_PASSPHRASE}\r"; exp_continue}
}
set returnvalues [wait]
set exitcode [lindex \$returnvalues 3]
exit \$exitcode
EOF

You must log in to answer this question.