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