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