gpg-interface.con commit repository: enable initialization of submodules (96dc883)
   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(sigc->payload);
  18        free(sigc->gpg_output);
  19        free(sigc->gpg_status);
  20        free(sigc->signer);
  21        free(sigc->key);
  22        sigc->payload = NULL;
  23        sigc->gpg_output = NULL;
  24        sigc->gpg_status = NULL;
  25        sigc->signer = NULL;
  26        sigc->key = NULL;
  27}
  28
  29static struct {
  30        char result;
  31        const char *check;
  32} sigcheck_gpg_status[] = {
  33        { 'G', "\n[GNUPG:] GOODSIG " },
  34        { 'B', "\n[GNUPG:] BADSIG " },
  35        { 'U', "\n[GNUPG:] TRUST_NEVER" },
  36        { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
  37        { 'E', "\n[GNUPG:] ERRSIG "},
  38        { 'X', "\n[GNUPG:] EXPSIG "},
  39        { 'Y', "\n[GNUPG:] EXPKEYSIG "},
  40        { 'R', "\n[GNUPG:] REVKEYSIG "},
  41};
  42
  43void parse_gpg_output(struct signature_check *sigc)
  44{
  45        const char *buf = sigc->gpg_status;
  46        int i;
  47
  48        /* Iterate over all search strings */
  49        for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
  50                const char *found, *next;
  51
  52                if (!skip_prefix(buf, sigcheck_gpg_status[i].check + 1, &found)) {
  53                        found = strstr(buf, sigcheck_gpg_status[i].check);
  54                        if (!found)
  55                                continue;
  56                        found += strlen(sigcheck_gpg_status[i].check);
  57                }
  58                sigc->result = sigcheck_gpg_status[i].result;
  59                /* The trust messages are not followed by key/signer information */
  60                if (sigc->result != 'U') {
  61                        sigc->key = xmemdupz(found, 16);
  62                        /* The ERRSIG message is not followed by signer information */
  63                        if (sigc-> result != 'E') {
  64                                found += 17;
  65                                next = strchrnul(found, '\n');
  66                                sigc->signer = xmemdupz(found, next - found);
  67                        }
  68                }
  69        }
  70}
  71
  72int check_signature(const char *payload, size_t plen, const char *signature,
  73        size_t slen, struct signature_check *sigc)
  74{
  75        struct strbuf gpg_output = STRBUF_INIT;
  76        struct strbuf gpg_status = STRBUF_INIT;
  77        int status;
  78
  79        sigc->result = 'N';
  80
  81        status = verify_signed_buffer(payload, plen, signature, slen,
  82                                      &gpg_output, &gpg_status);
  83        if (status && !gpg_output.len)
  84                goto out;
  85        sigc->payload = xmemdupz(payload, plen);
  86        sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
  87        sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
  88        parse_gpg_output(sigc);
  89
  90 out:
  91        strbuf_release(&gpg_status);
  92        strbuf_release(&gpg_output);
  93
  94        return sigc->result != 'G' && sigc->result != 'U';
  95}
  96
  97void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
  98{
  99        const char *output = flags & GPG_VERIFY_RAW ?
 100                sigc->gpg_status : sigc->gpg_output;
 101
 102        if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
 103                fputs(sigc->payload, stdout);
 104
 105        if (output)
 106                fputs(output, stderr);
 107}
 108
 109/*
 110 * Look at GPG signed content (e.g. a signed tag object), whose
 111 * payload is followed by a detached signature on it.  Return the
 112 * offset where the embedded detached signature begins, or the end of
 113 * the data when there is no such signature.
 114 */
 115size_t parse_signature(const char *buf, unsigned long size)
 116{
 117        char *eol;
 118        size_t len = 0;
 119        while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
 120                        !starts_with(buf + len, PGP_MESSAGE)) {
 121                eol = memchr(buf + len, '\n', size - len);
 122                len += eol ? eol - (buf + len) + 1 : size - len;
 123        }
 124        return len;
 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                set_signing_key(value);
 137        }
 138        if (!strcmp(var, "gpg.program")) {
 139                if (!value)
 140                        return config_error_nonbool(var);
 141                gpg_program = xstrdup(value);
 142        }
 143        return 0;
 144}
 145
 146const char *get_signing_key(void)
 147{
 148        if (configured_signing_key)
 149                return configured_signing_key;
 150        return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
 151}
 152
 153/*
 154 * Create a detached signature for the contents of "buffer" and append
 155 * it after "signature"; "buffer" and "signature" can be the same
 156 * strbuf instance, which would cause the detached signature appended
 157 * at the end.
 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
 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        static struct tempfile temp;
 211        int fd, ret;
 212        struct strbuf buf = STRBUF_INIT;
 213
 214        fd = mks_tempfile_t(&temp, ".git_vtag_tmpXXXXXX");
 215        if (fd < 0)
 216                return error_errno(_("could not create temporary file"));
 217        if (write_in_full(fd, signature, signature_size) < 0) {
 218                error_errno(_("failed writing detached signature to '%s'"),
 219                            temp.filename.buf);
 220                delete_tempfile(&temp);
 221                return -1;
 222        }
 223        close(fd);
 224
 225        argv_array_pushl(&gpg.args,
 226                         gpg_program,
 227                         "--status-fd=1",
 228                         "--keyid-format=long",
 229                         "--verify", temp.filename.buf, "-",
 230                         NULL);
 231
 232        if (!gpg_status)
 233                gpg_status = &buf;
 234
 235        sigchain_push(SIGPIPE, SIG_IGN);
 236        ret = pipe_command(&gpg, payload, payload_size,
 237                           gpg_status, 0, gpg_output, 0);
 238        sigchain_pop(SIGPIPE);
 239
 240        delete_tempfile(&temp);
 241
 242        ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
 243        strbuf_release(&buf); /* no matter it was used or not */
 244
 245        return ret;
 246}