| 1 |
#! /bin/bash |
| 2 |
|
| 3 |
# NOTE: To generate v3 signatures, this MUST be run on a system with GPG < 2.1, e.g. a RHEL 7. |
| 4 |
# WARNING: This lazily writes to $(pwd). It is best run on a short-term VM. |
| 5 |
|
| 6 |
# This is only a fragment of the ideal script; as you regenerate any other keys, please work on improving it. |
| 7 |
|
| 8 |
set -x |
| 9 |
|
| 10 |
dest=$(pwd)/new-fixtures |
| 11 |
mkdir -p "$dest" |
| 12 |
|
| 13 |
function resign() { |
| 14 |
local key_id=$1 |
| 15 |
local signature=$2 |
| 16 |
local other_opts=$3 |
| 17 |
|
| 18 |
(GNUPGHOME= gpg -d "signature/fixtures/$signature"; true) | gpg --sign --digest-algo SHA256 --default-key "$key_id" $other_opts - > "$dest/$signature" |
| 19 |
} |
| 20 |
|
| 21 |
export GNUPGHOME=$(mktemp -d -t regenerate-keys.XXXXXX) |
| 22 |
echo "GNUPGHOME: $GNUPGHOME" # Don't set up trap(1) to delete it, to allow inspection / debugging. |
| 23 |
|
| 24 |
# Key-Usage: auth is used because "cert" is implied, and the only one we want, but an empty value is not accepted |
| 25 |
# by gpg. |
| 26 |
cat >batch-input <<EOF |
| 27 |
Key-Type: RSA |
| 28 |
Key-Length: 3072 |
| 29 |
Key-Usage: auth |
| 30 |
Subkey-Type: RSA |
| 31 |
Subkey-Length: 3072 |
| 32 |
Subkey-Usage: sign |
| 33 |
%no-protection |
| 34 |
Name-Real: c/image test key with subkey |
| 35 |
Expire-Date: 0 |
| 36 |
%commit |
| 37 |
EOF |
| 38 |
out=$(gpg --batch --gen-key --cert-digest-algo SHA256 < batch-input --status-fd 1 --with-colons) |
| 39 |
echo "$out" | grep -v ' PROGRESS ' |
| 40 |
|
| 41 |
fingerprint=$(echo "$out" | awk '$2 == "KEY_CREATED" { print $4 }') |
| 42 |
# Yes, --fingerprint is used twice, to include the subkey fingerprint. |
| 43 |
subkey_fingerprint=$(gpg --list-keys --fingerprint --fingerprint --with-colon "$fingerprint" | awk -F ':' '$1 == "fpr" { fp = $10 } END { print fp }') |
| 44 |
echo "TestKeyFingerprintPrimaryWithSubkey = \"$fingerprint\"" > fixtures_info |
| 45 |
echo "TestKeyFingerprintSubkeyWithSubkey = \"$subkey_fingerprint\"" >> fixtures_info |
| 46 |
|
| 47 |
resign $subkey_fingerprint subkey.signature |
| 48 |
resign $subkey_fingerprint subkey.signature-v3 --force-v3-sigs |
| 49 |
gpg --export --armor "$fingerprint" > $dest/public-key-with-subkey.gpg |
| 50 |
|
| 51 |
# Key-Usage: auth is used because "cert" is implied, and the only one we want, but an empty value is not accepted |
| 52 |
# by gpg. |
| 53 |
cat >batch-input <<EOF |
| 54 |
Key-Type: RSA |
| 55 |
Key-Length: 3072 |
| 56 |
Key-Usage: auth |
| 57 |
Subkey-Type: RSA |
| 58 |
Subkey-Length: 3072 |
| 59 |
Subkey-Usage: sign |
| 60 |
%no-protection |
| 61 |
Name-Real: c/image test key with a REVOKED subkey |
| 62 |
Expire-Date: 0 |
| 63 |
%commit |
| 64 |
EOF |
| 65 |
out=$(gpg --batch --gen-key --cert-digest-algo SHA256 < batch-input --status-fd 1 --with-colons) |
| 66 |
echo "$out" | grep -v ' PROGRESS ' |
| 67 |
|
| 68 |
fingerprint=$(echo "$out" | awk '$2 == "KEY_CREATED" { print $4 }') |
| 69 |
# Yes, --fingerprint is used twice, to include the subkey fingerprint. |
| 70 |
subkey_fingerprint=$(gpg --list-keys --fingerprint --fingerprint --with-colon "$fingerprint" | awk -F ':' '$1 == "fpr" { fp = $10 } END { print fp }') |
| 71 |
echo "TestKeyFingerprintPrimaryWithRevokedSubkey = \"$fingerprint\"" >> fixtures_info |
| 72 |
echo "TestKeyFingerprintSubkeyWithRevokedSubkey = \"$subkey_fingerprint\"" >> fixtures_info |
| 73 |
|
| 74 |
resign $subkey_fingerprint subkey-revoked.signature |
| 75 |
resign $subkey_fingerprint subkey-revoked.signature-v3 --force-v3-sigs |
| 76 |
|
| 77 |
# FIXME? Can this be fully automated? --batch alone doesn't work, --yes seems to be ignored. |
| 78 |
# Answer "yes", "key is compromised" (NOT "no longer used", to break the subkey-revoked.signature* files created above), |
| 79 |
# an empty message, and finally, "save" |
| 80 |
gpg --yes --cert-digest-algo SHA256 --edit-key "$fingerprint" 'key 1' 'revkey' |
| 81 |
|
| 82 |
gpg --export --armor "$fingerprint" > $dest/public-key-with-revoked-subkey.gpg |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
# EVENTUALLY, rebuild signature/fixtures/pubring.gpg from all keys (currently impossible because this script |
| 88 |
# does not regenerate all keys that should be present there): |
| 89 |
# GNUPGHOME=$dest gpg --import "$dest/public-key-with-subkey.gpg" |
| 90 |
|
| 91 |
# === We are done. Show how the regenerated files differ. |
| 92 |
for i in "$dest"/*; do |
| 93 |
(echo "==== $i"; diff -u <(gpg --list-packets < "signature/fixtures/${i#$dest/}") <(gpg --list-packets < "$i")) |& less |
| 94 |
done |
| 95 |
|
| 96 |
cat fixtures_info |