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