gpg-interface: check gpg signature creation status

When we create a signature, it may happen that gpg returns with "success" but not with an actual detached signature on stdout. Check for the correct signature creation status to catch these cases better. Really, --status-fd parsing is the only way to check gpg status reliably. We do the same for verify already. Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael J Gruber committed Jun 17, 2016 at 19:38 UTC efee9553a4f97b2ecd8f49be19606dd4cf7d9c28
2 files changed +14 -3
gpg-interface.c
+6 -2
@@ -153,9 +153,11 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sig
153 struct child_process gpg = CHILD_PROCESS_INIT;
154 int ret;
155 size_t i, j, bottom;
156 + struct strbuf gpg_status = STRBUF_INIT;
157
158 argv_array_pushl(&gpg.args,
159 gpg_program,
160 + "--status-fd=2",
161 "-bsau", signing_key,
162 NULL);
163
@@ -167,10 +169,12 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sig
169 */
170 sigchain_push(SIGPIPE, SIG_IGN);
171 ret = pipe_command(&gpg, buffer->buf, buffer->len,
170 - signature, 1024, NULL, 0);
172 + signature, 1024, &gpg_status, 0);
173 sigchain_pop(SIGPIPE);
174
173 - if (ret || signature->len == bottom)
175 + ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
176 + strbuf_release(&gpg_status);
177 + if (ret)
178 return error(_("gpg failed to sign the data"));
179
180 /* Strip CR from the line endings, in case we are on Windows. */
t/t7004-tag.sh
+8 -1
@@ -1202,10 +1202,17 @@ test_expect_success GPG,RFC1991 \
1202 # try to sign with bad user.signingkey
1203 git config user.signingkey BobTheMouse
1204 test_expect_success GPG \
1205 - 'git tag -s fails if gpg is misconfigured' \
1205 + 'git tag -s fails if gpg is misconfigured (bad key)' \
1206 'test_must_fail git tag -s -m tail tag-gpg-failure'
1207 git config --unset user.signingkey
1208
1209 +# try to produce invalid signature
1210 +test_expect_success GPG \
1211 + 'git tag -s fails if gpg is misconfigured (bad signature format)' \
1212 + 'test_config gpg.program echo &&
1213 + test_must_fail git tag -s -m tail tag-gpg-failure'
1214 +
1215 +
1216 # try to verify without gpg:
1217
1218 rm -rf gpghome