Split GPG interface into its own helper library

This mostly moves existing code from builtin/tag.c (for signing)
and builtin/verify-tag.c (for verifying) to a new gpg-interface.c
file to provide a more generic library interface.

 - sign_buffer() takes a payload strbuf, a signature strbuf, and a signing
   key, runs "gpg" to produce a detached signature for the payload, and
   appends it to the signature strbuf. The contents of a signed tag that
   concatenates the payload and the detached signature can be produced by
   giving the same strbuf as payload and signature strbuf.

 - verify_signed_buffer() takes a payload and a detached signature as
   <ptr, len> pairs, and runs "gpg --verify" to see if the payload matches
   the signature. It can optionally capture the output from GPG to allow
   the callers to pretty-print it in a way more suitable for their
   contexts.

"verify-tag" (aka "tag -v") used to save the whole tag contents as if it
is a detached signature, and fed gpg the payload part of the tag. It
relied on gpg to fail when the given tag is not signed but just is
annotated.  The updated run_gpg_verify() function detects the lack of
detached signature in the input, and errors out without bothering "gpg".

Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano
2011-09-07 21:19:47 -07:00
parent 3dfbe68fc2
commit 2f47eae2a1
6 changed files with 166 additions and 100 deletions

View File

@ -14,6 +14,7 @@
#include "parse-options.h"
#include "diff.h"
#include "revision.h"
#include "gpg-interface.h"
static const char * const git_tag_usage[] = {
"git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
@ -23,8 +24,6 @@ static const char * const git_tag_usage[] = {
NULL
};
static char signingkey[1000];
struct tag_filter {
const char **patterns;
int lines;
@ -208,60 +207,7 @@ static int verify_tag(const char *name, const char *ref,
static int do_sign(struct strbuf *buffer)
{
struct child_process gpg;
const char *args[4];
char *bracket;
int len;
int i, j;
if (!*signingkey) {
if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
sizeof(signingkey)) > sizeof(signingkey) - 1)
return error(_("committer info too long."));
bracket = strchr(signingkey, '>');
if (bracket)
bracket[1] = '\0';
}
/* When the username signingkey is bad, program could be terminated
* because gpg exits without reading and then write gets SIGPIPE. */
signal(SIGPIPE, SIG_IGN);
memset(&gpg, 0, sizeof(gpg));
gpg.argv = args;
gpg.in = -1;
gpg.out = -1;
args[0] = "gpg";
args[1] = "-bsau";
args[2] = signingkey;
args[3] = NULL;
if (start_command(&gpg))
return error(_("could not run gpg."));
if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
close(gpg.in);
close(gpg.out);
finish_command(&gpg);
return error(_("gpg did not accept the tag data"));
}
close(gpg.in);
len = strbuf_read(buffer, gpg.out, 1024);
close(gpg.out);
if (finish_command(&gpg) || !len || len < 0)
return error(_("gpg failed to sign the tag"));
/* Strip CR from the line endings, in case we are on Windows. */
for (i = j = 0; i < buffer->len; i++)
if (buffer->buf[i] != '\r') {
if (i != j)
buffer->buf[j] = buffer->buf[i];
j++;
}
strbuf_setlen(buffer, j);
return 0;
return sign_buffer(buffer, buffer, get_signing_key());
}
static const char tag_template[] =
@ -270,21 +216,11 @@ static const char tag_template[] =
"# Write a tag message\n"
"#\n");
static void set_signingkey(const char *value)
{
if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey))
die(_("signing key value too long (%.10s...)"), value);
}
static int git_tag_config(const char *var, const char *value, void *cb)
{
if (!strcmp(var, "user.signingkey")) {
if (!value)
return config_error_nonbool(var);
set_signingkey(value);
return 0;
}
int status = git_gpg_config(var, value, cb);
if (status)
return status;
return git_default_config(var, value, cb);
}
@ -463,7 +399,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
if (keyid) {
sign = 1;
set_signingkey(keyid);
set_signing_key(keyid);
}
if (sign)
annotate = 1;

View File

@ -11,6 +11,7 @@
#include "run-command.h"
#include <signal.h>
#include "parse-options.h"
#include "gpg-interface.h"
static const char * const verify_tag_usage[] = {
"git verify-tag [-v|--verbose] <tag>...",
@ -19,42 +20,16 @@ static const char * const verify_tag_usage[] = {
static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
{
struct child_process gpg;
const char *args_gpg[] = {"gpg", "--verify", "FILE", "-", NULL};
char path[PATH_MAX];
size_t len;
int fd, ret;
int len;
fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
if (fd < 0)
return error("could not create temporary file '%s': %s",
path, strerror(errno));
if (write_in_full(fd, buf, size) < 0)
return error("failed writing temporary file '%s': %s",
path, strerror(errno));
close(fd);
/* find the length without signature */
len = parse_signature(buf, size);
if (verbose)
write_in_full(1, buf, len);
memset(&gpg, 0, sizeof(gpg));
gpg.argv = args_gpg;
gpg.in = -1;
args_gpg[2] = path;
if (start_command(&gpg)) {
unlink(path);
return error("could not run gpg.");
}
if (size == len)
return error("no signature found");
write_in_full(gpg.in, buf, len);
close(gpg.in);
ret = finish_command(&gpg);
unlink_or_warn(path);
return ret;
return verify_signed_buffer(buf, len, buf + len, size - len, NULL);
}
static int verify_tag(const char *name, int verbose)