gpg-interface.con commit signed push: teach smart-HTTP to pass "git push --signed" around (0ea47f9)
   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
  63/*
  64 * Look at GPG signed content (e.g. a signed tag object), whose
  65 * payload is followed by a detached signature on it.  Return the
  66 * offset where the embedded detached signature begins, or the end of
  67 * the data when there is no such signature.
  68 */
  69size_t parse_signature(const char *buf, unsigned long size)
  70{
  71        char *eol;
  72        size_t len = 0;
  73        while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
  74                        !starts_with(buf + len, PGP_MESSAGE)) {
  75                eol = memchr(buf + len, '\n', size - len);
  76                len += eol ? eol - (buf + len) + 1 : size - len;
  77        }
  78        return len;
  79}
  80
  81void set_signing_key(const char *key)
  82{
  83        free(configured_signing_key);
  84        configured_signing_key = xstrdup(key);
  85}
  86
  87int git_gpg_config(const char *var, const char *value, void *cb)
  88{
  89        if (!strcmp(var, "user.signingkey")) {
  90                set_signing_key(value);
  91        }
  92        if (!strcmp(var, "gpg.program")) {
  93                if (!value)
  94                        return config_error_nonbool(var);
  95                gpg_program = xstrdup(value);
  96        }
  97        return 0;
  98}
  99
 100const char *get_signing_key(void)
 101{
 102        if (configured_signing_key)
 103                return configured_signing_key;
 104        return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
 105}
 106
 107/*
 108 * Create a detached signature for the contents of "buffer" and append
 109 * it after "signature"; "buffer" and "signature" can be the same
 110 * strbuf instance, which would cause the detached signature appended
 111 * at the end.
 112 */
 113int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
 114{
 115        struct child_process gpg;
 116        const char *args[4];
 117        ssize_t len;
 118        size_t i, j, bottom;
 119
 120        memset(&gpg, 0, sizeof(gpg));
 121        gpg.argv = args;
 122        gpg.in = -1;
 123        gpg.out = -1;
 124        args[0] = gpg_program;
 125        args[1] = "-bsau";
 126        args[2] = signing_key;
 127        args[3] = NULL;
 128
 129        if (start_command(&gpg))
 130                return error(_("could not run gpg."));
 131
 132        /*
 133         * When the username signingkey is bad, program could be terminated
 134         * because gpg exits without reading and then write gets SIGPIPE.
 135         */
 136        sigchain_push(SIGPIPE, SIG_IGN);
 137
 138        if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
 139                close(gpg.in);
 140                close(gpg.out);
 141                finish_command(&gpg);
 142                return error(_("gpg did not accept the data"));
 143        }
 144        close(gpg.in);
 145
 146        bottom = signature->len;
 147        len = strbuf_read(signature, gpg.out, 1024);
 148        close(gpg.out);
 149
 150        sigchain_pop(SIGPIPE);
 151
 152        if (finish_command(&gpg) || !len || len < 0)
 153                return error(_("gpg failed to sign the data"));
 154
 155        /* Strip CR from the line endings, in case we are on Windows. */
 156        for (i = j = bottom; i < signature->len; i++)
 157                if (signature->buf[i] != '\r') {
 158                        if (i != j)
 159                                signature->buf[j] = signature->buf[i];
 160                        j++;
 161                }
 162        strbuf_setlen(signature, j);
 163
 164        return 0;
 165}
 166
 167/*
 168 * Run "gpg" to see if the payload matches the detached signature.
 169 * gpg_output, when set, receives the diagnostic output from GPG.
 170 * gpg_status, when set, receives the status output from GPG.
 171 */
 172int verify_signed_buffer(const char *payload, size_t payload_size,
 173                         const char *signature, size_t signature_size,
 174                         struct strbuf *gpg_output, struct strbuf *gpg_status)
 175{
 176        struct child_process gpg;
 177        const char *args_gpg[] = {NULL, "--status-fd=1", "--verify", "FILE", "-", NULL};
 178        char path[PATH_MAX];
 179        int fd, ret;
 180        struct strbuf buf = STRBUF_INIT;
 181        struct strbuf *pbuf = &buf;
 182
 183        args_gpg[0] = gpg_program;
 184        fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
 185        if (fd < 0)
 186                return error(_("could not create temporary file '%s': %s"),
 187                             path, strerror(errno));
 188        if (write_in_full(fd, signature, signature_size) < 0)
 189                return error(_("failed writing detached signature to '%s': %s"),
 190                             path, strerror(errno));
 191        close(fd);
 192
 193        memset(&gpg, 0, sizeof(gpg));
 194        gpg.argv = args_gpg;
 195        gpg.in = -1;
 196        gpg.out = -1;
 197        if (gpg_output)
 198                gpg.err = -1;
 199        args_gpg[3] = path;
 200        if (start_command(&gpg)) {
 201                unlink(path);
 202                return error(_("could not run gpg."));
 203        }
 204
 205        write_in_full(gpg.in, payload, payload_size);
 206        close(gpg.in);
 207
 208        if (gpg_output) {
 209                strbuf_read(gpg_output, gpg.err, 0);
 210                close(gpg.err);
 211        }
 212        if (gpg_status)
 213                pbuf = gpg_status;
 214        strbuf_read(pbuf, gpg.out, 0);
 215        close(gpg.out);
 216
 217        ret = finish_command(&gpg);
 218
 219        unlink_or_warn(path);
 220
 221        ret |= !strstr(pbuf->buf, "\n[GNUPG:] GOODSIG ");
 222        strbuf_release(&buf); /* no matter it was used or not */
 223
 224        return ret;
 225}