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