Skip to content

Commit e63cefb

Browse files
committed
Merge branch 'hi/gpg-use-check-signature'
"git merge signed-tag" while lacking the public key started to say "No signature", which was utterly wrong. This regression has been reverted. * hi/gpg-use-check-signature: Revert "gpg-interface: prefer check_signature() for GPG verification"
2 parents 5da7329 + 0106b1d commit e63cefb

File tree

4 files changed

+75
-72
lines changed

4 files changed

+75
-72
lines changed

builtin/fmt-merge-msg.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
494494
enum object_type type;
495495
unsigned long size, len;
496496
char *buf = read_object_file(oid, &type, &size);
497-
struct signature_check sigc = { 0 };
498497
struct strbuf sig = STRBUF_INIT;
499498

500499
if (!buf || type != OBJ_TAG)
@@ -503,12 +502,10 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
503502

504503
if (size == len)
505504
; /* merely annotated */
506-
else if (!check_signature(buf, len, buf + len, size - len,
507-
&sigc)) {
508-
strbuf_addstr(&sig, sigc.gpg_output);
509-
signature_check_clear(&sigc);
510-
} else
511-
strbuf_addstr(&sig, "gpg verification failed.\n");
505+
else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig, NULL)) {
506+
if (!sig.len)
507+
strbuf_addstr(&sig, "gpg verification failed.\n");
508+
}
512509

513510
if (!tag_number++) {
514511
fmt_tag_signature(&tagbuf, &sig, buf, len);

gpg-interface.c

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -256,55 +256,6 @@ static void parse_gpg_output(struct signature_check *sigc)
256256
FREE_AND_NULL(sigc->key);
257257
}
258258

259-
static int verify_signed_buffer(const char *payload, size_t payload_size,
260-
const char *signature, size_t signature_size,
261-
struct strbuf *gpg_output,
262-
struct strbuf *gpg_status)
263-
{
264-
struct child_process gpg = CHILD_PROCESS_INIT;
265-
struct gpg_format *fmt;
266-
struct tempfile *temp;
267-
int ret;
268-
struct strbuf buf = STRBUF_INIT;
269-
270-
temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
271-
if (!temp)
272-
return error_errno(_("could not create temporary file"));
273-
if (write_in_full(temp->fd, signature, signature_size) < 0 ||
274-
close_tempfile_gently(temp) < 0) {
275-
error_errno(_("failed writing detached signature to '%s'"),
276-
temp->filename.buf);
277-
delete_tempfile(&temp);
278-
return -1;
279-
}
280-
281-
fmt = get_format_by_sig(signature);
282-
if (!fmt)
283-
BUG("bad signature '%s'", signature);
284-
285-
argv_array_push(&gpg.args, fmt->program);
286-
argv_array_pushv(&gpg.args, fmt->verify_args);
287-
argv_array_pushl(&gpg.args,
288-
"--status-fd=1",
289-
"--verify", temp->filename.buf, "-",
290-
NULL);
291-
292-
if (!gpg_status)
293-
gpg_status = &buf;
294-
295-
sigchain_push(SIGPIPE, SIG_IGN);
296-
ret = pipe_command(&gpg, payload, payload_size,
297-
gpg_status, 0, gpg_output, 0);
298-
sigchain_pop(SIGPIPE);
299-
300-
delete_tempfile(&temp);
301-
302-
ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
303-
strbuf_release(&buf); /* no matter it was used or not */
304-
305-
return ret;
306-
}
307-
308259
int check_signature(const char *payload, size_t plen, const char *signature,
309260
size_t slen, struct signature_check *sigc)
310261
{
@@ -467,3 +418,51 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sig
467418

468419
return 0;
469420
}
421+
422+
int verify_signed_buffer(const char *payload, size_t payload_size,
423+
const char *signature, size_t signature_size,
424+
struct strbuf *gpg_output, struct strbuf *gpg_status)
425+
{
426+
struct child_process gpg = CHILD_PROCESS_INIT;
427+
struct gpg_format *fmt;
428+
struct tempfile *temp;
429+
int ret;
430+
struct strbuf buf = STRBUF_INIT;
431+
432+
temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
433+
if (!temp)
434+
return error_errno(_("could not create temporary file"));
435+
if (write_in_full(temp->fd, signature, signature_size) < 0 ||
436+
close_tempfile_gently(temp) < 0) {
437+
error_errno(_("failed writing detached signature to '%s'"),
438+
temp->filename.buf);
439+
delete_tempfile(&temp);
440+
return -1;
441+
}
442+
443+
fmt = get_format_by_sig(signature);
444+
if (!fmt)
445+
BUG("bad signature '%s'", signature);
446+
447+
argv_array_push(&gpg.args, fmt->program);
448+
argv_array_pushv(&gpg.args, fmt->verify_args);
449+
argv_array_pushl(&gpg.args,
450+
"--status-fd=1",
451+
"--verify", temp->filename.buf, "-",
452+
NULL);
453+
454+
if (!gpg_status)
455+
gpg_status = &buf;
456+
457+
sigchain_push(SIGPIPE, SIG_IGN);
458+
ret = pipe_command(&gpg, payload, payload_size,
459+
gpg_status, 0, gpg_output, 0);
460+
sigchain_pop(SIGPIPE);
461+
462+
delete_tempfile(&temp);
463+
464+
ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
465+
strbuf_release(&buf); /* no matter it was used or not */
466+
467+
return ret;
468+
}

gpg-interface.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ size_t parse_signature(const char *buf, size_t size);
5454
int sign_buffer(struct strbuf *buffer, struct strbuf *signature,
5555
const char *signing_key);
5656

57+
/*
58+
* Run "gpg" to see if the payload matches the detached signature.
59+
* gpg_output, when set, receives the diagnostic output from GPG.
60+
* gpg_status, when set, receives the status output from GPG.
61+
*/
62+
int verify_signed_buffer(const char *payload, size_t payload_size,
63+
const char *signature, size_t signature_size,
64+
struct strbuf *gpg_output, struct strbuf *gpg_status);
65+
5766
int git_gpg_config(const char *, const char *, void *);
5867
void set_signing_key(const char *);
5968
const char *get_signing_key(void);

log-tree.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -449,22 +449,22 @@ static void show_signature(struct rev_info *opt, struct commit *commit)
449449
{
450450
struct strbuf payload = STRBUF_INIT;
451451
struct strbuf signature = STRBUF_INIT;
452-
struct signature_check sigc = { 0 };
452+
struct strbuf gpg_output = STRBUF_INIT;
453453
int status;
454454

455455
if (parse_signed_commit(commit, &payload, &signature) <= 0)
456456
goto out;
457457

458-
status = check_signature(payload.buf, payload.len, signature.buf,
459-
signature.len, &sigc);
460-
if (status && sigc.result == 'N')
461-
show_sig_lines(opt, status, "No signature\n");
462-
else {
463-
show_sig_lines(opt, status, sigc.gpg_output);
464-
signature_check_clear(&sigc);
465-
}
458+
status = verify_signed_buffer(payload.buf, payload.len,
459+
signature.buf, signature.len,
460+
&gpg_output, NULL);
461+
if (status && !gpg_output.len)
462+
strbuf_addstr(&gpg_output, "No signature\n");
463+
464+
show_sig_lines(opt, status, gpg_output.buf);
466465

467466
out:
467+
strbuf_release(&gpg_output);
468468
strbuf_release(&payload);
469469
strbuf_release(&signature);
470470
}
@@ -497,7 +497,6 @@ static int show_one_mergetag(struct commit *commit,
497497
struct object_id oid;
498498
struct tag *tag;
499499
struct strbuf verify_message;
500-
struct signature_check sigc = { 0 };
501500
int status, nth;
502501
size_t payload_size, gpg_message_offset;
503502

@@ -527,13 +526,12 @@ static int show_one_mergetag(struct commit *commit,
527526
status = -1;
528527
if (extra->len > payload_size) {
529528
/* could have a good signature */
530-
if (!check_signature(extra->value, payload_size,
531-
extra->value + payload_size,
532-
extra->len - payload_size, &sigc)) {
533-
strbuf_addstr(&verify_message, sigc.gpg_output);
534-
signature_check_clear(&sigc);
529+
if (!verify_signed_buffer(extra->value, payload_size,
530+
extra->value + payload_size,
531+
extra->len - payload_size,
532+
&verify_message, NULL))
535533
status = 0; /* good */
536-
} else if (verify_message.len <= gpg_message_offset)
534+
else if (verify_message.len <= gpg_message_offset)
537535
strbuf_addstr(&verify_message, "No signature\n");
538536
/* otherwise we couldn't verify, which is shown as bad */
539537
}

0 commit comments

Comments
 (0)