gpg-interface t: extend the existing GPG tests with GPGSM
authorHenning Schild <henning.schild@siemens.com>
Fri, 20 Jul 2018 08:28:07 +0000 (10:28 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 20 Jul 2018 15:41:42 +0000 (08:41 -0700)
Add test cases to cover the new X509/gpgsm support. Most of them
resemble existing ones. They just switch the format to x509 and set the
signingkey when creating signatures. Validation of signatures does not
need any configuration of git, it does need gpgsm to be configured to
trust the key(-chain).
Several of the testcases build on top of existing gpg testcases.
The commit ships a self-signed key for committer@example.com and
configures gpgsm to trust it.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/lib-gpg.sh
t/lib-gpg/gpgsm-gen-key.in [new file with mode: 0644]
t/lib-gpg/gpgsm_cert.p12 [new file with mode: 0644]
t/t4202-log.sh
t/t5534-push-signed.sh
t/t7004-tag.sh
t/t7030-verify-tag.sh
index a5d3b2cbaad4edd078b8d58160dc4252ce8adec9..3fe02876c1fc7e4f7488c1534a4745f5760f1ca0 100755 (executable)
@@ -38,7 +38,33 @@ then
                        "$TEST_DIRECTORY"/lib-gpg/ownertrust &&
                gpg --homedir "${GNUPGHOME}" </dev/null >/dev/null 2>&1 \
                        --sign -u committer@example.com &&
-               test_set_prereq GPG
+               test_set_prereq GPG &&
+               # Available key info:
+               # * see t/lib-gpg/gpgsm-gen-key.in
+               # To generate new certificate:
+               #  * no passphrase
+               #       gpgsm --homedir /tmp/gpghome/ \
+               #               -o /tmp/gpgsm.crt.user \
+               #               --generate-key \
+               #               --batch t/lib-gpg/gpgsm-gen-key.in
+               # To import certificate:
+               #       gpgsm --homedir /tmp/gpghome/ \
+               #               --import /tmp/gpgsm.crt.user
+               # To export into a .p12 we can later import:
+               #       gpgsm --homedir /tmp/gpghome/ \
+               #               -o t/lib-gpg/gpgsm_cert.p12 \
+               #               --export-secret-key-p12 "committer@example.com"
+               echo | gpgsm --homedir "${GNUPGHOME}" 2>/dev/null \
+                       --passphrase-fd 0 --pinentry-mode loopback \
+                       --import "$TEST_DIRECTORY"/lib-gpg/gpgsm_cert.p12 &&
+               gpgsm --homedir "${GNUPGHOME}" 2>/dev/null -K \
+                       | grep fingerprint: | cut -d" " -f4 | tr -d '\n' > \
+                       ${GNUPGHOME}/trustlist.txt &&
+               echo " S relax" >> ${GNUPGHOME}/trustlist.txt &&
+               (gpgconf --kill gpg-agent >/dev/null 2>&1 || : ) &&
+               echo hello | gpgsm --homedir "${GNUPGHOME}" >/dev/null \
+                       -u committer@example.com -o /dev/null --sign - 2>&1 &&
+               test_set_prereq GPGSM
                ;;
        esac
 fi
diff --git a/t/lib-gpg/gpgsm-gen-key.in b/t/lib-gpg/gpgsm-gen-key.in
new file mode 100644 (file)
index 0000000..a7fd87c
--- /dev/null
@@ -0,0 +1,8 @@
+Key-Type: RSA
+Key-Length: 2048
+Key-Usage: sign
+Serial: random
+Name-DN: CN=C O Mitter, O=Example, SN=C O, GN=Mitter
+Name-Email: committer@example.com
+Not-Before: 1970-01-01 00:00:00
+Not-After: 3000-01-01 00:00:00
diff --git a/t/lib-gpg/gpgsm_cert.p12 b/t/lib-gpg/gpgsm_cert.p12
new file mode 100644 (file)
index 0000000..94ffad0
Binary files /dev/null and b/t/lib-gpg/gpgsm_cert.p12 differ
index 25b1f8cc73bc35bedaaffeacbb75a1329d725c3b..05d3707e38befc214678b4010ed96b6be94f16b3 100755 (executable)
@@ -1556,12 +1556,28 @@ test_expect_success GPG 'setup signed branch' '
        git commit -S -m signed_commit
 '
 
+test_expect_success GPGSM 'setup signed branch x509' '
+       test_when_finished "git reset --hard && git checkout master" &&
+       git checkout -b signed-x509 master &&
+       echo foo >foo &&
+       git add foo &&
+       test_config gpg.format x509 &&
+       test_config user.signingkey $GIT_COMMITTER_EMAIL &&
+       git commit -S -m signed_commit
+'
+
 test_expect_success GPG 'log --graph --show-signature' '
        git log --graph --show-signature -n1 signed >actual &&
        grep "^| gpg: Signature made" actual &&
        grep "^| gpg: Good signature" actual
 '
 
+test_expect_success GPGSM 'log --graph --show-signature x509' '
+       git log --graph --show-signature -n1 signed-x509 >actual &&
+       grep "^| gpgsm: Signature made" actual &&
+       grep "^| gpgsm: Good signature" actual
+'
+
 test_expect_success GPG 'log --graph --show-signature for merged tag' '
        test_when_finished "git reset --hard && git checkout master" &&
        git checkout -b plain master &&
@@ -1581,6 +1597,27 @@ test_expect_success GPG 'log --graph --show-signature for merged tag' '
        grep "^| | gpg: Good signature" actual
 '
 
+test_expect_success GPGSM 'log --graph --show-signature for merged tag x509' '
+       test_when_finished "git reset --hard && git checkout master" &&
+       test_config gpg.format x509 &&
+       test_config user.signingkey $GIT_COMMITTER_EMAIL &&
+       git checkout -b plain-x509 master &&
+       echo aaa >bar &&
+       git add bar &&
+       git commit -m bar_commit &&
+       git checkout -b tagged-x509 master &&
+       echo bbb >baz &&
+       git add baz &&
+       git commit -m baz_commit &&
+       git tag -s -m signed_tag_msg signed_tag_x509 &&
+       git checkout plain-x509 &&
+       git merge --no-ff -m msg signed_tag_x509 &&
+       git log --graph --show-signature -n1 plain-x509 >actual &&
+       grep "^|\\\  merged tag" actual &&
+       grep "^| | gpgsm: Signature made" actual &&
+       grep "^| | gpgsm: Good signature" actual
+'
+
 test_expect_success GPG '--no-show-signature overrides --show-signature' '
        git log -1 --show-signature --no-show-signature signed >actual &&
        ! grep "^gpg:" actual
index 1cea758f789edef35a61ce1a9bc11d9fda7bd7ac..030331f1c51fc2a962fffd04c84d8f31d096311c 100755 (executable)
@@ -194,10 +194,12 @@ test_expect_success GPG 'fail without key and heed user.signingkey' '
 
        EOF
 
-       unset GIT_COMMITTER_EMAIL &&
-       git config user.email hasnokey@nowhere.com &&
-       test_must_fail git push --signed dst noop ff +noff &&
-       git config user.signingkey committer@example.com &&
+       test_config user.email hasnokey@nowhere.com &&
+       (
+               sane_unset GIT_COMMITTER_EMAIL &&
+               test_must_fail git push --signed dst noop ff +noff
+       ) &&
+       test_config user.signingkey $GIT_COMMITTER_EMAIL &&
        git push --signed dst noop ff +noff &&
 
        (
@@ -218,4 +220,57 @@ test_expect_success GPG 'fail without key and heed user.signingkey' '
        test_cmp expect dst/push-cert-status
 '
 
+test_expect_success GPGSM 'fail without key and heed user.signingkey x509' '
+       test_config gpg.format x509 &&
+       prepare_dst &&
+       mkdir -p dst/.git/hooks &&
+       git -C dst config receive.certnonceseed sekrit &&
+       write_script dst/.git/hooks/post-receive <<-\EOF &&
+       # discard the update list
+       cat >/dev/null
+       # record the push certificate
+       if test -n "${GIT_PUSH_CERT-}"
+       then
+               git cat-file blob $GIT_PUSH_CERT >../push-cert
+       fi &&
+
+       cat >../push-cert-status <<E_O_F
+       SIGNER=${GIT_PUSH_CERT_SIGNER-nobody}
+       KEY=${GIT_PUSH_CERT_KEY-nokey}
+       STATUS=${GIT_PUSH_CERT_STATUS-nostatus}
+       NONCE_STATUS=${GIT_PUSH_CERT_NONCE_STATUS-nononcestatus}
+       NONCE=${GIT_PUSH_CERT_NONCE-nononce}
+       E_O_F
+
+       EOF
+
+       test_config user.email hasnokey@nowhere.com &&
+       test_config user.signingkey "" &&
+       (
+               sane_unset GIT_COMMITTER_EMAIL &&
+               test_must_fail git push --signed dst noop ff +noff
+       ) &&
+       test_config user.signingkey $GIT_COMMITTER_EMAIL &&
+       git push --signed dst noop ff +noff &&
+
+       (
+               cat <<-\EOF &&
+               SIGNER=/CN=C O Mitter/O=Example/SN=C O/GN=Mitter
+               KEY=
+               STATUS=G
+               NONCE_STATUS=OK
+               EOF
+               sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" dst/push-cert
+       ) >expect.in &&
+       key=$(cat "${GNUPGHOME}/trustlist.txt" | cut -d" " -f1 | tr -d ":") &&
+       sed -e "s/^KEY=/KEY=${key}/" expect.in >expect &&
+
+       noop=$(git rev-parse noop) &&
+       ff=$(git rev-parse ff) &&
+       noff=$(git rev-parse noff) &&
+       grep "$noop $ff refs/heads/ff" dst/push-cert &&
+       grep "$noop $noff refs/heads/noff" dst/push-cert &&
+       test_cmp expect dst/push-cert-status
+'
+
 test_done
index d7b319e919c83ca677737840f70075c173364209..2147938aa1b60d8b67557e0bbe3c0368ef712b69 100755 (executable)
@@ -1354,6 +1354,19 @@ test_expect_success GPG \
        'test_config gpg.program echo &&
         test_must_fail git tag -s -m tail tag-gpg-failure'
 
+# try to sign with bad user.signingkey
+test_expect_success GPGSM \
+       'git tag -s fails if gpgsm is misconfigured (bad key)' \
+       'test_config user.signingkey BobTheMouse &&
+        test_config gpg.format x509 &&
+        test_must_fail git tag -s -m tail tag-gpg-failure'
+
+# try to produce invalid signature
+test_expect_success GPGSM \
+       'git tag -s fails if gpgsm is misconfigured (bad signature format)' \
+       'test_config gpg.x509.program echo &&
+        test_config gpg.format x509 &&
+        test_must_fail git tag -s -m tail tag-gpg-failure'
 
 # try to verify without gpg:
 
index 291a1e2b07417559d3f3defd6a60043b505eb3c7..99f35a5bbe47aff93121c5e6beeec2dce00d18be 100755 (executable)
@@ -41,6 +41,13 @@ test_expect_success GPG 'create signed tags' '
        git tag -uB7227189 -m eighth eighth-signed-alt
 '
 
+test_expect_success GPGSM 'create signed tags x509 ' '
+       test_config gpg.format x509 &&
+       test_config user.signingkey $GIT_COMMITTER_EMAIL &&
+       echo 9 >file && test_tick && git commit -a -m "nineth gpgsm-signed" &&
+       git tag -s -m nineth nineth-signed-x509
+'
+
 test_expect_success GPG 'verify and show signatures' '
        (
                for tag in initial second merge fourth-signed sixth-signed seventh-signed
@@ -72,6 +79,13 @@ test_expect_success GPG 'verify and show signatures' '
        )
 '
 
+test_expect_success GPGSM 'verify and show signatures x509' '
+       git verify-tag nineth-signed-x509 2>actual &&
+       grep "Good signature from" actual &&
+       ! grep "BAD signature from" actual &&
+       echo nineth-signed-x509 OK
+'
+
 test_expect_success GPG 'detect fudged signature' '
        git cat-file tag seventh-signed >raw &&
        sed -e "/^tag / s/seventh/7th forged/" raw >forged1 &&
@@ -112,6 +126,13 @@ test_expect_success GPG 'verify signatures with --raw' '
        )
 '
 
+test_expect_success GPGSM 'verify signatures with --raw x509' '
+       git verify-tag --raw nineth-signed-x509 2>actual &&
+       grep "GOODSIG" actual &&
+       ! grep "BADSIG" actual &&
+       echo nineth-signed-x509 OK
+'
+
 test_expect_success GPG 'verify multiple tags' '
        tags="fourth-signed sixth-signed seventh-signed" &&
        for i in $tags
@@ -125,6 +146,19 @@ test_expect_success GPG 'verify multiple tags' '
        test_cmp expect.stderr actual.stderr
 '
 
+test_expect_success GPGSM 'verify multiple tags x509' '
+       tags="seventh-signed nineth-signed-x509" &&
+       for i in $tags
+       do
+               git verify-tag -v --raw $i || return 1
+       done >expect.stdout 2>expect.stderr.1 &&
+       grep "^.GNUPG:." <expect.stderr.1 >expect.stderr &&
+       git verify-tag -v --raw $tags >actual.stdout 2>actual.stderr.1 &&
+       grep "^.GNUPG:." <actual.stderr.1 >actual.stderr &&
+       test_cmp expect.stdout actual.stdout &&
+       test_cmp expect.stderr actual.stderr
+'
+
 test_expect_success GPG 'verifying tag with --format' '
        cat >expect <<-\EOF &&
        tagname : fourth-signed