gpg-interface.con commit gpg-interface.c: support getting key fingerprint via %GF format (3daaaab)
   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;
  10struct gpg_format {
  11        const char *name;
  12        const char *program;
  13        const char **verify_args;
  14        const char **sigs;
  15};
  16
  17static const char *openpgp_verify_args[] = {
  18        "--keyid-format=long",
  19        NULL
  20};
  21static const char *openpgp_sigs[] = {
  22        "-----BEGIN PGP SIGNATURE-----",
  23        "-----BEGIN PGP MESSAGE-----",
  24        NULL
  25};
  26
  27static const char *x509_verify_args[] = {
  28        NULL
  29};
  30static const char *x509_sigs[] = {
  31        "-----BEGIN SIGNED MESSAGE-----",
  32        NULL
  33};
  34
  35static struct gpg_format gpg_format[] = {
  36        { .name = "openpgp", .program = "gpg",
  37          .verify_args = openpgp_verify_args,
  38          .sigs = openpgp_sigs
  39        },
  40        { .name = "x509", .program = "gpgsm",
  41          .verify_args = x509_verify_args,
  42          .sigs = x509_sigs
  43        },
  44};
  45
  46static struct gpg_format *use_format = &gpg_format[0];
  47
  48static struct gpg_format *get_format_by_name(const char *str)
  49{
  50        int i;
  51
  52        for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
  53                if (!strcmp(gpg_format[i].name, str))
  54                        return gpg_format + i;
  55        return NULL;
  56}
  57
  58static struct gpg_format *get_format_by_sig(const char *sig)
  59{
  60        int i, j;
  61
  62        for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
  63                for (j = 0; gpg_format[i].sigs[j]; j++)
  64                        if (starts_with(sig, gpg_format[i].sigs[j]))
  65                                return gpg_format + i;
  66        return NULL;
  67}
  68
  69void signature_check_clear(struct signature_check *sigc)
  70{
  71        FREE_AND_NULL(sigc->payload);
  72        FREE_AND_NULL(sigc->gpg_output);
  73        FREE_AND_NULL(sigc->gpg_status);
  74        FREE_AND_NULL(sigc->signer);
  75        FREE_AND_NULL(sigc->key);
  76        FREE_AND_NULL(sigc->fingerprint);
  77}
  78
  79/* An exclusive status -- only one of them can appear in output */
  80#define GPG_STATUS_EXCLUSIVE    (1<<0)
  81/* The status includes key identifier */
  82#define GPG_STATUS_KEYID        (1<<1)
  83/* The status includes user identifier */
  84#define GPG_STATUS_UID          (1<<2)
  85/* The status includes key fingerprints */
  86#define GPG_STATUS_FINGERPRINT  (1<<3)
  87
  88/* Short-hand for standard exclusive *SIG status with keyid & UID */
  89#define GPG_STATUS_STDSIG       (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
  90
  91static struct {
  92        char result;
  93        const char *check;
  94        unsigned int flags;
  95} sigcheck_gpg_status[] = {
  96        { 'G', "GOODSIG ", GPG_STATUS_STDSIG },
  97        { 'B', "BADSIG ", GPG_STATUS_STDSIG },
  98        { 'U', "TRUST_NEVER", 0 },
  99        { 'U', "TRUST_UNDEFINED", 0 },
 100        { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID },
 101        { 'X', "EXPSIG ", GPG_STATUS_STDSIG },
 102        { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG },
 103        { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG },
 104        { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
 105};
 106
 107static void parse_gpg_output(struct signature_check *sigc)
 108{
 109        const char *buf = sigc->gpg_status;
 110        const char *line, *next;
 111        int i;
 112        int seen_exclusive_status = 0;
 113
 114        /* Iterate over all lines */
 115        for (line = buf; *line; line = strchrnul(line+1, '\n')) {
 116                while (*line == '\n')
 117                        line++;
 118                /* Skip lines that don't start with GNUPG status */
 119                if (!skip_prefix(line, "[GNUPG:] ", &line))
 120                        continue;
 121
 122                /* Iterate over all search strings */
 123                for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
 124                        if (skip_prefix(line, sigcheck_gpg_status[i].check, &line)) {
 125                                if (sigcheck_gpg_status[i].flags & GPG_STATUS_EXCLUSIVE) {
 126                                        if (++seen_exclusive_status > 1)
 127                                                goto found_duplicate_status;
 128                                }
 129
 130                                if (sigcheck_gpg_status[i].result)
 131                                        sigc->result = sigcheck_gpg_status[i].result;
 132                                /* Do we have key information? */
 133                                if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
 134                                        next = strchrnul(line, ' ');
 135                                        free(sigc->key);
 136                                        sigc->key = xmemdupz(line, next - line);
 137                                        /* Do we have signer information? */
 138                                        if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) {
 139                                                line = next + 1;
 140                                                next = strchrnul(line, '\n');
 141                                                free(sigc->signer);
 142                                                sigc->signer = xmemdupz(line, next - line);
 143                                        }
 144                                }
 145                                /* Do we have fingerprint? */
 146                                if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
 147                                        next = strchrnul(line, ' ');
 148                                        free(sigc->fingerprint);
 149                                        sigc->fingerprint = xmemdupz(line, next - line);
 150                                }
 151
 152                                break;
 153                        }
 154                }
 155        }
 156        return;
 157
 158found_duplicate_status:
 159        /*
 160         * GOODSIG, BADSIG etc. can occur only once for each signature.
 161         * Therefore, if we had more than one then we're dealing with multiple
 162         * signatures.  We don't support them currently, and they're rather
 163         * hard to create, so something is likely fishy and we should reject
 164         * them altogether.
 165         */
 166        sigc->result = 'E';
 167        /* Clear partial data to avoid confusion */
 168        FREE_AND_NULL(sigc->fingerprint);
 169        FREE_AND_NULL(sigc->signer);
 170        FREE_AND_NULL(sigc->key);
 171}
 172
 173int check_signature(const char *payload, size_t plen, const char *signature,
 174        size_t slen, struct signature_check *sigc)
 175{
 176        struct strbuf gpg_output = STRBUF_INIT;
 177        struct strbuf gpg_status = STRBUF_INIT;
 178        int status;
 179
 180        sigc->result = 'N';
 181
 182        status = verify_signed_buffer(payload, plen, signature, slen,
 183                                      &gpg_output, &gpg_status);
 184        if (status && !gpg_output.len)
 185                goto out;
 186        sigc->payload = xmemdupz(payload, plen);
 187        sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
 188        sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
 189        parse_gpg_output(sigc);
 190        status |= sigc->result != 'G' && sigc->result != 'U';
 191
 192 out:
 193        strbuf_release(&gpg_status);
 194        strbuf_release(&gpg_output);
 195
 196        return !!status;
 197}
 198
 199void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
 200{
 201        const char *output = flags & GPG_VERIFY_RAW ?
 202                sigc->gpg_status : sigc->gpg_output;
 203
 204        if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
 205                fputs(sigc->payload, stdout);
 206
 207        if (output)
 208                fputs(output, stderr);
 209}
 210
 211size_t parse_signature(const char *buf, size_t size)
 212{
 213        size_t len = 0;
 214        size_t match = size;
 215        while (len < size) {
 216                const char *eol;
 217
 218                if (get_format_by_sig(buf + len))
 219                        match = len;
 220
 221                eol = memchr(buf + len, '\n', size - len);
 222                len += eol ? eol - (buf + len) + 1 : size - len;
 223        }
 224        return match;
 225}
 226
 227void set_signing_key(const char *key)
 228{
 229        free(configured_signing_key);
 230        configured_signing_key = xstrdup(key);
 231}
 232
 233int git_gpg_config(const char *var, const char *value, void *cb)
 234{
 235        struct gpg_format *fmt = NULL;
 236        char *fmtname = NULL;
 237
 238        if (!strcmp(var, "user.signingkey")) {
 239                if (!value)
 240                        return config_error_nonbool(var);
 241                set_signing_key(value);
 242                return 0;
 243        }
 244
 245        if (!strcmp(var, "gpg.format")) {
 246                if (!value)
 247                        return config_error_nonbool(var);
 248                fmt = get_format_by_name(value);
 249                if (!fmt)
 250                        return error("unsupported value for %s: %s",
 251                                     var, value);
 252                use_format = fmt;
 253                return 0;
 254        }
 255
 256        if (!strcmp(var, "gpg.program") || !strcmp(var, "gpg.openpgp.program"))
 257                fmtname = "openpgp";
 258
 259        if (!strcmp(var, "gpg.x509.program"))
 260                fmtname = "x509";
 261
 262        if (fmtname) {
 263                fmt = get_format_by_name(fmtname);
 264                return git_config_string(&fmt->program, var, value);
 265        }
 266
 267        return 0;
 268}
 269
 270const char *get_signing_key(void)
 271{
 272        if (configured_signing_key)
 273                return configured_signing_key;
 274        return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
 275}
 276
 277int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
 278{
 279        struct child_process gpg = CHILD_PROCESS_INIT;
 280        int ret;
 281        size_t i, j, bottom;
 282        struct strbuf gpg_status = STRBUF_INIT;
 283
 284        argv_array_pushl(&gpg.args,
 285                         use_format->program,
 286                         "--status-fd=2",
 287                         "-bsau", signing_key,
 288                         NULL);
 289
 290        bottom = signature->len;
 291
 292        /*
 293         * When the username signingkey is bad, program could be terminated
 294         * because gpg exits without reading and then write gets SIGPIPE.
 295         */
 296        sigchain_push(SIGPIPE, SIG_IGN);
 297        ret = pipe_command(&gpg, buffer->buf, buffer->len,
 298                           signature, 1024, &gpg_status, 0);
 299        sigchain_pop(SIGPIPE);
 300
 301        ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
 302        strbuf_release(&gpg_status);
 303        if (ret)
 304                return error(_("gpg failed to sign the data"));
 305
 306        /* Strip CR from the line endings, in case we are on Windows. */
 307        for (i = j = bottom; i < signature->len; i++)
 308                if (signature->buf[i] != '\r') {
 309                        if (i != j)
 310                                signature->buf[j] = signature->buf[i];
 311                        j++;
 312                }
 313        strbuf_setlen(signature, j);
 314
 315        return 0;
 316}
 317
 318int verify_signed_buffer(const char *payload, size_t payload_size,
 319                         const char *signature, size_t signature_size,
 320                         struct strbuf *gpg_output, struct strbuf *gpg_status)
 321{
 322        struct child_process gpg = CHILD_PROCESS_INIT;
 323        struct gpg_format *fmt;
 324        struct tempfile *temp;
 325        int ret;
 326        struct strbuf buf = STRBUF_INIT;
 327
 328        temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
 329        if (!temp)
 330                return error_errno(_("could not create temporary file"));
 331        if (write_in_full(temp->fd, signature, signature_size) < 0 ||
 332            close_tempfile_gently(temp) < 0) {
 333                error_errno(_("failed writing detached signature to '%s'"),
 334                            temp->filename.buf);
 335                delete_tempfile(&temp);
 336                return -1;
 337        }
 338
 339        fmt = get_format_by_sig(signature);
 340        if (!fmt)
 341                BUG("bad signature '%s'", signature);
 342
 343        argv_array_push(&gpg.args, fmt->program);
 344        argv_array_pushv(&gpg.args, fmt->verify_args);
 345        argv_array_pushl(&gpg.args,
 346                         "--status-fd=1",
 347                         "--verify", temp->filename.buf, "-",
 348                         NULL);
 349
 350        if (!gpg_status)
 351                gpg_status = &buf;
 352
 353        sigchain_push(SIGPIPE, SIG_IGN);
 354        ret = pipe_command(&gpg, payload, payload_size,
 355                           gpg_status, 0, gpg_output, 0);
 356        sigchain_pop(SIGPIPE);
 357
 358        delete_tempfile(&temp);
 359
 360        ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
 361        strbuf_release(&buf); /* no matter it was used or not */
 362
 363        return ret;
 364}