gpg-interface.con commit t9813: avoid using pipes (c6f44e1)
   1#include "cache.h"
   2#include "run-command.h"
   3#include "strbuf.h"
   4#include "gpg-interface.h"
   5#include "sigchain.h"
   6#include "tempfile.h"
   7
   8static char *configured_signing_key;
   9static const char *gpg_program = "gpg";
  10
  11#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
  12#define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
  13
  14void signature_check_clear(struct signature_check *sigc)
  15{
  16        free(sigc->payload);
  17        free(sigc->gpg_output);
  18        free(sigc->gpg_status);
  19        free(sigc->signer);
  20        free(sigc->key);
  21        sigc->payload = NULL;
  22        sigc->gpg_output = NULL;
  23        sigc->gpg_status = NULL;
  24        sigc->signer = NULL;
  25        sigc->key = NULL;
  26}
  27
  28static struct {
  29        char result;
  30        const char *check;
  31} sigcheck_gpg_status[] = {
  32        { 'G', "\n[GNUPG:] GOODSIG " },
  33        { 'B', "\n[GNUPG:] BADSIG " },
  34        { 'U', "\n[GNUPG:] TRUST_NEVER" },
  35        { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
  36        { 'E', "\n[GNUPG:] ERRSIG "},
  37        { 'X', "\n[GNUPG:] EXPSIG "},
  38        { 'Y', "\n[GNUPG:] EXPKEYSIG "},
  39        { 'R', "\n[GNUPG:] REVKEYSIG "},
  40};
  41
  42void parse_gpg_output(struct signature_check *sigc)
  43{
  44        const char *buf = sigc->gpg_status;
  45        int i;
  46
  47        /* Iterate over all search strings */
  48        for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
  49                const char *found, *next;
  50
  51                if (!skip_prefix(buf, sigcheck_gpg_status[i].check + 1, &found)) {
  52                        found = strstr(buf, sigcheck_gpg_status[i].check);
  53                        if (!found)
  54                                continue;
  55                        found += strlen(sigcheck_gpg_status[i].check);
  56                }
  57                sigc->result = sigcheck_gpg_status[i].result;
  58                /* The trust messages are not followed by key/signer information */
  59                if (sigc->result != 'U') {
  60                        sigc->key = xmemdupz(found, 16);
  61                        /* The ERRSIG message is not followed by signer information */
  62                        if (sigc-> result != 'E') {
  63                                found += 17;
  64                                next = strchrnul(found, '\n');
  65                                sigc->signer = xmemdupz(found, next - found);
  66                        }
  67                }
  68        }
  69}
  70
  71int check_signature(const char *payload, size_t plen, const char *signature,
  72        size_t slen, struct signature_check *sigc)
  73{
  74        struct strbuf gpg_output = STRBUF_INIT;
  75        struct strbuf gpg_status = STRBUF_INIT;
  76        int status;
  77
  78        sigc->result = 'N';
  79
  80        status = verify_signed_buffer(payload, plen, signature, slen,
  81                                      &gpg_output, &gpg_status);
  82        if (status && !gpg_output.len)
  83                goto out;
  84        sigc->payload = xmemdupz(payload, plen);
  85        sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
  86        sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
  87        parse_gpg_output(sigc);
  88
  89 out:
  90        strbuf_release(&gpg_status);
  91        strbuf_release(&gpg_output);
  92
  93        return sigc->result != 'G' && sigc->result != 'U';
  94}
  95
  96void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
  97{
  98        const char *output = flags & GPG_VERIFY_RAW ?
  99                sigc->gpg_status : sigc->gpg_output;
 100
 101        if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
 102                fputs(sigc->payload, stdout);
 103
 104        if (output)
 105                fputs(output, stderr);
 106}
 107
 108/*
 109 * Look at GPG signed content (e.g. a signed tag object), whose
 110 * payload is followed by a detached signature on it.  Return the
 111 * offset where the embedded detached signature begins, or the end of
 112 * the data when there is no such signature.
 113 */
 114size_t parse_signature(const char *buf, unsigned long size)
 115{
 116        char *eol;
 117        size_t len = 0;
 118        while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
 119                        !starts_with(buf + len, PGP_MESSAGE)) {
 120                eol = memchr(buf + len, '\n', size - len);
 121                len += eol ? eol - (buf + len) + 1 : size - len;
 122        }
 123        return len;
 124}
 125
 126void set_signing_key(const char *key)
 127{
 128        free(configured_signing_key);
 129        configured_signing_key = xstrdup(key);
 130}
 131
 132int git_gpg_config(const char *var, const char *value, void *cb)
 133{
 134        if (!strcmp(var, "user.signingkey")) {
 135                set_signing_key(value);
 136        }
 137        if (!strcmp(var, "gpg.program")) {
 138                if (!value)
 139                        return config_error_nonbool(var);
 140                gpg_program = xstrdup(value);
 141        }
 142        return 0;
 143}
 144
 145const char *get_signing_key(void)
 146{
 147        if (configured_signing_key)
 148                return configured_signing_key;
 149        return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
 150}
 151
 152/*
 153 * Create a detached signature for the contents of "buffer" and append
 154 * it after "signature"; "buffer" and "signature" can be the same
 155 * strbuf instance, which would cause the detached signature appended
 156 * at the end.
 157 */
 158int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
 159{
 160        struct child_process gpg = CHILD_PROCESS_INIT;
 161        int ret;
 162        size_t i, j, bottom;
 163        struct strbuf gpg_status = STRBUF_INIT;
 164
 165        argv_array_pushl(&gpg.args,
 166                         gpg_program,
 167                         "--status-fd=2",
 168                         "-bsau", signing_key,
 169                         NULL);
 170
 171        bottom = signature->len;
 172
 173        /*
 174         * When the username signingkey is bad, program could be terminated
 175         * because gpg exits without reading and then write gets SIGPIPE.
 176         */
 177        sigchain_push(SIGPIPE, SIG_IGN);
 178        ret = pipe_command(&gpg, buffer->buf, buffer->len,
 179                           signature, 1024, &gpg_status, 0);
 180        sigchain_pop(SIGPIPE);
 181
 182        ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
 183        strbuf_release(&gpg_status);
 184        if (ret)
 185                return error(_("gpg failed to sign the data"));
 186
 187        /* Strip CR from the line endings, in case we are on Windows. */
 188        for (i = j = bottom; i < signature->len; i++)
 189                if (signature->buf[i] != '\r') {
 190                        if (i != j)
 191                                signature->buf[j] = signature->buf[i];
 192                        j++;
 193                }
 194        strbuf_setlen(signature, j);
 195
 196        return 0;
 197}
 198
 199/*
 200 * Run "gpg" to see if the payload matches the detached signature.
 201 * gpg_output, when set, receives the diagnostic output from GPG.
 202 * gpg_status, when set, receives the status output from GPG.
 203 */
 204int verify_signed_buffer(const char *payload, size_t payload_size,
 205                         const char *signature, size_t signature_size,
 206                         struct strbuf *gpg_output, struct strbuf *gpg_status)
 207{
 208        struct child_process gpg = CHILD_PROCESS_INIT;
 209        static struct tempfile temp;
 210        int fd, ret;
 211        struct strbuf buf = STRBUF_INIT;
 212
 213        fd = mks_tempfile_t(&temp, ".git_vtag_tmpXXXXXX");
 214        if (fd < 0)
 215                return error_errno(_("could not create temporary file"));
 216        if (write_in_full(fd, signature, signature_size) < 0) {
 217                error_errno(_("failed writing detached signature to '%s'"),
 218                            temp.filename.buf);
 219                delete_tempfile(&temp);
 220                return -1;
 221        }
 222        close(fd);
 223
 224        argv_array_pushl(&gpg.args,
 225                         gpg_program,
 226                         "--status-fd=1",
 227                         "--keyid-format=long",
 228                         "--verify", temp.filename.buf, "-",
 229                         NULL);
 230
 231        if (!gpg_status)
 232                gpg_status = &buf;
 233
 234        sigchain_push(SIGPIPE, SIG_IGN);
 235        ret = pipe_command(&gpg, payload, payload_size,
 236                           gpg_status, 0, gpg_output, 0);
 237        sigchain_pop(SIGPIPE);
 238
 239        delete_tempfile(&temp);
 240
 241        ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
 242        strbuf_release(&buf); /* no matter it was used or not */
 243
 244        return ret;
 245}