gpg-interface.c 12 KB

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